Perimeter of Every Island
Updated
Problem statement
Given a rectangular binary matrix grid, return the perimeter of every island. A cell containing 1 is a unit-square land cell; 0 is water.
An island is a maximal group of land cells connected horizontally or vertically through shared edges. Diagonal contact does not connect islands.
An island’s perimeter is the number of unit edges next to water or outside the grid. Edges around enclosed water holes also count; edges shared by two land cells do not.
Return one integer per island in row-major discovery order: scan rows from top to bottom and columns from left to right, and append an island’s perimeter when its first land cell is encountered. Keep separate entries for islands with equal perimeters.
If the grid has no rows, no columns or no land cells, return an empty array.
Function
islandPerimeters(grid: int[][]) → int[]Examples
Example 1
grid = [[1,1,0,0,1],[1,0,0,0,1],[0,0,1,1,0],[1,0,0,1,0]]return = [8,6,8,4]The islands first appear at (0,0), (0,4), (2,2) and (3,0). Their perimeters are 8, 6, 8 and 4; equal perimeters remain separate results.
Example 2
grid = [[1,1,1],[1,0,1],[1,1,1]]return = [16]The eight land cells form one island. Its outer boundary has length 12 and the central water hole adds 4, for a perimeter of 16.
Example 3
grid = []return = []There are no cells and no islands, so the result is an empty array.
Constraints
0 <= grid.length <= 32.- Every row has the same length, between
0and32inclusive. - Every cell is
0or1. - Only horizontal and vertical shared edges connect land cells.
- Inputs satisfy these rules; ragged rows and other cell values are outside the accepted domain.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Word SearchBloomberg LP · OA · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Count Groups of Diseased PlantsYellow.ai · PHONE SCREEN
- Count Enclosed Freshwater RegionsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Count Starts Reaching a Grid MaximumDuolingo · PHONE SCREENDuolingo interview questions
- Count Connected IslandsUber · PHONE SCREENUber interview questions
- Island Area and Water BoundaryWaymo · PHONE SCREENWaymo interview questions
- Number of IslandsSpaceX · PHONE SCREENSpaceX interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions