← TikTok Interview Insights

TikTok·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

TikTok data engineer technical screen, two-part grid/island problem. Part A was pretty standard BFS stuff but the no-in-place-modification constraint tripped me up for a minute. Part B was the real test and I don't think I fully nailed the shape encoding.

Questions Asked (2)

Q1

Given a binary grid where 1s are land and 0s are water, count the number of islands using BFS. You cannot modify the grid in place to track visited cells.

Algorithms & Data Structures
Author's notes

The BFS part was fine, I've done island counting before.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use BFS to traverse each island, but since the grid cannot be modified, maintain a separate visited matrix (or set) to track explored cells. Iterate through each cell; when an unvisited land cell is found, increment the island count and BFS to mark all connected land cells as visited.

Pro tip: Explicitly discuss the space-time tradeoff: using a visited matrix costs O(m*n) extra space but preserves the input, which is often required in production systems. Mention that if memory is a concern, you could use a hash set of encoded coordinates, but a boolean matrix is more efficient.

1. Clarify constraints and edge cases

Confirm grid dimensions, connectivity (4-directional), and that the grid must remain unmodified. Discuss edge cases like empty grid, all water, or all land.

2. Choose data structures

Use a 2D boolean array (or set) for visited tracking and a queue for BFS. Explain why a separate visited structure is necessary.

3. Iterate and initiate BFS

Loop through each cell. If it's land and unvisited, increment island count and start BFS from that cell.

4. Perform BFS traversal

For each dequeued cell, check its 4 neighbors. If a neighbor is land and unvisited, mark it visited and enqueue it.

5. Analyze complexity and discuss alternatives

State time complexity O(m*n) and space complexity O(m*n) due to visited matrix and queue. Mention that DFS is an alternative but BFS avoids recursion depth issues.

Key Points to Mention

  • Use a separate visited matrix to avoid modifying the input grid.
  • BFS queue processes cells level by level, ensuring all connected land is visited.
  • Time complexity is O(m*n) because each cell is visited at most once.
  • Space complexity is O(m*n) for the visited matrix and queue in the worst case.
  • Edge cases: empty grid, single row/column, all land or all water.
  • Alternative: DFS with visited matrix, but BFS is iterative and avoids stack overflow.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Follow-up: given the same grid, return the count of distinct island shapes, where two islands are the same shape if one can be translated to overlap the other (no rotations or reflections).

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I started to sweat.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use DFS/BFS to identify each island and record its cells. For each island, normalize its shape by translating it so that its top-left-most cell (or minimum row and column) is at (0,0), then store the normalized shape in a hash set. The count of distinct shapes is the size of the set.

Pro tip: Mention that you can encode the normalized shape as a string or tuple of coordinates to use as a hash key, and emphasize that translation normalization is key to ignoring absolute positions.

1. Identify islands

Traverse the grid using DFS or BFS to find all connected components of land cells (1s). For each island, collect the coordinates of its cells.

2. Normalize shape

For each island, find the minimum row and column among its cells. Subtract these from all cell coordinates to translate the island so its top-left-most cell is at (0,0).

3. Canonical representation

Convert the normalized list of coordinates into a hashable form, such as a sorted tuple of (row, col) pairs or a string encoding.

4. Count distinct shapes

Insert each canonical representation into a hash set. The number of distinct shapes is the size of the set.

5. Complexity analysis

State that time complexity is O(R*C) for traversal and normalization, and space complexity is O(R*C) for storing visited cells and shapes.

Key Points to Mention

  • Translation normalization by subtracting min row and min col
  • Using a hash set to store canonical shapes
  • DFS/BFS for island detection
  • Handling of duplicate shapes
  • Time and space complexity
  • Edge cases: empty grid, no islands, single-cell islands

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.