# Number of Islands in an `n×n` Binary Grid

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/1420/number-islands
Source observed: 2026-11-04T23:11:37.962Z
Last checked: 2026-11-04T23:11:37.962+00:00
Indexed: 2026-11-05T06:40:08.911+00:00

---

# Number of Islands in an `n×n` Binary Grid

We provide this **Medium** Data Structures & Algorithms coding question for interview preparation. The task is to implement `getNumberOfIslands` for a two-dimensional array named `binaryMatrix` containing `0`s and `1`s.

## Problem

Return the number of islands of `1`s in `binaryMatrix`.

An island is a group of adjacent cells whose values are all `1`. Cells are adjacent only when they share the same row or column. Diagonal cells that touch only at a corner belong to different islands.

Explain and code the most efficient solution possible, then analyze its time and space complexity. We provide a Python editor with Output, Test Results, and Console panels; [sign up to run code](https://www.aced.io/signup?src=lesson).

## Example

```text
input: binaryMatrix = [
  [0, 1, 0, 1, 0],
  [0, 0, 1, 1, 1],
  [1, 0, 0, 1, 0],
  [0, 1, 1, 0, 0],
  [1, 0, 1, 0, 1]
]

output: 6
# This is the number of islands in binaryMatrix.
# See all 6 islands color-coded below.
```

![Color-coded example showing the six islands](https://exponent-content.s3.amazonaws.com/img09_1afeaec648.png)

## Interview information

The question is categorized as **Data Structures & Algorithms** and is listed for these roles:

- [Software Engineer](https://www.aced.io/questions?role=swe): 9
- [Machine Learning Engineer](https://www.aced.io/questions?role=ml-engineer): 3
- [Data Scientist](https://www.aced.io/questions?role=data-science)
- [Data Engineer](https://www.aced.io/questions?role=data-engineer)

The rendered page shows 14 interviews and reports that the question was last asked 6 months ago. The page includes Google, Snap, LinkedIn, and Meta interview experiences, with four shared experiences shown below.

### Interview experiences

- **[Google · 6 months ago](https://www.aced.io/experiences/google-software-engineer-interview-1a64d6)** — “DSA is the key. Learning problems will not help but understanding them will. I had 2 rounds online (1 technical, 1 behavioral) and 2 technical rounds onsite. Each technical round had 2 questions and one has to do well in every round. I did well on the online rounds and got stuck on 1 question in one of the onsite round. Ultimately, I took more than expected time for 1 question in my last round which I could have done better.” Software Engineer · Entry Level / L3
- **[Snap · a year ago](https://www.aced.io/experiences/snap-software-engineer-interview-5cfb1a)** — “The process was average. Nothing was really standoutish about the whole process. Just normal SWE interview” Software Engineer · Entry Level / L3
- **[LinkedIn · 2 years ago](https://www.aced.io/experiences/linkedin-machine-learning-engineer-interview-2e79e7)** — “The process was quite rigorous. I went through 7 rounds in total. The interviewers were experienced and helpful and dropped sufficient hints. The feedback was great but I felt like hiring bar was very high and nitpicky.” Machine Learning Engineer · Mid Level / L4
- **[Meta · 2 years ago](https://www.aced.io/experiences/meta-facebook-software-engineer-interview-44d6d3)** — “Focusing on leetcode was sufficient, escpecially the leetcode premium. the hard part was behavioral and needed real examples” Software Engineer · Entry Level / L3

No answers have been contributed by the community yet.

## Related questions

- [Number of palindromic substrings](https://www.aced.io/questions/4625/palindromic-substrings)
- [Determine whether an integer is a power of 2](https://www.aced.io/questions/1689/determine-power-of-two)
- [Number of days between two dates](https://www.aced.io/questions/4645/number-of-days)

## Related courses

### [Machine Learning Engineer Interview Prep](https://www.aced.io/courses/ml-engineer)

![Machine Learning Engineer Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/6UfkHxUyLQVfDHSfLNQtWa/1be1539a65b4f6f15201f5b573c0a578/ML_Engineer_Interview_Prep_Course.jpeg?w=800&h=800&fm=webp&q=75)

We cover mock interviews, frameworks, and advice from senior candidates for machine-learning roles. Topics include ML system design, core concepts, coding, behavioral interviews, and more. The course is designed for roles at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other top companies.

### [Data Science Interview Prep](https://www.aced.io/courses/data-science)

![Data Science Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/3U4C5C0OkDR0UeGch4AmlE/7ad489430b676a088cb973307fe77884/Data_Science_Interview_Prep_Course.jpeg?w=800&h=800&fm=webp&q=75)

We cover statistics, experimentation, coding, SQL, machine learning, behavioral interviews, mock interviews, frameworks, and advice from senior candidates preparing for data-science roles at Google, Amazon, Microsoft, Meta, Apple, and other top companies.

[Browse all questions](https://www.aced.io/questions)

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F1420%2Fnumber-islands&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F1420%2Fnumber-islands). Opening a form does not execute a tool.