Pattern

Simulation

Follow the problem's stated rules step by step and get the bookkeeping exactly right, rather than deriving a formula.

What it is

Simulation questions describe a process with explicit rules — asteroids colliding, a board evolving generation by generation, a path traced through a grid — and ask you to compute the end state by carrying out those rules faithfully. There's usually no clever formula that shortcuts the work; the challenge is entirely in choosing the right data structure to make each step of the simulation efficient and correct (a stack for "the most recent unresolved thing," careful boundary tracking for a shrinking region) and in handling the process's edge cases (a collision chain, a boundary between regions, in-place updates that must not see already-updated data).

When it applies

Tells in the problem

The classic mistake

Writing a new value into a cell or list, then later in the same pass reading that written value as if it were the untouched original, lets the simulation see its own in-progress output as if it were still the original input — the mistake that shows up most. This is easy to miss because a normal, in-order loop looks correct at first glance; the bug only shows up on inputs where an early write actually gets revisited before the pass finishes (adjacent cells in a "simultaneous" update, a chain of three or more collisions in one direction). The habit that catches it: before writing the loop, ask "could a later step in this same pass observe a value I already changed, when the rules say it should see the original?" — if yes, either snapshot the original state first, encode "new" values in a way distinguishable from "old" ones, or process in an order where the hazard can't occur.

Questions to start with

All 3 simulation questions

Problem statement, examples and hints are free on every page; the worked approach, solution and tests are in the company guides.

Free starter pack

13 must-tier questions with full solutions

Sign in with an email — no purchase — and read them in the full reader.

No password, no spam. One sign-in email; you can read the pack in the full reader.