FreeCareerPath
Build the PrimitiveMedium

Number of Islands

Write `num_islands(grid)` counting connected groups of `"1"` cells in a grid of `"1"` and `"0"`. Cells connect up, down, left and right — not diagonally. Scan for an unvisited land cell, count it, then flood the whole island so it is never counted again. Marking visited by overwriting the grid is O(1) space; a separate visited set is fine too. Worth saying out loud: a recursive flood on a 300x300 all-land grid can blow the stack, so BFS with an explicit queue is the safer choice.

What to expect: A timer starts when you begin. Edit the starter code, run it against the test suite as many times as you like, then finish when you're done. The reference solution and interviewer follow-up questions unlock only after you finish.

Log in to start this session

Timed sessions and your results for "Number of Islands" are saved to your account — logging in takes a few seconds.

Number of Islands — FreeCareerPath