AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started

Topic

Simulation coding problems

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.

137 problems · 66 easy · 66 medium · 5 hard · judged by hidden tests in 13 languages

137problems

66 easy

66 medium

5 hard

Sign in to tick off what you solve.

Study plan: 14 problems · about 8 h 10 min

Start the study plan

Next up: Concatenation of Array

How it works

Simulation, step by step

Where a robot ends up after a command string, by simulating each step

grid 5×4, obstacles at (2, 2) and (4, 1), start (0, 0) facing north, commands = "GGRGGLGG"

commandsGGRGGLGG##012340123↑at (0, 0) facing north

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.

0:01 / 0:30Step 1 of 10
Transcript · 10 steps

Study plan

Learn simulation in 8 days

14 of the 137 Simulation problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 123 in the full list below are practice at your own pace. Then move on to Enumeration.

  1. Day 1

    about 1 h 15 min

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

  2. Day 2

    about 55 min

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

  3. Day 3

    about 1 h 10 min

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

  4. Day 4

    about 1 h 10 min

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

  5. Day 5

    about 1 h 10 min

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

  6. Day 6

    about 50 min

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

  7. Day 7

    about 50 min

    Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

  8. Day 8

    about 50 min

    Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

    Next topic: Enumeration

Simulation Coding Problems: 137 Questions with Solutions

137 simulation coding problems — 66 easy · 66 medium · 5 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 137
  • By difficulty: 66 easy · 66 medium · 5 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

14 of the 137 Simulation problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 123 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.

Day 7

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Day 8

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

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 (66)

Medium (66)

Hard (5)

Companies that ask simulation problems

Next topic: Enumeration