Practice: Number of Islands
Problem: https://leetcode.com/problems/number-of-islands/
Recognition reminder: the grid is a graph in disguise. Each "1" cell is a node, and two land cells are adjacent when they touch up, down, left, or right (not diagonally). “How many separate islands” is “how many connected components”. You count components by walking the grid; each time you find unvisited land, you flood that whole island and add one.
Signature note
Write your solution as a function num_islands(grid: list[list[str]]) -> int. The grid holds the strings "1" (land) and "0" (water), not integers. The provided test passes a fresh copy of each grid on every call, because the standard flood-fill solution marks visited land by overwriting it (it mutates the grid); the test does not assume whether you mutate or copy, but it never reuses a grid you might have changed.
Before you start (the five-beat rhythm)
- Your Pattern Card for this week is already written.
- Name the pattern aloud and write your approach as a plain-English comment before any code.
- Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
- If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
- Debrief in your commit message before moving to the next problem.
Your target
Fill these in yourself before you look at anyone else’s solution:
Target time complexity: ____
Target space complexity: ____
Think in terms of how many cells you visit and how many times you visit each one. The grid has rows times columns cells; a correct flood-fill visits each once.
Where your code goes
Write your solution in your own work repo (see getting-started.md), not in this folder. This folder ships only the problem spec and a provided-example test (tests/test_provided.py) so you can check the given cases locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.
Debrief (paste into your commit message)
1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?