Count Delivery Clusters as New Drop Points Come Online

Last updated: November 29, 2026

Quick Overview

A city grid starts empty, and delivery drop points are activated one cell at a time; adjacent active cells (up, down, left, right) form one cluster. After each activation, return the current number of clusters.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


A city grid starts empty, and delivery drop points are activated one cell at a time; adjacent active cells (up, down, left, right) form one cluster. After each activation, return the current number of clusters.

This is an online connectivity question framed around Amazon Logistics planning delivery zones as new Hub and Locker points open. Re-running a flood fill after each update is O(k * rows * cols); the interviewer wants an incremental union find.

What the Interviewer Expects
  • Explain why repeated BFS is too slow
  • Add each cell as a new component then union with active neighbors
  • Decrement the count only on successful unions
  • Handle duplicate activations
  • O(k alpha(k)) time using only activated cells
Key Topics to Cover
union find
online connectivity
grid
incremental updates
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if drop points can also be deactivated?
  • How would you report the size of the largest cluster after each step?
  • What if the grid is unbounded?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: rows, cols, and a list of (r, c) activations. Output: a list of cluster counts after each activation. Duplicate activations are allowed and change nothing....

High-Level Design

Keep a DSU only over active cells, stored in a dictionary so memory scales with activations, not the grid. When a cell turns on:

  1. If already active, append the current count.
  2. Otherwise add it as...

Submit Your Answer
Markdown supported