PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/TikTok

Solve two grid problems (islands + min-cost path)

Last updated: Mar 29, 2026

Quick Overview

This pair of problems evaluates grid-based algorithm competencies, specifically connected-component detection and translation-invariant shape recognition for distinct-island counting, plus shortest-path/graph-search reasoning under unit-change costs for the directed-grid minimum-cost path.

  • medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Solve two grid problems (islands + min-cost path)

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given two separate coding questions. ## Problem A: Count distinct islands (translation-equivalent) Given an `m x n` binary grid `grid` (0 = water, 1 = land), an **island** is a maximal 4-directionally connected group of `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. **Task:** Return the number of **distinct** island shapes in the grid. **Input:** `grid: List[List[int]]` **Output:** `int` **Notes/constraints (assume typical interview constraints):** - `1 <= m, n <= 500` - Grid can be large; aim for about `O(mn)` time. --- ## Problem B: Minimum cost to create a valid path in a directed grid You are given an `m x n` grid `dir` where each cell contains a direction pointing to a neighbor: - `1` = right, `2` = left, `3` = down, `4` = up Starting at `(0,0)`, if you follow directions, you move to the indicated neighboring cell (if within bounds). A **valid path** is a path that reaches `(m-1, n-1)`. You may **change** the direction in any cell at a cost of `1` per changed cell (each cell can be changed to any of the 4 directions). **Task:** Return the minimum total cost to ensure there exists at least one valid path from `(0,0)` to `(m-1,n-1)`. **Input:** `dir: List[List[int]]` **Output:** `int` **Notes/constraints (assume typical interview constraints):** - `1 <= m, n <= 1000` - Aim for near-linear time in number of cells.

Quick Answer: This pair of problems evaluates grid-based algorithm competencies, specifically connected-component detection and translation-invariant shape recognition for distinct-island counting, plus shortest-path/graph-search reasoning under unit-change costs for the directed-grid minimum-cost path.

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 22, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
5
0
Loading...

You are given two separate coding questions.

Problem A: Count distinct islands (translation-equivalent)

Given an m x n binary grid grid (0 = water, 1 = land), an island is a maximal 4-directionally connected group of 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.

Task: Return the number of distinct island shapes in the grid.

Input: grid: List[List[int]]

Output: int

Notes/constraints (assume typical interview constraints):

  • 1 <= m, n <= 500
  • Grid can be large; aim for about O(mn) time.

Problem B: Minimum cost to create a valid path in a directed grid

You are given an m x n grid dir where each cell contains a direction pointing to a neighbor:

  • 1 = right, 2 = left, 3 = down, 4 = up

Starting at (0,0), if you follow directions, you move to the indicated neighboring cell (if within bounds). A valid path is a path that reaches (m-1, n-1).

You may change the direction in any cell at a cost of 1 per changed cell (each cell can be changed to any of the 4 directions).

Task: Return the minimum total cost to ensure there exists at least one valid path from (0,0) to (m-1,n-1).

Input: dir: List[List[int]]

Output: int

Notes/constraints (assume typical interview constraints):

  • 1 <= m, n <= 1000
  • Aim for near-linear time in number of cells.

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.