← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Senior

Senior
Apr 2026

Summary

Google SWE interview with a multi-part algorithmic problem about simulating infection spread on a 2D grid. The problem kept building on itself across four parts, which was either clever or exhausting depending on how your brain works under pressure.

Questions Asked (4)

Q1

Given a 2D grid where cells can be healthy, infected, recovered, or walls, compute the state of the grid after exactly one step of infection spread. Healthy cells become infected if any orthogonal neighbor is infected; all other cells stay the same. Updates are simultaneous.

Algorithms & Data Structures
Author's notes

Pretty clean setup, just scan every cell and check neighbors.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and edge cases, then propose a solution that reads the original grid and writes to a new grid to ensure simultaneous updates. Discuss time and space complexity, and consider optimizations like in-place marking if allowed.

Pro tip: Mention that you would use a separate grid or encode state changes to avoid overwriting cells before their neighbors are processed, demonstrating awareness of the simultaneity requirement.

1. Clarify the problem

Ask about grid dimensions, cell types, and whether in-place modification is allowed. Confirm that updates are simultaneous and that walls block infection.

2. Choose a strategy

Decide between using a new grid or in-place marking. For simplicity and correctness, a new grid is often preferred, but in-place can save space if carefully implemented.

3. Implement the solution

Iterate through each cell, check its orthogonal neighbors for infection, and update the new grid accordingly. Ensure walls and recovered cells remain unchanged.

4. Analyze complexity

State that time complexity is O(rows * cols) and space complexity is O(rows * cols) for the new grid, or O(1) extra space if using in-place marking.

5. Test with examples

Walk through a small example, including edge cases like no infected cells, all walls, or infection at borders.

Key Points to Mention

  • Simultaneous update requirement and how to handle it (e.g., using a separate grid or state encoding).
  • Time and space complexity analysis.
  • Edge cases: empty grid, no infected cells, all walls, infection at borders.
  • Orthogonal neighbors definition (up, down, left, right).
  • In-place modification technique if space optimization is needed.
  • Clarifying questions about input format and constraints.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Extend the single-step simulation to run for k steps, where walls permanently block infection spread and recovered cells are immune. Return the grid after k steps.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Straightforward extension but the wall behavior is worth thinking through carefully.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the grid as a 2D array and simulate each step by computing the next state based on the current state, ensuring walls block spread and recovered cells are immune. Use a queue or multi-source BFS to efficiently propagate infection from all infected cells simultaneously, updating states step by step until k steps are completed.

Pro tip: Clarify the state transitions and edge cases upfront (e.g., walls, immunity, simultaneous updates) to avoid bugs, and discuss time/space complexity trade-offs between naive simulation and BFS.

1. Clarify rules and state representation

Define cell states (e.g., empty, wall, infected, recovered) and confirm that infection spreads to orthogonal neighbors, walls block permanently, and recovered cells are immune. Decide on data structures to represent the grid and track changes.

2. Design step simulation logic

For each step, compute the next state by checking each infected cell's neighbors. Ensure updates are simultaneous (use a copy or two-phase update) to avoid cascading within the same step.

3. Optimize with BFS or queue

Instead of scanning the entire grid each step, use a queue of infected cells to process only active frontiers. This reduces time complexity, especially for sparse infections.

4. Handle k steps and termination

Loop k times or until no new infections occur. After k steps, return the grid. Consider early termination if the infection cannot spread further.

5. Analyze complexity and edge cases

Discuss time and space complexity (e.g., O(k * N*M) for naive, O(N*M) for BFS). Mention edge cases like k=0, no infected cells, all walls, or grid boundaries.

Key Points to Mention

  • Simultaneous update of cell states to prevent within-step cascading
  • Use of multi-source BFS to efficiently propagate infection from all initially infected cells
  • State transitions: infected -> recovered after one step, recovered cells are immune
  • Walls permanently block infection and never change state
  • Time and space complexity analysis, comparing naive simulation vs. BFS
  • Edge cases: k=0, no initial infection, grid boundaries, and early termination

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Add a recovery mechanic: each infected cell tracks how many steps it has been infected. After exactly d steps of infection, the cell recovers permanently. Simulate k steps and return the final grid.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I started fumbling a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the rules for infection spread and recovery, then design a simulation that tracks both the infection state and the number of steps each cell has been infected. Use a separate grid or data structure to store infection timers, and update the grid in discrete steps for k iterations, ensuring recovered cells become permanently immune.

Pro tip: Discuss the trade-offs between using a single grid with encoded states versus separate grids for infection status and timers, and highlight how your choice affects time and space complexity. Also, mention edge cases like d=0 or k=0 to show thoroughness.

1. Clarify requirements and constraints

Ask questions to confirm the rules: how infection spreads (e.g., to orthogonal neighbors), what happens when a cell recovers (does it become susceptible again?), and the initial state of the grid. Also confirm the range of d and k.

2. Choose data structures

Decide whether to use a single grid with integer values representing states (e.g., -1 for healthy, 0..d for infected steps, d+1 for recovered) or separate grids for infection status and timers. Consider memory and ease of update.

3. Design simulation loop

For each of the k steps, iterate through the grid to identify newly infected cells based on the previous state, update infection timers, and handle recoveries. Use a copy or buffer to avoid overwriting states mid-step.

4. Implement recovery logic

Increment the infection timer for each infected cell each step. When a cell's timer reaches d, mark it as recovered permanently, ensuring it no longer spreads infection or becomes reinfected.

5. Analyze complexity and edge cases

State the time complexity O(k * N * M) and space complexity O(N * M). Discuss edge cases such as d=0 (immediate recovery), k=0 (no steps), and grids with no initial infection.

Key Points to Mention

  • State representation: using integers to encode healthy, infected (with step count), and recovered states.
  • Simultaneous update: using a copy of the grid or a buffer to ensure all infections spread based on the same time step.
  • Recovery condition: after exactly d steps, the cell becomes permanently immune and cannot be reinfected.
  • Time and space complexity: O(k * N * M) time and O(N * M) space, with potential optimizations like tracking active infected cells.
  • Edge cases: d=0, k=0, no initial infected cells, and all cells infected.
  • Trade-offs: single grid vs. multiple grids, and in-place vs. copy-based updates.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q4

Combine all previous rules and add early stopping: halt the simulation as soon as the grid reaches a fixed point where no further changes are possible. A fixed point requires both that no healthy cell is adjacent to an infected cell AND that no infected cells remain. Return the final grid and the actual number of steps taken.

Algorithms & Data StructuresSystem Design
Author's notes

The stopping condition tripped me up more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the simulation as a state transition where each step simultaneously updates all cells based on the previous state. After each step, check if the grid has reached a fixed point: no infected cells and no healthy cell adjacent to an infected cell. If so, stop and return the grid and the number of steps taken; otherwise, continue until the fixed point is reached.

Pro tip: Emphasize the importance of simultaneous updates to avoid order-dependent artifacts, and discuss how early stopping can be implemented efficiently by tracking the number of infected cells and the set of healthy cells adjacent to infected ones.

1. Understand the problem and define fixed point

Clarify that a fixed point occurs when there are no infected cells and no healthy cell is adjacent to an infected cell. This means the infection has either died out or all reachable healthy cells have been infected.

2. Design the simulation loop

Plan a loop that continues until the fixed point is reached. In each iteration, compute the next state of the grid based on the current state, ensuring all updates are simultaneous.

3. Implement state transition

For each cell, determine its next state: infected cells become healthy (or remain infected if the rule is different? Actually, based on the problem, infected cells likely become healthy after one step? But the problem says 'no infected cells remain' for fixed point, so infected cells must eventually disappear. Typically, in such simulations, infected cells become healthy after one step. So we need to clarify the rules. However, the problem statement says 'combine all previous rules', so we assume the rules are: each step, any healthy cell adjacent to an infected cell becomes infected, and infected cells become healthy? Or infected cells remain infected? The fixed point requires no infected cells, so infected cells must be removed at some point. Probably the rule is: infected cells become healthy after one step, and healthy cells adjacent to infected become infected. So we need to apply both simultaneously.)

4. Check for fixed point after each step

After updating the grid, check if there are any infected cells. If none, also check if any healthy cell is adjacent to an infected cell (but since no infected cells, this is automatically false). So the condition simplifies to: no infected cells. However, the problem explicitly states both conditions, so we should check both to be safe.

5. Return result

Once the fixed point is reached, return the final grid and the number of steps taken (the number of iterations performed before reaching the fixed point).

Key Points to Mention

  • Simultaneous updates: all cells change state based on the previous grid, not the current one being modified.
  • Fixed point condition: no infected cells AND no healthy cell adjacent to an infected cell.
  • Early stopping: break the loop as soon as the fixed point is detected to avoid unnecessary iterations.
  • Efficiency: use a queue or set to track infected cells and their neighbors to avoid scanning the entire grid each step.
  • Edge cases: initial grid with no infected cells (0 steps), or all cells infected (fixed point after one step if infected cells become healthy).
  • Time and space complexity: O(N*M) per step, but with early stopping, total steps may be less than the maximum possible.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.