← Openai Interview Insights

Openai·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Coding round at OpenAI for a software engineer position. One algorithmic problem, grid-based BFS, nothing too wild on the surface but the 8-directional spread detail is easy to miss if you're on autopilot.

Questions Asked (1)

Q1

Given an m x n grid where cells are either infected or uninfected, infected cells spread to all 8 neighbors each day. How many days until no new infections occur?

Algorithms & Data Structures
Author's notes

My first instinct was standard BFS and I almost forgot the diagonals.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the spread as a multi-source BFS on a graph where each cell is a node and edges connect to all 8 neighbors. Compute the maximum shortest-path distance from any initially infected cell to any uninfected cell; that distance is the number of days until no new infections occur. If there are no uninfected cells, return 0.

Pro tip: Clarify edge cases upfront: if the grid has no infected cells, the answer is 0 (no spread); if all cells are infected initially, also 0. Also mention that using a queue with level-order traversal naturally tracks days.

1. Clarify the problem and edge cases

Confirm that infection spreads to all 8 neighbors (including diagonals) and that a cell once infected remains infected. Discuss edge cases: no infected cells, all cells infected, and grids with uninfected cells unreachable from any infected cell.

2. Model as a graph and choose BFS

Treat each cell as a node with edges to its 8 neighbors. Use multi-source BFS starting from all initially infected cells simultaneously to compute the minimum time each cell becomes infected.

3. Implement BFS with day tracking

Initialize a queue with all infected cells and a day counter. Process the queue level by level (each level = one day), infecting uninfected neighbors and adding them to the queue. Stop when the queue is empty.

4. Compute the answer

The number of days is the number of BFS levels processed minus 1 (or the maximum distance from any infected cell to any uninfected cell). If there are no uninfected cells, return 0.

5. Analyze complexity and test

State time complexity O(m*n) since each cell is visited once, and space complexity O(m*n) for the queue and visited set. Walk through a small example to verify correctness.

Key Points to Mention

  • Multi-source BFS to simulate simultaneous spread from all initially infected cells.
  • 8-directional adjacency (including diagonals) and how it differs from 4-directional.
  • Tracking days via level-order traversal (BFS levels) or by storing distance in the queue.
  • Edge cases: no infected cells, all cells infected, and disconnected components with uninfected cells.
  • Time and space complexity: O(m*n) time and O(m*n) space.
  • Alternative approaches like DFS with memoization or union-find, and why BFS is optimal.

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