Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a function to calculate the number of islands in a 2D grid where 1 represents land and 0 represents water.

Hard

Problem Statement

Write a function to calculate the number of islands in a 2D grid where 1 represents land and 0 represents water.

Sample Input

[
 [1, 1, 0, 0, 0],
 [1, 1, 0, 0, 0],
 [0, 0, 1, 0, 0],
 [0, 0, 0, 1, 1]
 ]

Sample Output

Number of Islands: 3

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
function numIslands(grid: number[][]): number { // Your logic here }
Log in or sign up to run or submit your code.Log inSign up
Input
[
 [1, 1, 0, 0, 0],
 [1, 1, 0, 0, 0],
 [0, 0, 1, 0, 0],
 [0, 0, 0, 1, 1]
 ]
Expected Output
Number of Islands: 3