Medium · Graphs · spoken drill
Free AI LeetCode Communication Coach — Number of Islands
Treat the grid as a graph; each unvisited land starts a DFS/BFS that sinks the island.
The problem
Given a 2D grid of '1' (land) and '0' (water), return the number of islands. Islands are 4-directionally connected.
Example: grid with two separate land blobs → 2
Scan + flood fill.
def numIslands(grid):
if not grid: return 0
rows, cols = len(grid), len(grid[0])
def dfs(r, c):
if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] != "1":
return
grid[r][c] = "0"
for dr, dc in ((1,0),(-1,0),(0,1),(0,-1)):
dfs(r+dr, c+dc)
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == "1":
count += 1
dfs(r, c)
return countHow to explain it
1. Restate
Say the problem in your own words.
One or two sentences. Show you understood the input, the output, and the goal — not that you memorised the prompt.
2. Approach
Name the method before you code.
Brute force first if you need it, then the structure you will use: hash map, two pointers, stack, binary search.
3. Example
Walk one concrete input.
Pick small numbers. Say what you store, what you compare, and what you return. Interviewers follow an example more easily than abstract talk.
4. Time and space
One sentence each.
After the example, before you claim you are done. “Time is O(n) because we scan once. Space is O(n) for the map.”
5. Edge cases
Name at least one unusual input.
Empty input, duplicates, already sorted, overflow. Invite a follow-up: “I would also check …”
- “Each unvisited land starts one island.”
- “I flood-fill to mark the whole component.”
Practise out loud
3 free scored runs left this hour.
Problem · Medium
Given a 2D grid of '1' (land) and '0' (water), return the number of islands. Islands are 4-directionally connected.
Example: grid with two separate land blobs → 2
- 1Restate — Say the problem in your own words.
- 2Approach — Name the method before you code.
- 3Example — Walk one concrete input.
- 4Time and space — One sentence each.
- 5Edge cases — Name at least one unusual input.
Hit record. Short countdown, then 60–90 seconds. We score the five-step script — never pronunciation.
Recording needs Chrome or Edge. You can still type below.
Model spoken script (~75s)
1. Restate
I need how many connected land components are in the grid, 4-directional.
2. Approach
I scan every cell. When I find land, I increment the count and DFS or BFS to mark the whole island as visited water.
3. Example
If two land blobs are separated by water, the first flood fill marks one island, the second scan finds the other — count is two.
4. Time and space
Time is O(m n). Space is O(m n) in the worst case for the call stack or queue.
5. Edge cases
I would check an all-water grid and a single land cell.
How to explain Number of Islands out loud, step by step
In one sentence: Given a grid of land and water cells, you have to count how many separate patches of land there are, where land connects only up, down, left and right.
What to say, in order
- “I'll treat the grid as a graph: each land cell is a node, connected to its land neighbours in four directions.”
- “So the question becomes: how many connected components of land are there?”
- “I scan every cell, row by row. When I find a land cell I haven't visited, I've found a new island, so I add one to the count.”
- “Then I flood-fill from that cell with DFS or BFS, marking every connected land cell as visited so I don't count it again.”
- “Marking can be done by setting the cell to water, if I'm allowed to modify the input, or with a separate visited set.”
- “When the scan finishes, the count is the answer.”
Key English phrases for Number of Islands
- connected component
- — a group of cells all reachable from each other
- flood fill
- — spreading from one cell to mark the whole island
- four-directional neighbours
- — up, down, left and right — not diagonals
- mark as visited
- — record that a cell has already been counted
- out of bounds
- — a neighbour index outside the grid
How to say the time and space complexity
“Time is O(m times n), where m is the number of rows and n the number of columns, because each cell is visited a constant number of times. Space is O(m times n) in the worst case, for the recursion stack or the queue when the whole grid is one island.”
Define m and n first. Don't say O(n squared) unless the grid is square and you said so.
Communication mistakes on Number of Islands
- Mistake: Saying 'I count all the ones' — that counts land cells, not islands. Instead: Say 'I count how many times I start a new flood fill.'
- Mistake: Silently mutating the input grid. Instead: Ask: 'Is it okay to modify the grid, or should I keep a visited set?'
- Mistake: Forgetting to mention bounds checks. Instead: Say 'Before I visit a neighbour I check it's inside the grid and is land.'
- Mistake: Saying the space is O(1) because the grid is reused. Instead: The recursion stack or queue can still grow to the size of the grid.
Likely follow-up questions
Interviewer: “What if the grid is huge and recursion might overflow?”
You: “I'd use BFS with an explicit queue, or an iterative DFS with my own stack. Same O(m times n) time, but no deep call stack.”
Interviewer: “What if land cells are added one at a time and you need the count after each addition?”
You: “I'd use union-find. Each new land cell starts as its own island; I union it with any land neighbours, and every successful union reduces the count by one. With path compression and union by rank, each operation is nearly constant.”
Common mistakes when explaining Number of Islands
- Forgetting to mark visited land (infinite recursion).
- Using 8 directions when the prompt says 4.
Other problems
Easy · Hash map
Two Sum
Classic hash-map scan. Name brute force, then the map, walk [2,7,11,15] target 9, finish with O(n).
Medium · Hash map
Group Anagrams
Group strings by anagram signature — sorted key or 26-letter count — then walk one bucket.
Easy · Two pointers
Valid Palindrome
Two pointers from both ends; skip junk characters; compare lowercase letters.
Medium · Two pointers
3Sum
Sort, fix i, then two pointers for the pair that sums to −nums[i]. Deduplicate out loud.
Medium · Sliding window
Longest Substring Without Repeating Characters
Grow a window; when a duplicate appears, move the left edge past its last index.
Easy · Stack
Valid Parentheses
Stack story: push openers, pop on matching closer, empty stack at the end.
FAQ
Questions
More questions? Email us at contact@mocklyenglish.com.
Course: How to explain a LeetCode solution · Think out loud · Explain code out loud