OpenAI logoOpenAI
Coding·60 minFree preview

Infection Spread / Cellular Automata

M×N grid with infected cells (X) and susceptible cells (./*); infection propagates each day under escalating rules. 5 sub-parts in 60 min; passing bar is typically solving the first 3 with clean BFS.

SWE
MLE
RE
RS
EM
algorithm
simulation
bfs
medium
Frequency
Very high
Last asked
2026-11-14
Stage
phone-screen · tech-screen

Problem

Part 1 (basic spread)

  • Each day every infected cell (1) simultaneously infects all 4-neighbor healthy (0) cells (orthogonal only, no diagonals); cells newly infected today only start propagating tomorrow.
    • The general rule the levels build on: a healthy cell becomes infected next step if it has at least N infected 4-neighbors. Part 1 is the N = 1 specialization (any single infected neighbor suffices); later parts raise N (Part 4) or add state.
    • Minority variant: some loops use 8-neighbor propagation (diagonal-inclusive) — clarify before coding; it changes day-counts on every test case.
  • Multiple infection sources allowed.
  • Return the number of days to reach full infection, or -1 if it is impossible (no infection source, or some cells are permanently unreachable).
    • Minority variant: return days until the grid stabilizes rather than days until full infection; clarify the stopping condition before coding.
  • Edge cases: empty grid, no sources (return -1), all-infected (return 0), 1×1, multiple sources, single row/column line spread.
def time_to_full_infection(grid: list[list[int]]) -> int:
    """
    Args:
        grid: An n×m grid where 0 = healthy, 1 = infected.
    Returns:
        Steps until all cells are infected, or -1 if impossible
        (no initial source, or unreachable healthy cells remain).
    """
    ...

Part 2 (immune cells)

  • Introduce immune cells (2): never get infected, never propagate, skipped when counting neighbors.
    • Minority variant (encoding): the immune sentinel is sometimes -1 instead of 2; some loops use letter notation — . / X / I for healthy / infected / immune — clarify encoding before coding.
  • Return days until all reachable healthy cells are infected, or -1 if any healthy cell remains permanently blocked behind immune walls (it will never be infected).
def time_to_full_infection_with_immunity(grid: list[list[int]]) -> int:
    """
    Grid values: 0 = healthy, 1 = infected, 2 = immune (permanent wall).
    Returns steps until all non-immune healthy cells are infected,
    or -1 if any healthy cell is unreachable (walled off by immune cells).
    """
    ...

Part 3 (D-day recovery → immunity)

  • After being infected for D days, a cell becomes immune (2, self-heals — no longer propagates).
  • Balance condition: no active infected cells remain (grid is fully stable — some cells may be healthy forever).
  • Watch day semantics / off-by-one: a cell infected on day t becomes immune when current_day - t >= D; immunity takes effect before spread on the same tick.
def time_to_stable_state(grid: list[list[int]], D: int) -> int:
    """
    Grid values: 0 = healthy, 1 = initially infected.
    D: days until an infected cell becomes immune (stops spreading).
    Returns days until no active infections remain.
    """
    ...

Part 4 (threshold + death) — three confirmed variants

  • Variant A: a healthy cell only becomes infected next day if it has ≥ K infected 4-neighbors (threshold spread).
  • Variant B: an infected cell that's alive and not yet immune begins a death countdown if ≥ K infected neighbors; dies after N days. In the stricter version, a cell that was infected while surrounded by at least K infected neighbors dies when its recovery timer expires instead of becoming immune. Return days-to-end + final death count.
  • Variant C (composite): Part 1+2+3 + death all stacked.

Part 5 (very few candidates reach this)

  • Each day, choose any row or column and burn everything on it. Minimize total deaths.
    • Minority variant: choose a row, a column, or no burn to minimize total deaths; clarify whether this is a single choice or a repeated daily decision before coding.
  • Some loops swap this optimization branch for engineering follow-ups ("what if the grid is huge").

Examples

Part 1 (N = 1), 4-neighbor spread:

  • Single center source in a 3×3 → 2 (neighbors at step 1, corners at step 2).
  • Two opposite corners infected in a 3×3 → 2 (waves meet in the middle).
  • All-healthy grid → -1 (no source can start the spread).
  • Single-row line [[1,0,0,0,0]] → 4 (linear chain; a single center source in a 5×5 likewise reaches the farthest cell in 4).
  • [[1]] → 0; [[0]] → -1.

Variants

Recovery-first, 8-direction version

  • One recent phone screen skipped the incremental build-up entirely: the round opened directly on the D-day recovery part, and infection spread in all 8 directions (diagonals included). Expect the parts to be reordered or merged, and confirm the neighbor set before writing the spread step.

Pitfalls

  • Extreme speed focus. Interviewers sometimes frame "finish all 5 to pass", but in practice a "strong" verdict is reachable after just Parts 1-3 with clean edge cases.
  • A current Part 4 framing expresses spread/death as a neighbor-count threshold rather than a fixed direction list; ask whether K is an infection threshold, a death threshold, or both before coding.
  • Listen to the interviewer's full description before coding. The verbal walkthrough usually contains the intended approach; one candidate who started writing before finishing the prompt missed that the round began at the recovery stage with 8-direction spread.

Preparation

  • Drill until you can finish Parts 1-3 in 30-40 min with all fixed tests passing and edges handled.
  • Focus drills: synchronous update (double buffer vs queue + day stamp), off-by-one, multi-source BFS, immune barriers.
  • Don't get fancy — clarity beats efficiency.
  • Turn the Part 1–4 spec above into a practice harness with fixed tests (an LLM can generate one from the spec), then solve it against the 30–40 minute target.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...