Skip to main content
All Questions

Given an nxn grid of 1s and 0s, return the number of islands in the input.

Medium

Given a 2D array binaryMatrix of 0s and 1s, implement a function getNumberOfIslands that returns the number of islands of 1s in binaryMatrix.

An island is defined as a group of adjacent values that are all 1s. A cell in binaryMatrix is considered adjacent to another cell if they are next to each either on the same row or column. Note that two values of 1 are not part of the same island if they’re sharing only a mutual "corner" (i.e. they are diagonally neighbors).

Explain and code the most efficient solution possible and analyze its time and space complexities.

Example

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 # since this is the number of islands in binaryMatrix. # See all 6 islands color-coded below.

img09

Interview experiences

5 shared
Mindbody · 3 months ago
"I interviewed for an SD3 role at Mindbody, and the process felt pretty standard structurally, just long. They told me it was around six rounds, and a lot of it was centered on DSA and design. The most memorable part was a PR review round on an order-messaging flow where I had to find bugs, make it easier to follow, and explain how I'd keep it extensible if the messaging vendor changed later. AI was not a side topic either, because it kept showing up in different rounds. By the end, it felt like they were testing both the usual engineering fundamentals and whether I could actually use AI in a practical way."
Software Engineer · Senior / L5
Google · 7 months ago
"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
"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
"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
"Focusing on leetcode was sufficient, escpecially the leetcode premium. the hard part was behavioral and needed real examples"
Software Engineer · Entry Level / L3

Related courses

Course

Machine Learning Engineer Interview Prep

Land your dream machine learning role at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Explore ML system design, core concepts, coding, behavioral interviews, and more.

Course

Data Science Interview Prep

Land your dream data science role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Practice statistics, experimentation, coding, SQL, machine learning, behavioral interviews, and more.