← Two Sigma Interview Insights
My first instinct was to just count connected components, which is obviously wrong.
Use DFS/BFS to find each island and compute a canonical representation of its shape by normalizing the coordinates relative to the island's top-left bounding box. Store these canonical forms in a set to count distinct shapes. This approach efficiently handles translation invariance and avoids explicit rotation/reflection checks.
Pro tip: Emphasize that the canonical representation must be invariant to translation but not rotation/reflection, and discuss how to handle large grids by using a set for O(1) lookups. Mention that the time complexity is O(R*C) and space O(R*C) in the worst case.
Iterate through each cell; when encountering an unvisited '1', start a DFS/BFS to explore the entire island.
During traversal, record the relative coordinates of each cell in the island (e.g., row and column offsets from the starting cell).
Compute the minimum row and column among the coordinates and subtract them to shift the island to the origin, ensuring translation invariance.
Convert the normalized coordinates into a hashable form (e.g., a sorted tuple of coordinates or a string) and add it to a set of distinct shapes.
After processing all islands, the size of the set gives the number of distinct island shapes.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.