PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's understanding of grid-based graph traversal and connected-component detection, along with the ability to recognize and canonicalize distinct island shapes.

  • medium
  • LinkedIn
  • Coding & Algorithms
  • Software Engineer

Count islands and distinct shapes

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

##### Question LeetCode 200. Number of Islands LeetCode 694. Number of Distinct Islands https://leetcode.com/problems/number-of-islands/description/ https://leetcode.com/problems/number-of-distinct-islands/description/

Quick Answer: This question evaluates a candidate's understanding of grid-based graph traversal and connected-component detection, along with the ability to recognize and canonicalize distinct island shapes.

Given a binary grid of 0s and 1s, count the total number of islands and the number of distinct island shapes. An island is a maximal group of 1s connected 4-directionally (up, down, left, right). Two islands are the same shape if one can be translated (shifted) to match the other; rotations and reflections do not count. Return [number_of_islands, number_of_distinct_shapes].

Constraints

  • 1 <= rows, cols <= 200
  • grid[i][j] is 0 or 1
  • Connectivity is 4-directional (up, down, left, right)
  • Two islands are the same shape only under translation; rotations/reflections are different
  • Return format: [number_of_islands, number_of_distinct_shapes]

Hints

  1. Traverse each unvisited land cell with DFS or BFS to mark its island.
  2. Record each island's cells relative to the starting cell to normalize by translation.
  3. Sort the relative coordinates to get a canonical shape representation and store it in a set.
  4. Use only 4-directional neighbors; diagonals do not connect islands.
Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Longest Run of Ones After One Flip - LinkedIn (medium)
  • The Celebrity Problem - LinkedIn (medium)
  • Count Trips From Vehicle Logs - LinkedIn (easy)
  • Process Mutable Matrix Sum Queries - LinkedIn (medium)
  • Design O(1) Randomized Multiset - LinkedIn (easy)