Count clusters covered by dashers
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: 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.
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
Examples
Input: ([[1,1],[1,1]], [[1,0],[0,1]])
Expected Output: 2
Explanation: Two single-cell clusters at (0,0) and (1,1); both have D==1, so both qualify.
Input: ([[1,0],[1,1]], [[1,1],[1,1]])
Expected Output: 0
Explanation: All four user cells form one cluster, but D[0][1]==0, so the cluster is not fully covered.
Input: ([[1,1,1],[1,0,1],[1,1,1]], [[1,1,0],[0,0,0],[0,1,1]])
Expected Output: 2
Explanation: Cluster {(0,0),(0,1)} is covered (D both 1) and cluster {(2,1),(2,2)} is covered (D both 1); both qualify.
Input: ([[1,1],[1,1]], [[1,1],[1,1]])
Expected Output: 1
Explanation: All user cells form a single connected cluster fully covered by dashers.
Input: ([], [])
Expected Output: 0
Explanation: Empty matrices contain no clusters.
Input: ([[0]], [[1]])
Expected Output: 0
Explanation: The lone user cell has no dasher (D==0), so it does not qualify.
Input: ([[1]], [[1]])
Expected Output: 1
Explanation: A single covered user cell forms one qualifying cluster.
Input: ([[1,0,1],[0,0,0],[1,0,1]], [[1,1,1],[0,0,0],[1,0,1]])
Expected Output: 2
Explanation: Top-row cluster fails (D[0][1]==0), but isolated cells (2,0) and (2,2) each qualify (D==1).
Input: ([[1,1,1,1]], [[1,0,1,1]])
Expected Output: 2
Explanation: Two clusters in the row: {(0,0)} and {(0,2),(0,3)} (the 0 at column 1 separates them); both are fully covered.
Hints
- Iterate over all cells; when you hit an unvisited user cell (U==1), it is the seed of a new cluster — flood-fill it.
- Use BFS or DFS with only the 4 orthogonal neighbors. Mark cells visited as you push them so you never count a cluster twice.
- 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.
- Edge cases: empty matrix returns 0; a single user cell with no dasher under it does not qualify.