Stage 1 of 5: Nobody healthy left
Background. A rectangular grid holds people and empty cells. An illness passes from infected people to the healthy people next to them, one step a day.
Implement spread_days(grid) in outbreak.py.
grid is a list of strings of equal length, one string per row. Each character is a cell:
.an empty cellHa healthy personIan infected person
The grid as given is day 0. On each day after that, every healthy person who had at least one infected neighbour at the end of the previous day becomes infected. Everyone changes at once, so a person infected on some day infects nobody until the next day. A cell's neighbours are the cells directly above, below, left and right of it, never the diagonals, and the illness does not cross empty cells.
Return the first day at the end of which nobody is healthy: 0 if nobody is healthy on day 0, and -1 if someone stays healthy forever.
Example
H.IH H.II H.II H.II I.II
HHH. -> HHI. -> HII. -> III. -> III.
.HHH .HHH .HIH .III .III
day 0 day 1 day 2 day 3 day 4
spread_days(["H.IH", "HHH.", ".HHH"]) returns 4.
Notes
- A grid has 1 to 10 rows and 1 to 10 columns.
- The interview goes on from here, and finishing every step in the hour is not expected.
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
- Editable:
outbreak.py - Read-only: all
tests/test_*.pyfiles - Neighbours are the four edge-adjacent cells: up, down, left, right
What comes after stage 1
The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.
- Stage 2: Immune people. Some people can never catch it, so not everyone can be reached. How long does the outbreak last?
- Stage 3: Recovery. Infected people get better after a while. When does the grid stop changing?
- Stage 4: Countdowns to death. Some cases are fatal, depending on how many sick neighbours a person has. How long does it last, and how many die?
- Stage 5: Burn one line. Before the first day, one row or column can be sacrificed. Which one costs the fewest lives?
The files you get
- Yours to edit: outbreak.py.
- Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.
The round: no AI, an assistant that only answers
OpenAI runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.
Frequently asked, briefly answered
Is Infection Spread free?
Stage 1 is free with an account. The other 4 stages need Nyrion Pro.
Which company asks something like this?
OpenAI. It is modelled on OpenAI's coding round, which runs without AI. Based on candidates' reports of OpenAI interviews. Nyrion wrote this version, so the question you get may differ.
Do I bring my own AI tool?
No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.
What does the interviewer ask?
5 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.
Start the problem
Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the first stage of this problem is free.
More like it: every practice problem, OpenAI's interviews, the AI coding interview guide.