PracHub
QuestionsCoachesLearningGuidesInterview Prep

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.

Part 1: Count Distinct Translation-Equivalent Islands

You are given a rectangular binary grid where 0 represents water and 1 represents land. An island is a maximal group of land cells connected 4-directionally: up, down, left, or right. Two islands are considered the same shape if one can be translated, or shifted, to exactly match the other. Rotations and reflections do not count as the same shape. Return the number of distinct island shapes in the grid.

Constraints

  • 1 <= len(grid) <= 500
  • 1 <= len(grid[0]) <= 500
  • grid[i][j] is either 0 or 1
  • The grid is rectangular
  • Aim for O(m * n) time

Examples

Input: ([[0]],)

Expected Output: 0

Explanation: There is no land, so there are no island shapes.

Input: ([[1]],)

Expected Output: 1

Explanation: There is exactly one island consisting of one cell.

Input: ([[1, 1, 0, 0, 0], [1, 0, 0, 1, 1], [0, 0, 0, 1, 0], [1, 1, 0, 0, 0], [1, 0, 0, 1, 1]],)

Expected Output: 2

Explanation: There are multiple L-shaped islands with the same translation-normalized shape, plus one horizontal domino shape.

Input: ([[1, 1, 0, 1, 1], [1, 0, 0, 0, 1]],)

Expected Output: 2

Explanation: The two islands are mirror images, but reflections do not count as the same shape.

Input: ([[1, 0, 1], [0, 1, 0], [1, 0, 1]],)

Expected Output: 1

Explanation: Diagonal land cells are not connected, so there are five single-cell islands, all with the same shape.

Hints

  1. For each island, record every land cell relative to the island's first discovered cell.
  2. Store a canonical representation of each normalized island shape in a set.

Part 2: Minimum Cost to Create a Valid Path in a Directed Grid

You are given an m x n grid where each cell contains a direction: 1 means right, 2 means left, 3 means down, and 4 means up. Starting from cell (0, 0), following a cell's direction moves you to the indicated neighboring cell if that neighbor is inside the grid. You may change the direction of any cell to any of the four directions, with cost 1 per changed cell. Return the minimum total cost needed so that there exists at least one valid path from (0, 0) to (m - 1, n - 1).

Constraints

  • 1 <= len(dir) <= 1000
  • 1 <= len(dir[0]) <= 1000
  • dir[i][j] is one of 1, 2, 3, or 4
  • The grid is rectangular
  • Aim for near-linear time in the number of cells

Examples

Input: ([[1]],)

Expected Output: 0

Explanation: The start cell is already the destination.

Input: ([[1, 1, 3], [3, 2, 2], [1, 1, 4]],)

Expected Output: 0

Explanation: Following the existing directions already reaches the bottom-right cell.

Input: ([[1, 2], [4, 3]],)

Expected Output: 1

Explanation: Follow right from the start, then change the top-right cell to point down.

Input: ([[1, 1, 1, 1], [2, 2, 2, 2], [1, 1, 1, 1], [2, 2, 2, 2]],)

Expected Output: 3

Explanation: At least three downward moves are needed, and each downward move requires changing a cell's direction.

Input: ([[4, 4, 4], [4, 4, 4], [4, 4, 4]],)

Expected Output: 4

Explanation: All arrows point upward, so every right or down move on a shortest path requires a direction change.

Hints

  1. Model each cell as a graph node. Moving in the cell's current direction has cost 0; moving in any other valid direction has cost 1.
  2. Because all edge weights are only 0 or 1, use 0-1 BFS with a deque instead of ordinary BFS.
Last updated: Jul 8, 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

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Solve common string/DP/stack problems - TikTok (medium)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)