Red Hat Hiring | Off-Campus 2026 | Number of Islands

Question · Posted Jun 2026

Number of Islands Problem Statement Given a 2D grid consisting of '1's (land) and '0's (water), count the number of islands. An island is formed by connecting adjacent lands horizontally or vertically. You may assume that all four edges of the grid are surrounded by water. Return the total number of islands. Input Format First line contains two integers N and M representing the number of rows and columns. Next N lines contain M space-separated values (0 or 1). Output ...

The full answer & interview discussion are available to premium members.

Log in Create a free account