PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/TikTok

Count islands using BFS without modifying grid

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of BFS-based graph traversal on grids, managing visited state without modifying the input, and techniques for encoding island shapes to detect distinct configurations.

  • hard
  • TikTok
  • Coding & Algorithms
  • Data Engineer

Count islands using BFS without modifying grid

Company: TikTok

Role: Data Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: HR Screen

You are given an `m x n` binary grid `grid` where: - `1` represents land - `0` represents water - Islands are groups of horizontally or vertically adjacent land cells (4-directional connectivity). ### Part A Return the number of islands in the grid. **Constraint:** You must solve it using **BFS**, and you are **not allowed to modify `grid` in-place** (e.g., you cannot flip visited `1`s to `0`s). ### Part B (follow-up) Two islands are considered the **same shape** if one can be translated (shifted up/down/left/right) to match the other exactly (rotations/reflections do **not** count unless explicitly stated). Return the number of **distinct island shapes** in the grid. ### Input/Output - **Input:** `grid: List[List[int]]` - **Output (Part A):** integer count of islands - **Output (Part B):** integer count of distinct island shapes ### Assumptions / Constraints - `1 <= m, n <= 200` (or similar) - Time should be near `O(mn)` - Extra space allowed for visited tracking and shape encoding

Quick Answer: This question evaluates understanding of BFS-based graph traversal on grids, managing visited state without modifying the input, and techniques for encoding island shapes to detect distinct configurations.

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
Oct 19, 2025, 12:00 AM
Data Engineer
HR Screen
Coding & Algorithms
5
0

You are given an m x n binary grid grid where:

  • 1 represents land
  • 0 represents water
  • Islands are groups of horizontally or vertically adjacent land cells (4-directional connectivity).

Part A

Return the number of islands in the grid.

Constraint: You must solve it using BFS, and you are not allowed to modify grid in-place (e.g., you cannot flip visited 1s to 0s).

Part B (follow-up)

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

Return the number of distinct island shapes in the grid.

Input/Output

  • Input: grid: List[List[int]]
  • Output (Part A): integer count of islands
  • Output (Part B): integer count of distinct island shapes

Assumptions / Constraints

  • 1 <= m, n <= 200 (or similar)
  • Time should be near O(mn)
  • Extra space allowed for visited tracking and shape encoding

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More TikTok•More Data Engineer•TikTok Data Engineer•TikTok Coding & Algorithms•Data Engineer Coding & Algorithms
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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.