Question bank

How do you count the number of distinct islands in a non-empty 2D array of 0's and 1's, where islands are defined as groups of connected 1's (land) that can be translated but not rotated or reflected?

January 6, 2025Updated March 31, 20263 min read
MediumCodingData AnalysisProblem-SolvingCritical ThinkingData ScientistSoftware Engineer
How do you count the number of distinct islands in a non-empty 2D array of 0's and 1's, where islands are defined as groups of connected 1's (land) that can be translated but not rotated or reflected?

Approach When tasked with counting the number of distinct islands in a 2D array of 0's and 1's, a systematic approach is essential. Here’s a structured framework to tackle this problem: Understand the Problem : Clearly define what constitutes an island. An…

Approach

When tasked with counting the number of distinct islands in a 2D array of 0's and 1's, a systematic approach is essential. Here’s a structured framework to tackle this problem:

  1. Understand the Problem: Clearly define what constitutes an island. An island is a group of connected 1's (land) that can be translated but cannot be rotated or reflected.
  2. Data Structure Choice: Choose appropriate data structures to hold the array and possibly the unique shapes of the islands.
  3. Traversal Method: Decide on a method for traversing the 2D array, such as Depth-First Search (DFS) or Breadth-First Search (BFS).
  4. Shape Normalization: Normalize the shape of each island to ensure that different translations of the same island are counted as one.
  5. Store Unique Shapes: Use a set to keep track of unique island shapes.
  6. Count and Return: Finally, return the count of unique shapes stored in the set.

Key Points

  • Definition of Island: Islands are groups of adjacent 1's connected vertically or horizontally.
  • Shape Normalization: Essential for ensuring that different translations of the same island are recognized as identical.
  • Data Structures: Utilize sets for storing unique shapes to leverage their properties for quick look-up and insertion.
  • Traversal Techniques: Familiarize yourself with both DFS and BFS approaches to navigate through the matrix.

Standard Response

Here’s a comprehensive sample answer detailing the process of counting distinct islands in a 2D array of 0's and 1's:

def numDistinctIslands(grid):
 if not grid:
 return 0
 
 visited = set()
 unique_islands = set()

 def dfs(r, c, origin_r, origin_c, shape):
 if (r < 0 or r >= len(grid) or c < 0 or c >= len(grid[0]) or 
 grid[r][c] == 0 or (r, c) in visited):
 return
 
 visited.add((r, c))
 shape.append((r - origin_r, c - origin_c)) # Normalize shape

 dfs(r + 1, c, origin_r, origin_c, shape) # down
 dfs(r - 1, c, origin_r, origin_c, shape) # up
 dfs(r, c + 1, origin_r, origin_c, shape) # right
 dfs(r, c - 1, origin_r, origin_c, shape) # left

 for r in range(len(grid)):
 for c in range(len(grid[0])):
 if grid[r][c] == 1 and (r, c) not in visited:
 shape = []
 dfs(r, c, r, c, shape) # Start DFS
 unique_islands.add(tuple(shape)) # Store as tuple to add to set

 return len(unique_islands)

Tips & Variations

Common Mistakes to Avoid

  • Ignoring Edge Cases: Always consider edge cases, such as completely empty grids.
  • Failing to Normalize Shapes: If shapes are not normalized, distinct islands may be incorrectly counted.
  • Using Mutable Data Structures: When adding shapes to sets, ensure they are immutable (e.g., convert lists to tuples).

Alternative Ways to Answer

  • BFS Approach: Instead of DFS, a BFS approach can also be implemented, especially in languages or environments where recursion depth is a concern.
VA

Verve AI Editorial Team

Question Bank

Related reads

Explore More Question Bank Entries

How would you boost team motivation and engagement if they are struggling?
February 9, 2025Medium

How would you boost team motivation and engagement if they are struggling?

Approach To effectively answer the question, "How would you boost team motivation and engagement if they are struggling?", follow this structured framework: Acknowledge the Situation : Recognize the current state of the team and the factors contributing to…

Read answer guide
Which brands do you follow on social media, and what influences your choices?
January 25, 2025Easy

Which brands do you follow on social media, and what influences your choices?

Approach When answering the question, "Which brands do you follow on social media, and what influences your choices?" it's essential to create a structured framework that showcases your understanding of branding, personal values, and engagement with the…

Read answer guide
Can you share an example of a situation where you chose to break a rule, and explain your reasoning?
February 15, 2025Medium

Can you share an example of a situation where you chose to break a rule, and explain your reasoning?

Approach When answering the interview question, "Can you share an example of a situation where you chose to break a rule, and explain your reasoning?", it’s essential to follow a structured framework. This allows you to present your thoughts clearly and…

Read answer guide
How do you incorporate user feedback in your role as a Technical Product Manager?
January 5, 2025Medium

How do you incorporate user feedback in your role as a Technical Product Manager?

Approach When answering the question, "How do you incorporate user feedback in your role as a Technical Product Manager?", consider the following structured framework: Understand the Importance of User Feedback Recognize that user feedback is critical for…

Read answer guide
How would you handle a situation where your project manager asks you to bill for hours not worked due to the project running under budget?
January 24, 2025Hard

How would you handle a situation where your project manager asks you to bill for hours not worked due to the project running under budget?

Approach When responding to the question, "How would you handle a situation where your project manager asks you to bill for hours not worked due to the project running under budget?", it's essential to follow a structured framework. Here’s a step-by-step…

Read answer guide
What inputs do you consider when creating a product roadmap?
January 21, 2025Medium

What inputs do you consider when creating a product roadmap?

Approach To effectively answer the interview question, "What inputs do you consider when creating a product roadmap?", follow these structured steps: Understanding the Product Vision : Start with the overarching goals of the product and the company.…

Read answer guide
How can you implement a dynamic programming solution for the burst balloons problem?
January 16, 2025Hard

How can you implement a dynamic programming solution for the burst balloons problem?

Approach To effectively answer how to implement a dynamic programming solution for the burst balloons problem, follow this structured framework: Understand the Problem Statement : Clearly define the burst balloons problem and identify its key components.…

Read answer guide
How would you estimate Meta's daily user registrations?
February 13, 2025Medium

How would you estimate Meta's daily user registrations?

Approach When asked to estimate Meta's daily user registrations, it's essential to have a structured approach that showcases your analytical thinking and problem-solving skills. Here's a breakdown of the thought process into logical steps: Define the Problem…

Read answer guide
How do you calculate levered and unlevered beta, and why is it important in financial analysis?
January 16, 2025Hard

How do you calculate levered and unlevered beta, and why is it important in financial analysis?

Approach When approaching the question, "How do you calculate levered and unlevered beta, and why is it important in financial analysis?", it's essential to have a structured framework that allows you to articulate your understanding clearly. Follow these…

Read answer guide