← Openai Interview Insights

Openai·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Interviewed for a software engineer role at OpenAI and got a grid infection spread problem. Pretty standard BFS territory but the edge case around unreachable cells tripped me up a bit.

Questions Asked (1)

Q1

Given an m x n grid where cells are either healthy or infected, infection spreads to all 4-directional neighbors each day. How many days until no further spread is possible? Return -1 if any healthy cell can never be reached.

Algorithms & Data Structures
Author's notes

Knew pretty quickly it was multi-source BFS, seeded from all infected cells at once.

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 days (BFS levels) until no more cells can be infected, and after BFS, check if any healthy cells remain uninfected; if so, return -1.

Pro tip: Clarify edge cases upfront: if there are no healthy cells initially, return 0; if there are no infected cells and healthy cells exist, return -1. Also, mention that you can optimize space by reusing the grid to mark visited cells.

1. Understand the problem and edge cases

Restate the problem: infection spreads to 4-directional neighbors each day. Identify edge cases: no infected cells, no healthy cells, unreachable healthy cells (e.g., isolated by walls or boundaries).

2. Choose the right algorithm

Recognize this as a multi-source BFS problem where all initially infected cells are sources. BFS naturally simulates the day-by-day spread because it processes nodes level by level.

3. Implement BFS with a queue

Initialize a queue with all infected cells and count healthy cells. For each day (BFS level), process all current infected cells, infect their healthy neighbors, add them to the queue, and decrement the healthy count. Increment days after each level.

4. Check for unreachable cells

After BFS, if the healthy count is greater than 0, return -1 because some healthy cells were never infected. Otherwise, return the number of days (or 0 if no healthy cells initially).

5. Analyze complexity and optimize

Time complexity: O(m*n) since each cell is processed once. Space complexity: O(m*n) for the queue in worst case. Mention that you can modify the grid in-place to avoid a separate visited set.

Key Points to Mention

  • Multi-source BFS: all initially infected cells are sources, processed simultaneously.
  • Level-by-level processing to count days: each BFS level corresponds to one day.
  • Tracking the number of healthy cells to detect unreachable cells.
  • Edge cases: no infected cells, no healthy cells, unreachable healthy cells.
  • Time and space complexity: O(m*n) time, O(m*n) space.
  • In-place modification of the grid to mark infected cells and save space.

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