cswork / libraryOpen practice workspace

Coding questions · DoorDash

Count clusters covered by dashers

Count clusters covered by dashers evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

You are given two m×n binary matrices of equal size: D for dasher availability and U for user presence. A cell value 1 indicates presence; 0 indicates absence. In U, define a cluster as a maximal group of horizontally or vertically adjacent 1-cells (4-neighborhood; diagonal adjacency does not connect). Task: Return the number of clusters in U such that every cell (i, j) in the cluster also has D[i][j] = 1 (i.e., every user in that cluster has a dasher at the same position). Describe your algorithm and analyze time and space complexity. Follow-up: For each qualifying cluster, output the list of its member coordinates (row, col) in a consistent order (e.g., increasing row, then column), and discuss how this affects complexity and memory usage.

What a Strong Answer Covers

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?

Count Clusters Covered by Dashers

You are given two m x n binary matrices of equal size: D for dasher availability and U for user presence. A cell value of 1 indicates presence; 0 indicates absence.

In U, a cluster is a maximal group of horizontally or vertically adjacent 1-cells (4-neighborhood; diagonal adjacency does NOT connect cells).

Return the number of clusters in U such that every cell (i, j) in the cluster also has D[i][j] == 1 — i.e., every user in the cluster has a dasher at the same position.

Write a function countCoveredClusters(D, U) that returns this count.

Example

D = [[1,1],
     [1,1]]
U = [[1,0],
     [0,1]]

There are two clusters in U (the single cell (0,0) and the single cell (1,1)). Both are fully covered by D, so the answer is 2.

Approach. Scan every cell of U. When you find an unvisited user cell, flood-fill (BFS or DFS) its whole connected component using only 4-directional moves, marking each cell visited. While exploring, check whether every member cell also has D == 1. If the entire cluster is covered, increment the count. Each cell is visited once, giving O(m·n) time and O(m·n) space for the visited grid and the traversal frontier.

Follow-up. For each qualifying cluster, you could additionally collect its member coordinates (row, col). Sorting them by increasing row then column adds O(k log k) per cluster of size k (or O(m·n) total if you collect in scan order). Memory grows by the total number of qualifying user cells stored.

Constraints

  • 1 <= m, n (matrices may also be empty, in which case return 0)
  • D and U have identical dimensions m x n
  • Every cell of D and U is 0 or 1
  • Clusters use 4-directional (horizontal/vertical) adjacency only; diagonals do not connect
  • A cluster qualifies only if EVERY one of its cells has D == 1

Hints

  1. Iterate over all cells; when you hit an unvisited user cell (U==1), it is the seed of a new cluster — flood-fill it.
  2. Use BFS or DFS with only the 4 orthogonal neighbors. Mark cells visited as you push them so you never count a cluster twice.
  3. Track a boolean for the current cluster: it stays true only if every visited cell also satisfies D[i][j]==1. Don't break early — you must mark the whole component visited even if it already fails coverage.
  4. Edge cases: empty matrix returns 0; a single user cell with no dasher under it does not qualify.

Solution

Solution Alignment

The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.

Problem Restatement

You are given two m×n binary matrices of equal size: D for dasher availability and U for user presence. A cell value 1 indicates presence; 0 indicates absence. In U, define a cluster as a maximal group of horizontally or vertically adjacent 1-cells (4-neighborhood; diagonal adjacency does not connect). Task: Return the number of clusters in U such that every cell (i, j) in the cluster also has D[i][j] = 1 (i.e., every user in that cluster has a dasher at the same position). Describe your algorithm and analyze time and space complexity. Follow-up: For each qualifying cluster, output the list of its member coordinates (row, col) in a consistent order (e.g., increasing row, then column), and di...

Recommended Approach

Start with a brute-force baseline to confirm correctness, then identify the repeated work or ordering property that enables a better data structure such as a hash map, heap, stack, queue, two pointers, prefix sums, BFS/DFS, or dynamic programming. Write the implementation around a small invariant and test that invariant directly.

Correctness

The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.

Complexity

State the baseline complexity and the optimized complexity. For most interview constraints, justify why the optimized approach meets the expected input size.

Edge Cases and Tests

Empty and singleton inputs, duplicates, ties, invalid inputs, boundary values, and tests that exercise the main invariant.