PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Uber
  • Coding & Algorithms
  • Machine Learning Engineer

Count connected delivery zones

Company: Uber

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

Given an m x n grid representing a service area, each cell is either 'Z' (deliverable zone) or '#' (blocked). Two 'Z' cells belong to the same zone if they are 4-directionally adjacent. Implement countZones(grid: List[List[char]]) -> int that returns the number of connected zones. Follow-up: return the sizes of all zones in descending order. Analyze time and space complexity, and then optimize for minimal extra space.

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

You are given an `m x n` grid representing a service area. Each cell is either `'Z'` (a deliverable zone) or `'#'` (blocked). Two `'Z'` cells belong to the same zone if they are 4-directionally adjacent (up, down, left, right). Implement `countZones(grid)` that returns the number of connected zones (connected components of `'Z'` cells). This is the classic "Number of Islands" problem framed as a delivery service area. To minimize extra space, flip each visited `'Z'` to `'#'` in place instead of using a separate visited set. Example: ``` grid = [['Z','Z','#','#','#'], ['Z','Z','#','#','#'], ['#','#','Z','#','#'], ['#','#','#','Z','Z']] countZones(grid) -> 3 ``` There are three zones: the 2x2 block in the top-left, the single cell in the middle, and the pair in the bottom-right.

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

  1. This is 'Number of Islands' with 'Z' as land and '#' as water.
  2. Scan every cell; when you hit an unvisited 'Z', increment the count and flood-fill the whole component.
  3. 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

Follow-up to 'Count connected delivery zones'. Given the same `m x n` grid of `'Z'` (deliverable zone) and `'#'` (blocked) cells, implement `zoneSizes(grid)` that returns a list of the sizes of all connected zones (4-directionally connected components of `'Z'` cells), sorted in descending order. Example: ``` grid = [['Z','Z','#','#','#'], ['Z','Z','#','#','#'], ['#','#','Z','#','#'], ['#','#','#','Z','Z']] zoneSizes(grid) -> [4, 2, 1] ``` The top-left block has 4 cells, the bottom-right pair has 2, and the middle cell has 1.

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

  1. Reuse the flood-fill from countZones, but count the cells in each component instead of just incrementing a component counter.
  2. Collect each component's size as you finish flooding it.
  3. Sort the collected sizes in descending order before returning.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Deep Equality of Two Records - Uber (medium)
  • Shortest Path in a Grid with Blocked Cells - Uber (medium)
  • Design and Implement an LRU Cache - Uber (medium)
  • Reconstruct the Alphabet Order of an Alien Language - Uber (medium)
  • Maximize Throughput and Count Trigger Components - Uber (medium)