← Openai Interview Insights

Openai·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Interviewed for a software engineering role at OpenAI and got a grid infection spread problem. Pretty classic BFS variant but the edge case indexing tripped me up more than I expected.

Questions Asked (1)

Q1

Given a 2D grid where cells are either infected ('X') or healthy ('.'), each day the infection spreads from every infected cell to all 8 neighboring cells. How many days until the infection can no longer spread?

Algorithms & Data Structures
Author's notes

Multi-source BFS from all starting X cells, track the max depth, done.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the grid as a graph and use multi-source BFS starting from all initially infected cells simultaneously. Track the number of BFS layers (days) until no new cells are infected, then return that count.

Pro tip: Clarify edge cases upfront: if there are no infected cells, the answer is 0; if all cells are infected, also 0. Also, mention that you can optimize space by modifying the grid in-place or using a queue of coordinates.

1. Understand the problem

Restate the problem: infection spreads to all 8 neighbors each day. We need the number of days until no more spread. Confirm that diagonal spread is allowed and that we count full days.

2. Choose the algorithm

Recognize this as a multi-source BFS problem. All initially infected cells are sources at day 0. Each BFS layer represents one day of spread.

3. Implement BFS

Initialize a queue with all infected cells. For each day, process all cells currently in the queue (snapshot the size), and for each, check all 8 neighbors. If a neighbor is healthy, infect it and add to queue. Increment day count after each layer.

4. Handle termination and edge cases

Stop when the queue is empty. Return the number of days elapsed. Handle cases with no infected cells (return 0) and all infected cells (return 0).

5. Analyze complexity

Time complexity: O(R*C) since each cell is processed once. Space complexity: O(R*C) for the queue in worst case.

Key Points to Mention

  • Multi-source BFS: all infected cells start at day 0 and spread simultaneously.
  • 8-directional neighbors: include diagonals, so check all 8 adjacent cells.
  • Day counting: increment day after processing each BFS layer.
  • Edge cases: no infected cells or all infected cells yield 0 days.
  • In-place modification: can mark infected cells as 'X' to avoid revisiting.
  • Complexity: O(R*C) time and space, where R and C are grid dimensions.

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