Simulation Coding Problems: 88 Questions with Solutions

88 simulation coding problems — 48 easy · 39 medium · 1 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 6-day plan.

  • Problems: 88
  • By difficulty: 48 easy · 39 medium · 1 hard
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
  • Cost: Free on every plan; sign in to run and submit

Some problems are solved by doing exactly what the statement says, carefully: move the robot, apply the operations in order, deal the cards, run the game. The difficulty is not the idea but the state — keeping every variable right through every step — and the discipline of writing the loop so that each rule is applied once and in the right order.

How simulation works, step by step

commandsGGRGGLGG##012340123↑at (1, 3) facing north · blocked steps: 2
Where a robot ends up after a command string, by simulating each step. Example: grid 5×4, obstacles at (2, 2) and (4, 1), start (0, 0) facing north, commands = "GGRGGLGG"
  1. The robot starts at (0, 0) facing north. G steps one cell forward unless an obstacle (#) or the edge of the grid is in the way; L and R turn 90° on the spot. No cleverness is needed: following the rules exactly is the solution.
  2. G: facing north, a step adds (0, 1) to the position. (0, 1) is free, so the robot moves there.
  3. Another G, still facing north: (0, 2) is free too, so the robot moves again.
  4. R at (0, 2) turns the robot from north to east without moving. Headings are an index into [north, east, south, west], so R is +1 and L is −1, mod 4.
  5. G: now facing east, a step adds (1, 0) instead, and (1, 2) is free, so the robot moves there.
  6. G at (1, 2): the cell ahead, (2, 2), holds an obstacle, so the robot stays put. The command is used up, but the position does not change.
  7. L at (1, 2) turns the robot from east to north without moving. Only the heading changes, so the next G goes a new way.
  8. G: now facing north, a step adds (0, 1) instead, and (1, 3) is free, so the robot moves there.
  9. G at (1, 3): the cell ahead, (1, 4), is off the grid, so the wall stops the robot exactly as an obstacle would.
  10. All 8 commands are done: the robot ends at (1, 3) facing north, after 2 blocked steps. With the obstacles in a hash set each command is O(1), so the run is O(n) for n commands.

Simulation study plan

12 of the 88 Simulation problems (4 easy, 7 medium and 1 hard) over 6 days, about 6 h 30 min in all — the pattern first, then easiest to hardest. After that, the other 76 in the full list below are practice at your own pace. Then move on to Enumeration.

Day 1

Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 6

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

Next topic: Enumeration

Simulation: the essentials

When to reach for it

The statement describes a process step by step — a robot's moves, a game's turns, rounds of an operation on an array — and the limits make running it affordable: steps × cost per step should stay within about 10⁷–10⁸ simple operations. If the number of steps is huge (10⁹ rounds), the state must repeat or follow a formula; find that instead of running every step.

The pattern

Write down the full state first, every variable that changes between steps, then a function from one state to the next. When all cells or players update "at the same time", compute the new state from an untouched copy of the old one, or encode old and new together in each cell. Keep each rule on its own line, in the statement's order.

def life_step(board):
    R, C = len(board), len(board[0])
    old = [row[:] for row in board]     # read the old, write the new
    for r in range(R):
        for c in range(C):
            live = sum(old[i][j]
                       for i in range(max(0, r - 1), min(R, r + 2))
                       for j in range(max(0, c - 1), min(C, c + 2))) - old[r][c]
            board[r][c] = int(live == 3 or (live == 2 and old[r][c] == 1))

Cost

Steps × cost per step. Copying the state each step adds O(size) space; encoding both values in one cell (say, 2 for "alive, about to die") removes it.

Common mistakes

  • Updating in place when the rules are simultaneous, so later cells see half-updated neighbours.
  • Applying rules in a different order from the statement, or one rule twice in a step.
  • An off-by-one in the number of rounds — whether the initial state counts as round 0.
  • Running 10⁹ steps of a state that repeats; detect the cycle and skip ahead.

Start with

All simulation problems

Easy (48)

Medium (39)

Hard (1)

Companies that ask simulation problems

Next topic: Enumeration