Count connected delivery zones
Company: Uber
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates graph traversal and connected-components detection on grids, measuring competence in grid-based algorithms, neighbor connectivity, and algorithmic analysis of time and space complexity.
Count connected delivery zones
Constraints
- 1 <= m, n; the grid may be empty (return 0).
- Each cell is either 'Z' or '#'.
- Connectivity is 4-directional only (no diagonals).
- The input grid may be mutated in place by the reference solution.
Examples
Input: ([['Z','Z','#','#','#'],['Z','Z','#','#','#'],['#','#','Z','#','#'],['#','#','#','Z','Z']],)
Expected Output: 3
Explanation: Three components: the top-left 2x2 block, the single middle cell, and the bottom-right pair.
Input: ([['Z','Z','Z','Z'],['Z','#','#','Z'],['Z','Z','Z','Z']],)
Expected Output: 1
Explanation: A single ring-shaped component surrounding two blocked cells is still one connected zone.
Input: ([],)
Expected Output: 0
Explanation: Empty grid has no zones.
Input: ([['#','#'],['#','#']],)
Expected Output: 0
Explanation: All cells blocked, so there are zero zones.
Input: ([['Z']],)
Expected Output: 1
Explanation: A single 'Z' cell is one zone.
Input: ([['Z','#','Z','#','Z']],)
Expected Output: 3
Explanation: Three isolated 'Z' cells separated by blocked cells form three zones.
Hints
- This is 'Number of Islands' with 'Z' as land and '#' as water.
- Scan every cell; when you hit an unvisited 'Z', increment the count and flood-fill the whole component.
- Instead of a visited set, overwrite each visited 'Z' with '#' to use O(1) extra marking space — that is the minimal-space optimization the follow-up asks for.
Delivery zone sizes in descending order
Constraints
- 1 <= m, n; the grid may be empty (return []).
- Each cell is either 'Z' or '#'.
- Connectivity is 4-directional only (no diagonals).
- The result must be sorted in descending order by size.
- The input grid may be mutated in place by the reference solution.
Examples
Input: ([['Z','Z','#','#','#'],['Z','Z','#','#','#'],['#','#','Z','#','#'],['#','#','#','Z','Z']],)
Expected Output: [4, 2, 1]
Explanation: Sizes 4 (top-left block), 2 (bottom-right pair), and 1 (middle cell), sorted descending.
Input: ([['Z','Z','Z','Z'],['Z','#','#','Z'],['Z','Z','Z','Z']],)
Expected Output: [10]
Explanation: One connected ring-shaped zone of 10 cells.
Input: ([],)
Expected Output: []
Explanation: Empty grid has no zones.
Input: ([['#','#'],['#','#']],)
Expected Output: []
Explanation: All cells blocked, so there are no zones.
Input: ([['Z']],)
Expected Output: [1]
Explanation: A single 'Z' cell is one zone of size 1.
Input: ([['Z','#','Z','#','Z']],)
Expected Output: [1, 1, 1]
Explanation: Three isolated single-cell zones.
Hints
- Reuse the flood-fill from countZones, but count the cells in each component instead of just incrementing a component counter.
- Collect each component's size as you finish flooding it.
- Sort the collected sizes in descending order before returning.