← TikTok Interview Insights

TikTok·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

Coding interview at TikTok for a software engineer role. Two algorithm questions, both with follow-up discussions on complexity and design trade-offs. Pretty standard competitive programming territory but the extensions they asked about kept things interesting.

Questions Asked (2)

Q1

Given a binary grid where 1s are land and 0s are water, count the number of distinct island shapes. Two islands count as the same only if one can be translated to match the other; rotations and reflections are considered different. Then explain how you'd extend this if rotations and reflections also made islands equivalent.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The base version I got through fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use DFS/BFS to find each island, then compute a canonical representation of its shape by normalizing the coordinates (e.g., subtracting the min row and min col). Store these canonical forms in a set to count distinct shapes. For the extension, generate all 8 transformations (rotations and reflections) of each shape, normalize each, and use the lexicographically smallest as the canonical key.

Pro tip: Clarify upfront that you assume translation only for the first part, and that the grid is binary and islands are 4-connected. For the extension, mention that you can precompute all 8 transformations and pick the minimum to avoid redundant comparisons.

1. Clarify assumptions and constraints

Confirm connectivity (4-directional), grid size, and that translation is the only allowed transformation for the first part. Ask if the grid can be large to discuss complexity.

2. Find and extract islands

Use DFS or BFS to traverse each unvisited land cell, collecting all coordinates of the island. Mark visited cells to avoid reprocessing.

3. Compute canonical shape representation

For each island, subtract the minimum row and column from all coordinates to normalize its position. This yields a translation-invariant representation.

4. Count distinct shapes

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

5. Extend to rotations and reflections

For each island, generate all 8 transformations (rotations by 0°, 90°, 180°, 270° and their reflections). Normalize each transformed shape, then choose the lexicographically smallest as the canonical key. Use that key in the set.

Key Points to Mention

  • DFS/BFS for island discovery and marking visited cells.
  • Normalization by subtracting min row and min col to achieve translation invariance.
  • Using a hash set to store canonical shapes for O(1) average insertion and lookup.
  • Time complexity: O(R*C) for traversal plus O(K) per island for normalization, where K is island size.
  • For the extension, generating all 8 transformations and selecting the lexicographically smallest as canonical key.
  • Space complexity: O(R*C) for visited and O(N) for storing shapes, where N is total land cells.
  • Potential optimization: encode shape as a string or tuple of sorted coordinates for hashing.

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

Q2

Given an integer array and a window size k, find the maximum value in every contiguous subarray of length k. Implement a linear time solution and explain why it's O(n). Then compare it to a heap-based approach and discuss the trade-offs.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Sliding window maximum with a deque.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then present the optimal O(n) solution using a monotonic deque, explaining its linear time complexity. Next, describe a heap-based approach and compare their time/space complexities and practical trade-offs, concluding with when to use each.

Pro tip: Emphasize that the deque approach achieves O(n) because each element is added and removed at most once, and mention that while a heap is simpler to implement, it may be slower in practice due to higher constant factors and O(n log k) time.

1. Clarify the problem

Restate the problem, confirm input/output format, and discuss edge cases like k=1, k=n, empty array, or k>n.

2. Present the optimal solution

Explain the monotonic deque approach: maintain a deque of indices with decreasing values, remove out-of-window indices, and add the front element to the result for each window.

3. Analyze time and space complexity

Argue that each element is pushed and popped at most once, so total operations are O(n); space is O(k) for the deque and O(n-k+1) for the output.

4. Compare with heap-based approach

Describe using a max-heap of size k: insert first k elements, then for each slide, remove the outgoing element (lazy deletion) and insert the new one, recording the max. Complexity: O(n log k) time, O(k) space.

5. Discuss trade-offs

Compare: deque is O(n) time and O(k) space, but more complex; heap is O(n log k) time and O(k) space, simpler but slower for large n. Mention that for small k, heap may be competitive due to lower constant factors.

Key Points to Mention

  • Monotonic deque maintains indices of potential maximums in decreasing order.
  • Each element is added and removed at most once, ensuring O(n) time.
  • Heap-based approach uses lazy deletion to handle out-of-window elements.
  • Time complexity: deque O(n) vs heap O(n log k).
  • Space complexity: both O(k) for the data structure, plus O(n-k+1) for output.
  • Trade-offs: deque is optimal but trickier to implement; heap is simpler but less efficient for large n.

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