FastPrepNumber of Islands
Problems/Number of Islands

Number of Islands

Tripjack logoTripjack● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This comes from an in-person Tripjack interview for an SDE-1 role. It was the only coding question in the second technical round, a short data structures and algorithms round. The report gives only the well-known name of the problem.

The report gives no limits or examples, so this version uses the usual rules: land cells join only up, down, left or right, and you count the groups. The map has up to 300 rows and 300 columns, and each row is a string of 0s and 1s.

Our version is about 95% the same as the reported question.

You are given a map as an array of strings grid. Each string is one row, and every row has the same length. The character '1' is land and '0' is water.

An island is a group of land cells joined up, down, left or right. Two land cells that touch only at a corner are not joined. Everything outside the map is water.

Return the number of islands.

Function

numIslands(grid: String[]) → int

Examples

Example 1

grid = ["1110","1010","1110","0001"]return = 2
grid
012301110110102111030001

The eight land cells in the top-left 3 by 3 area form a ring around one water cell. Each of them touches the next one up, down, left or right, so the ring is one island.

The land cell in the bottom-right corner touches the ring only at a corner, so it is a second island. The answer is 2.

Example 2

grid = ["101","010","101"]return = 5
grid
012010110102101

The five land cells touch each other only at corners. None of them is joined to another, so each one is its own island.

Example 3

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

There is no land, so there are no islands.

Constraints

  • 1 <= grid.length <= 300
  • 1 <= grid[i].length <= 300
  • All rows have the same length.
  • Every character is '0' or '1'.

More Tripjack Onsite Interview questions

  • Reconstruct ItineraryONSITE INTERVIEW · Seen Nov 2026
  • Wildcard MatchingONSITE INTERVIEW · Seen Nov 2026
  • Jump Game IIONSITE INTERVIEW · Seen Aug 2026
  • Zigzag ConversionONSITE INTERVIEW · Seen Aug 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
  • Mouse and Cheese ReachabilityMeta · ONSITE INTERVIEWMeta interview questions
  • Number of IslandsTekion · ONSITE INTERVIEWTekion interview questions
  • Grid Pathfinding with Obstacles (DFS)Roblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • Number of IslandsGoldman Sachs · OA · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
  • Number of IslandsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Number of IslandsCrowdstrike · ONSITE INTERVIEWCrowdstrike interview questions
  • Max Area of IslandRoku · ONSITE INTERVIEWRoku interview questions
All Tripjack interview questionsSee Tripjack hiring insights
Saved
class Solution {
    public int numIslands(String[] grid) {
        // Write your code here.
    }
}
Checking account…