Number of Distinct Islands
Updated
Problem statement
In an ocean, there are islands marked by 1. Water is represented by 0.
Determine how many unique shapes (no rotation or mirror) among these islands.
Islands are connected 4-directionally.
Function
numDistinctIslands(grid: int[][]) → int
Complete the function numDistinctIslands in the editor.
numDistinctIslands has the following parameter:
int[][] grid: a 2D array of integers representing the ocean
Returns
int: the number of unique island shapes
Examples
Example 1
grid = [[1, 1, 1, 1, 0, 0], [1, 1, 0, 0, 0, 1], [0, 0, 1, 1, 0, 1], [1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1], [1, 0, 1, 1, 0, 0]]return = 4There are 4 unique island shapes:
- the 2 6-sized islands,
- the 2 2-sized islands,
- the 1 2-sized island,
- the 1 1-sized island.
Standard BFS/DFS problem with a translation of the starting point to the origin of each island. The solution is done correctly and optimally with O(n) complexity.
Constraints
1 ≤ grid.length, grid[i].length ≤ 500grid[i][j]is0or1.- Cells are connected only in the four cardinal directions.
- Shapes are equal only under translation; rotations and reflections are distinct.
More Bloomberg LP Onsite Interview questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Currency Conversion RatePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Query Whether an Island Size ExistsMeta · ONSITE INTERVIEWMeta interview questions
- Number of Distinct Islands (For L5 :)Google · PHONE SCREENGoogle interview questions
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- User Journey Paths from a Log StreamWhatNot · PHONE SCREENWhatNot interview questions
- Package Install OrderVerily · ONSITE INTERVIEW
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Shortest Bridge Between Two IslandsCompass International Holdings · ONSITE INTERVIEWCompass International Holdings interview questions
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)