← Openai Interview Insights

Openai·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

Interviewed at OpenAI for a software engineering role and got hit with a grid simulation problem that looked straightforward but had a few edge cases that tripped me up.

Questions Asked (1)

Q1

Given a 2D grid where each cell is either healthy (0), infected (1), or immune (-1), simulate infection spreading to adjacent healthy cells each minute. Return the minimum number of minutes to infect all healthy cells, or -1 if it's not possible.

Algorithms & Data Structures
Author's notes

My first instinct was BFS and I got that part right.

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 time each healthy cell gets infected, and after BFS, check if any healthy cell remains uninfected; if so, return -1, else return the maximum time.

Pro tip: Clarify that immune cells act as obstacles and that infection spreads in four directions; also mention that if there are no healthy cells initially, the answer is 0. This shows attention to edge cases and problem constraints.

1. Understand the problem and edge cases

Confirm that infection spreads to adjacent healthy cells (up, down, left, right) each minute, immune cells block spread, and if any healthy cell is unreachable, return -1. Handle cases with no healthy cells (return 0) and no infected cells (return -1 if healthy cells exist).

2. Initialize BFS queue and counters

Add all initially infected cells to a queue and count the total number of healthy cells. Use a variable to track the number of infected healthy cells or the minutes elapsed.

3. Perform multi-source BFS

Process the queue level by level (each level represents one minute). For each infected cell, check its four neighbors; if a neighbor is healthy, infect it, add to queue, and decrement the healthy count. Increment time after each level.

4. Check completion and return result

After BFS, if the healthy count is zero, return the elapsed time; otherwise, return -1 because some healthy cells were unreachable.

Key Points to Mention

  • Multi-source BFS to simulate simultaneous spread from all infected cells.
  • Time complexity O(m*n) where m and n are grid dimensions, as each cell is visited once.
  • Space complexity O(m*n) for the queue in the worst case.
  • Immune cells (-1) act as obstacles and are never infected.
  • Edge cases: no healthy cells (return 0), no infected cells (return -1 if healthy cells exist), and disconnected components.
  • Use of a queue and level-order traversal to track minutes accurately.

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