← TikTok Interview Insights

TikTok·Machine Learning Engineer·Onsite - Coding / Algorithms·Senior

SeniorPrefer not to say
Jul 2026

Summary

TikTok ML engineer coding round, four algorithmic problems back to back. The problems ranged from classic stack design to a tree path question that had a subtle twist I didn't fully appreciate until after.

Questions Asked (4)

Q1

Design a MinStack that supports push, pop, top, and getMin, where all operations must run in O(1) time.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Pretty standard warmup.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use an auxiliary stack to track the minimum value at each state, ensuring O(1) time for all operations. Explain how each operation maintains the auxiliary stack to keep getMin constant time.

Pro tip: Mention that this design uses O(n) extra space, but you can optimize to O(1) extra space by storing the difference between the value and the current minimum, which is a common follow-up.

1. Clarify requirements and constraints

Confirm that all operations must be O(1) time and discuss space complexity expectations. Ask if the stack can contain negative numbers or duplicates.

2. Propose the auxiliary stack approach

Describe using a main stack for values and a min stack that stores the minimum at each level. Explain how push, pop, top, and getMin work in O(1).

3. Walk through an example

Trace operations like push(5), push(3), push(7), getMin(), pop(), getMin() to demonstrate correctness and O(1) time.

4. Discuss trade-offs and optimizations

Mention the O(n) space overhead and the O(1) space optimization using difference encoding. Compare pros and cons.

5. Consider edge cases and extensions

Address empty stack operations, duplicate minima, and potential follow-ups like thread safety or generic types.

Key Points to Mention

  • Auxiliary stack storing the minimum value at each state
  • O(1) time for push, pop, top, and getMin
  • Space complexity O(n) for the auxiliary stack
  • Optimization to O(1) extra space using difference encoding
  • Handling duplicates and negative numbers correctly
  • Edge cases: empty stack, popping when min is removed

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

Q2

Design a MaxStack that supports push, pop, top, peekMax, and popMax. For popMax, if there are multiple maximum elements, remove the one closest to the top. Discuss time complexity tradeoffs.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where things got messier.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements and edge cases, then propose a solution using two stacks: one for the main stack and one for tracking maximums. Discuss the time complexity of each operation, highlighting that popMax is O(n) in the simple approach, and then explore optimizations like a doubly linked list with a tree map for O(log n) operations.

Pro tip: Mention that in real-world ML systems, such as feature stores or model versioning, similar stack-like structures with max retrieval are used, and the tradeoff between simplicity and performance often depends on the frequency of popMax operations.

1. Clarify requirements and edge cases

Ask about the expected frequency of operations, whether the stack can be empty, and if there are constraints on memory. Confirm that popMax should remove the topmost maximum element.

2. Propose a simple two-stack solution

Use a main stack for all elements and a max stack that keeps track of the maximum value at each level. Explain how push, pop, top, and peekMax work in O(1), but popMax requires O(n) to find and remove the topmost maximum.

3. Analyze time complexity tradeoffs

Compare the simple solution with more advanced data structures like a doubly linked list combined with a balanced BST (e.g., TreeMap) to achieve O(log n) for all operations. Discuss the overhead and implementation complexity.

4. Discuss optimizations and alternatives

Mention that if popMax is rare, the simple solution is acceptable. If frequent, consider the linked list + TreeMap approach, or a heap with lazy deletion, noting that lazy deletion can lead to O(n) worst-case for popMax if many stale entries.

5. Conclude with a recommendation

Summarize the tradeoffs and suggest a solution based on the assumed operation frequencies. Emphasize that the choice depends on the specific use case and performance requirements.

Key Points to Mention

  • Two-stack approach for O(1) push, pop, top, peekMax, but O(n) popMax.
  • Optimized approach using doubly linked list and TreeMap for O(log n) all operations.
  • Tradeoff between implementation complexity and performance.
  • Handling duplicates: popMax removes the topmost maximum.
  • Edge cases: empty stack, single element, multiple maximums.
  • Real-world relevance to ML systems (e.g., feature versioning, caching).

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

Q3

Given a continuous stream of integers, design a data structure that supports inserting numbers and querying the current median at any point.

Algorithms & Data StructuresSystem Design
Author's notes

Two heaps.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use two heaps (a max-heap for the lower half and a min-heap for the upper half) to maintain the median in O(log n) insertion and O(1) query. Explain the balancing logic and how to handle even/odd counts, then discuss trade-offs with alternative approaches like balanced BSTs or sorted arrays.

Pro tip: Mention that this is a classic streaming median problem and that the two-heap approach is optimal for online queries; also note that for ML pipelines at TikTok, you might need to handle large-scale streams with distributed or approximate methods.

1. Clarify requirements and constraints

Ask about the expected volume of insertions, query frequency, memory limits, and whether exact median is required. This shows you consider real-world system constraints.

2. Propose the two-heap solution

Describe maintaining a max-heap for the lower half and a min-heap for the upper half, ensuring their sizes differ by at most one. Explain how to insert and rebalance.

3. Detail insertion and median query operations

Walk through the insertion algorithm: add to appropriate heap, rebalance if needed. For median, if heaps are equal size, average the roots; otherwise return the root of the larger heap.

4. Analyze complexity and trade-offs

State O(log n) insertion and O(1) query time, O(n) space. Compare with alternatives like balanced BST (O(log n) insert, O(log n) query) or sorted list (O(n) insert).

5. Discuss extensions and ML relevance

Mention handling duplicates, negative numbers, and potential need for approximate medians in distributed streams. Relate to ML feature engineering or monitoring tasks.

Key Points to Mention

  • Two-heap approach: max-heap for lower half, min-heap for upper half
  • Balancing condition: sizes differ by at most 1
  • Insertion: add to correct heap, then rebalance by moving top element if needed
  • Median query: O(1) by checking heap sizes and roots
  • Time complexity: O(log n) insert, O(1) query; space O(n)
  • Trade-offs: exact vs approximate, memory vs speed, and distributed streaming considerations

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

Q4

Given a binary tree with positive integer node values and a target integer, determine whether there is a path starting at any node and moving only upward toward the root where the node values sum to the target. Return true or false.

Algorithms & Data Structures
Author's notes

The upward-only constraint tripped me up at first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the path must be upward-only (from any node to an ancestor) and then use a recursive DFS that passes down the current prefix sum from the root. At each node, check whether the difference between the current prefix sum and the target exists in a hash set of ancestor prefix sums, which detects any valid upward path ending at that node. Return true if any such path is found.

Pro tip: Mention that this is essentially the 'path sum III' pattern but restricted to upward paths, and that using a hash set of prefix sums gives O(n) time instead of O(n^2) brute force. Also note that because node values are positive, you can optionally prune when the prefix sum exceeds the target, but the hash set approach handles all cases cleanly.

1. Clarify the problem and constraints

Confirm that the path starts at any node and moves only upward toward the root, and that node values are positive integers. Ask whether the path must include at least one node and whether the target can be zero or negative (though values are positive).

2. Define the recursive state

Use DFS from the root, passing down the current prefix sum from the root to the current node. Maintain a hash set of prefix sums of all ancestors of the current node (including the current node's prefix sum before processing children).

3. Check for valid upward paths at each node

At each node, compute the current prefix sum. If (current prefix sum - target) exists in the ancestor prefix sum set, then there is an upward path ending at this node that sums to the target. Return true immediately.

4. Recurse and backtrack

Add the current prefix sum to the set, recurse into left and right children, then remove the current prefix sum from the set before returning to the parent (backtracking).

5. Return the final result

If any recursive call returns true, propagate true up the call stack. If the entire tree is traversed without finding a valid path, return false.

Key Points to Mention

  • The path is upward-only, meaning it goes from a node to one of its ancestors (including itself).
  • Use prefix sums from the root to efficiently check for a subpath sum equal to the target.
  • A hash set of ancestor prefix sums allows O(1) lookup for the required complement (current prefix sum - target).
  • Backtracking is necessary to remove the current node's prefix sum when returning to the parent, so the set only contains ancestors of the current node.
  • Time complexity is O(n) and space complexity is O(h) for the recursion stack plus O(h) for the hash set, where h is the tree height.
  • Edge cases: empty tree, single node, target equal to a node value, and paths that start and end at the same node.

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