FastPrepNumber of Islands
Problems/Number of Islands

Number of Islands

Goldman Sachs logoGoldman Sachs● MediumFULLTIMEOAPHONE SCREENONSITE INTERVIEW

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Three Goldman Sachs reports identify Number of Islands or the same four-directional connected-component grid task across an online assessment, CoderPad screen, and superday. The reports omit a shared portable interface and complete constraints; this practice version is an approximately 98% match.

Given an array of equal-length strings grid, where '1' represents land and '0' represents water, return the number of islands.

An island is a maximal group of land cells connected horizontally or vertically. Cells outside the matrix are water.

Function

numIslands(grid: String[]) → int

Examples

Example 1

grid = ["11110","11010","11000","00000"]return = 1
grid
01234011110111010211000300000

All land cells belong to one orthogonally connected component.

Example 2

grid = ["11000","11000","00100","00011"]return = 3
grid
01234011000111000200100300011

The upper-left block, center cell, and lower-right pair are separate islands.

Example 3

grid = ["000","000"]return = 0
grid
01200001000

The grid contains no land cells.

Constraints

  • 1 <= grid.length <= 300.
  • 1 <= grid[i].length <= 300, and every row has the same length.
  • Every character is '0' or '1'.

More Goldman Sachs Phone Screen questions

  • House Robber IIIPHONE SCREEN · Seen Nov 2026
  • Trapping Rain WaterPHONE SCREEN · Seen Nov 2026
  • Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Check a Complete Binary TreePHONE SCREEN · Seen Oct 2026
  • Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Count Staircase WaysPHONE SCREEN · Seen Oct 2026
  • Is a Power of TenPHONE SCREEN · Seen Oct 2026
  • Minimum Traveling Salesperson TourPHONE SCREEN · Seen Oct 2026
  • Square Root with Newton IterationPHONE SCREEN · Seen Oct 2026
  • First Non-Repeating CharacterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
  • Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
  • Count Connected IslandsUber · PHONE SCREENUber interview questions
  • Minimum Cleaning Robot RunsHSBC · OAHSBC interview questions
  • Number of IslandsSpaceX · PHONE SCREENSpaceX interview questions
  • Mouse and Cheese ReachabilityMeta · ONSITE INTERVIEWMeta interview questions
  • Count Islands with DFS and BFSVisa · OAVisa interview questions
  • Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
  • Number of IslandsTripjack · ONSITE INTERVIEWTripjack interview questions
All Goldman Sachs interview questionsSee Goldman Sachs hiring insights
Saved
class Solution {
    public int numIslands(String[] grid) {
        // Write your code here.
    }
}
Checking account…