Count Four-Neighbor Islands
Updated
Problem statement
Given a rectangular grid of 0 (water) and 1 (land), count its islands.
For this practice, land cells belong to the same island when connected through their top, bottom, left, or right sides. Diagonally touching cells are separate unless a path of side-connected land joins them. Each land cell belongs to exactly one island. Return 0 for an empty grid.
Function
numIslands(grid: int[][]) → intExamples
Example 1
grid = [[1,1,0],[0,1,0],[0,0,1]]return = 2The three connected upper-left land cells form one island, and the lower-right cell forms another.
Example 2
grid = [[1,0],[0,1]]return = 2The two land cells meet only diagonally, so they form two islands.
Example 3
grid = []return = 0There are no land cells in an empty grid.
Constraints
0 <= grid.length <= 50.- When the grid is nonempty, every row has the same length between
1and50. - Every cell is
0or1. - The input grid need not be modified.
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
- Detonate the Maximum BombsZomato / Eternal · PHONE SCREENZomato / Eternal interview questions
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- All Halfway Courses Across Course TracksCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Course Schedule Cycle DetectionGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Evaluate DivisionApple · PHONE SCREENApple interview questions
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
- Find the Unique User with Access to Every CameraVerkada · PHONE SCREENVerkada interview questions