← TikTok Interview Insights

TikTok·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

TikTok software engineer interview with a graph problem that had a twist I didn't see coming. The torus wrapping condition threw me off more than I expected, and the follow-ups kept coming.

Questions Asked (4)

Q1

Given an m x n binary grid on a torus (top wraps to bottom, left wraps to right), count the number of connected land components (islands) using 4-directional adjacency with wrap-around.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I started with standard BFS island counting and felt pretty good about it, then the wrap-around adjacency hit me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the torus by using modulo arithmetic for neighbor coordinates, then apply a standard connected components algorithm like BFS/DFS or Union-Find. Clearly explain how wrap-around affects neighbor generation and discuss trade-offs between traversal and union-find approaches.

Pro tip: Mention that you can avoid explicit grid duplication by using modulo indexing, and highlight that Union-Find with path compression is particularly efficient for dynamic connectivity or when the grid is large and sparse.

1. Clarify the problem and constraints

Confirm the definition of connected components (4-directional with wrap-around) and discuss edge cases like empty grid, all land, all water, and single row/column.

2. Choose an algorithm

Decide between BFS/DFS with visited set or Union-Find. Consider time/space complexity and whether the grid is static or dynamic.

3. Handle torus wrap-around

Use modulo arithmetic: for a cell (r, c), neighbors are ((r±1) mod m, c) and (r, (c±1) mod n). Ensure no out-of-bounds errors.

4. Implement and traverse

Iterate through each cell; if it's land and unvisited, increment component count and explore all connected land using the chosen algorithm.

5. Analyze complexity and trade-offs

State O(m*n) time and space for BFS/DFS, and near O(m*n α) for Union-Find. Discuss when one is preferable over the other.

Key Points to Mention

  • Modulo arithmetic for neighbor coordinates to simulate torus wrap-around.
  • BFS/DFS with visited matrix or Union-Find for connected components.
  • Time and space complexity: O(m*n) for traversal, O(m*n) space for visited; Union-Find uses O(m*n) space for parent array.
  • Edge cases: empty grid, all land (one component), all water (zero components), single row/column where wrap-around creates cycles.
  • Trade-offs: BFS/DFS simpler to implement; Union-Find better for dynamic updates or when grid is large and sparse.
  • Avoiding infinite loops: ensure visited cells are marked before enqueueing or use Union-Find to merge only unvisited neighbors.

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

Q2

Solve the same torus island problem using two different approaches, such as graph search and a disjoint-set (union-find) structure.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

They asked for two distinct approaches and I fumbled the union-find version.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem: count the number of distinct islands on a torus (grid wraps around edges). Then, present two solutions: one using DFS/BFS with a visited set, and another using union-find to merge adjacent land cells. Compare their time/space complexity and discuss trade-offs.

Pro tip: Mention that on a torus, you must handle wrap-around neighbors carefully, and that union-find can be more efficient for dynamic connectivity but DFS is simpler for static grids. Also, note that both approaches have the same asymptotic complexity, but union-find may have better constant factors for large grids with many merges.

1. Clarify the problem

Confirm that the grid is a torus (edges wrap around) and that islands are connected components of land cells (1s) using 4-directional adjacency. Ask if diagonal connections count.

2. Graph search approach

Explain how to use DFS or BFS: iterate over each cell, if it's land and unvisited, start a search, mark all connected land cells as visited, and increment island count. Handle wrap-around by using modulo arithmetic for neighbor indices.

3. Union-find approach

Describe initializing a union-find structure for all land cells. For each land cell, union it with its right and down neighbors (with wrap-around). Then count the number of distinct roots among land cells.

4. Compare and contrast

Discuss time and space complexity: both are O(N) where N is number of cells, but union-find may have higher constant factors due to path compression and union by rank. DFS uses recursion stack (or explicit stack) which may be a limitation for very large grids.

5. Handle edge cases

Mention edge cases: empty grid, all water, all land (which forms one island on a torus), and grids with dimensions 1xN or Nx1 where wrap-around creates cycles.

Key Points to Mention

  • Torus wrap-around: use modulo arithmetic for neighbor indices (e.g., (row+1)%R, (col+1)%C).
  • DFS/BFS: mark visited in-place or with a separate visited matrix to avoid revisiting.
  • Union-find: use path compression and union by rank for near O(1) operations.
  • Counting islands: in union-find, count unique roots after all unions.
  • Complexity: both O(R*C) time, but union-find may use O(R*C) space for parent array, while DFS uses O(R*C) for visited and recursion stack.
  • Trade-offs: DFS is simpler to implement; union-find is better for dynamic connectivity or when recursion depth is a concern.

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

Q3

What are the time and space complexities of your solutions?

Algorithms & Data Structures
Author's notes

Fine, standard O(m*n) analysis.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

For each solution, clearly state the time and space complexity in Big-O notation, then briefly explain the reasoning behind each. If there are multiple solutions, compare their complexities and discuss trade-offs, especially in the context of TikTok's large-scale data.

Pro tip: Always mention the space complexity of the input and output separately from auxiliary space, and clarify if your analysis assumes average or worst-case scenarios. This shows attention to detail and a deep understanding of complexity analysis.

1. State the complexity

Clearly state the time and space complexity using Big-O notation for each solution you present.

2. Explain the reasoning

Briefly explain why the complexity is what it is, referencing the key operations (e.g., loops, recursion, data structure operations).

3. Compare solutions

If you provided multiple solutions, compare their complexities and discuss trade-offs between time and space.

4. Consider edge cases

Mention how the complexity might change with different inputs (e.g., best/worst/average cases, input size, data distribution).

5. Relate to scale

If relevant, discuss how the complexity impacts performance at TikTok's scale and whether optimizations are needed.

Key Points to Mention

  • Big-O notation and its formal definition
  • Time complexity analysis of loops, recursion, and common data structure operations
  • Space complexity: auxiliary space vs. input/output space
  • Best, average, and worst-case scenarios
  • Trade-offs between time and space (e.g., using extra space to reduce time)
  • Amortized analysis for dynamic arrays or hash tables

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

Q4

Extend your solution to handle a stream of single-cell flip updates (toggling between land and water) and report the island count after each update.

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

This is where I really struggled.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: we need to maintain the island count under cell flips, which is a dynamic connectivity problem. Propose a solution using Union-Find (Disjoint Set Union) with incremental updates: when a cell flips to land, increment count and union with adjacent land cells, decrementing count for each successful union; when flipping to water, decrement count and rebuild connections among its neighbors (or use a more advanced approach like Euler tour trees). Discuss trade-offs between simplicity and efficiency, and mention potential optimizations for TikTok's scale.

Pro tip: Emphasize that while Union-Find handles additions efficiently, deletions are tricky; showing awareness of this limitation and proposing workarounds (like offline processing or advanced data structures) demonstrates depth. Also, relate to TikTok's real-time content moderation or user graph updates to show business impact.

1. Clarify requirements and constraints

Ask about the grid size, frequency of updates, and whether updates are online or can be batched. Confirm that only single-cell flips occur and that we need the count after each update.

2. Choose data structures

Propose Union-Find for efficient union operations on land cells. For deletions, discuss options: rebuilding local components, using a dynamic connectivity structure, or processing offline in reverse.

3. Design the algorithm

Outline the update logic: for land addition, increment count, then for each neighbor that is land, union and decrement count if union succeeds. For water addition, decrement count, then re-evaluate connectivity among neighbors (e.g., by temporarily removing the cell and running BFS/DFS to recount).

4. Analyze complexity and trade-offs

State time complexity: O(α(N)) per union/find for additions, but deletions may be O(k) where k is component size if rebuilding. Discuss space complexity O(N). Mention that for large-scale streams, an offline approach with reverse processing can make all operations additions.

5. Optimize and handle edge cases

Suggest optimizations like path compression, union by rank, and early termination. Handle edge cases: flipping a cell that is already in the desired state, grid boundaries, and isolated cells.

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank for near-constant time operations.
  • Incremental island count update: +1 for new land, -1 for each successful union with adjacent land.
  • Challenge of deletions: Union-Find does not support efficient deletion, so need alternative strategies (e.g., local rebuild, Euler tour trees, or offline reverse processing).
  • Trade-offs between online vs. offline processing: offline allows reversing the stream to convert deletions into additions, simplifying the problem.
  • Time and space complexity analysis: O(α(N)) per addition, O(N) space; deletions may be costlier.
  • Scalability considerations for TikTok: handling high-frequency updates, potential use of distributed systems or sharding for massive grids.

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