PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/TikTok

Count distinct island shapes in a grid

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of grid-based connectivity and shape equivalence, exercising skills in graph traversal, spatial normalization, and efficient deduplication of patterns.

  • medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Count distinct island shapes in a grid

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem You are given an `m x n` grid of integers where `1` represents land and `0` represents water. An **island** is a group of horizontally or vertically adjacent `1`s. Two islands are considered the **same shape** if one can be translated (shifted up/down/left/right) to match the other **exactly**. Rotations and reflections **do not** count as the same shape. Return the number of **distinct island shapes** in the grid. ## Input - `grid`: `m x n` matrix of `0/1` ## Output - An integer: the number of distinct island shapes. ## Constraints - `1 <= m, n <= 500` (typical) - Grid may contain many islands ## Notes - Adjacency is 4-directional (up/down/left/right). - Translation allowed; rotation/reflection not allowed. ## Example If the grid contains two L-shaped islands in different locations, they count as **one** distinct shape.

Quick Answer: This question evaluates understanding of grid-based connectivity and shape equivalence, exercising skills in graph traversal, spatial normalization, and efficient deduplication of patterns.

Related Interview Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)
TikTok logo
TikTok
Jan 18, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
1
0
Loading...

Problem

You are given an m x n grid of integers where 1 represents land and 0 represents water. An island is a group of horizontally or vertically adjacent 1s.

Two islands are considered the same shape if one can be translated (shifted up/down/left/right) to match the other exactly. Rotations and reflections do not count as the same shape.

Return the number of distinct island shapes in the grid.

Input

  • grid : m x n matrix of 0/1

Output

  • An integer: the number of distinct island shapes.

Constraints

  • 1 <= m, n <= 500 (typical)
  • Grid may contain many islands

Notes

  • Adjacency is 4-directional (up/down/left/right).
  • Translation allowed; rotation/reflection not allowed.

Example

If the grid contains two L-shaped islands in different locations, they count as one distinct shape.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More TikTok•More Software Engineer•TikTok Software Engineer•TikTok Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.