Count Distinct Shapes of High-Demand Hotspot Clusters

Last updated: November 29, 2026

Quick Overview

A city demand grid marks high-demand cells with 1. A hotspot cluster is a 4-directionally connected group of 1s. Return how many distinct cluster shapes exist, where two clusters have the same shape if one is a translation of the other.

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

0

0

0 solved


A city demand grid marks high-demand cells with 1. A hotspot cluster is a 4-directionally connected group of 1s. Return how many distinct cluster shapes exist, where two clusters have the same shape if one is a translation of the other.

An Uber onsite grid traversal problem framed around recurring demand patterns (airport queues, stadium exits). The interviewer checks flood fill plus a canonical shape signature that is translation invariant.

What the Interviewer Expects
  • Finds clusters with BFS or iterative DFS
  • Normalizes cell coordinates relative to an anchor
  • Builds a hashable canonical signature
  • Avoids recursion depth issues on large grids
  • Explains why the traversal order must not affect the signature
Key Topics to Cover
Flood fill
BFS
Hashing
Matrix
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
  • How would you treat rotations and reflections as the same shape?
  • How would you count how many times each shape appears?
  • How would you do this on a streaming grid that updates every minute?
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: list of lists of 0/1. Output: number of distinct translation-equivalent shapes. Grid up to 500 x 500, so recursion could overflow Python's stack; use an explicit stack or queue....

High-Level Design

Scan cells; on an unvisited 1, flood fill its cluster with BFS, marking cells visited. Collect cells as offsets from the cluster's minimum row and minimum column, sort the offsets, and store the tuple...


Submit Your Answer
Markdown supported