← TikTok Interview Insights

TikTok·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Two coding problems back to back for a TikTok SWE screen. Linked list manipulation and a binary tree deletion with a specific reconnection rule. Nothing too crazy but the second one had enough edge cases to slow me down.

Questions Asked (2)

Q1

Given the head of a singly linked list and an integer n, remove the nth node from the end of the list and return the new head. Aim for a single pass with constant extra space.

Algorithms & Data Structures
Author's notes

Two-pointer approach, pretty standard.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use the two-pointer technique: advance a fast pointer n+1 steps ahead, then move both pointers until fast reaches the end. The slow pointer will be just before the node to remove, allowing a single-pass deletion with constant space. Handle edge cases like removing the head by using a dummy node.

Pro tip: Explicitly discuss edge cases (e.g., n equals list length, single-node list) and how the dummy node simplifies head removal. Also, mention that this approach is optimal for time and space, which interviewers at TikTok appreciate.

1. Clarify and Confirm

Restate the problem to ensure understanding: remove the nth node from the end of a singly linked list, return the new head, in one pass with O(1) space. Ask about constraints (e.g., n validity, list length).

2. Design the Two-Pointer Approach

Explain that you'll use two pointers, fast and slow, initially at a dummy node. Advance fast n+1 steps ahead, then move both until fast reaches null. Slow will point to the node before the one to remove.

3. Handle Edge Cases

Discuss using a dummy node to simplify removal of the head (when n equals list length). Also consider n=1 (remove last node) and single-node list.

4. Implement and Test

Write clean code with clear variable names. Walk through an example (e.g., 1->2->3->4->5, n=2) to verify correctness. Mention time complexity O(L) and space O(1).

5. Review and Optimize

Double-check pointer manipulations for off-by-one errors. Confirm that the solution meets the single-pass and constant-space requirements. Discuss potential follow-ups (e.g., what if n is invalid).

Key Points to Mention

  • Two-pointer technique with a gap of n+1 to locate the node before the target.
  • Use of a dummy node to handle removal of the head uniformly.
  • Time complexity O(L) where L is list length, space complexity O(1).
  • Edge cases: n=1, n=list length, single-node list, and invalid n.
  • Single-pass requirement satisfied by not traversing the list twice.
  • Return dummy.next as the new head.

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

Q2

Given the root of a binary tree (not a BST) and a key value, delete the node matching that key using this rule: find the deepest rightmost node in the tree, copy its value into the target node, then remove that deepest rightmost node. Return the new root.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This tripped me up more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use level-order traversal (BFS) to locate the target node and simultaneously track the deepest rightmost node and its parent. Then copy the deepest rightmost node's value into the target node and delete the deepest rightmost node by updating its parent's child pointer.

Pro tip: Clarify edge cases upfront: if the tree is empty, the key is not found, or the target node is the deepest rightmost node itself. Also mention that this approach modifies the tree in-place and runs in O(n) time with O(n) space for the queue.

1. Handle edge cases and clarify assumptions

Check if the root is null or if the key is not present. Confirm whether the tree can have duplicate keys and whether the deepest rightmost node is defined as the last node in level-order traversal.

2. Traverse the tree to find the target and deepest rightmost node

Perform a BFS using a queue, storing each node along with its parent. Track the target node and its parent, and update the deepest rightmost node and its parent as you process each level.

3. Copy the deepest rightmost node's value to the target node

If the target node is found, replace its value with the value of the deepest rightmost node. If the target is the root and the tree has only one node, handle removal appropriately.

4. Remove the deepest rightmost node

Update the parent of the deepest rightmost node to remove the reference to it (set the appropriate child pointer to null). If the deepest rightmost node is the root (only one node), set root to null.

5. Return the new root

After deletion, return the original root (unless it was the only node and removed). Discuss time and space complexity: O(n) time, O(n) space due to queue.

Key Points to Mention

  • BFS is preferred over DFS because it naturally finds the deepest rightmost node by processing level by level.
  • Tracking parent pointers is essential to efficiently remove the deepest rightmost node without additional traversal.
  • Edge cases: empty tree, key not found, tree with single node, target node being the deepest rightmost node.
  • Time complexity: O(n) where n is number of nodes; space complexity: O(n) for the queue in worst case.
  • Alternative approach: DFS with height tracking, but BFS is simpler and more intuitive for this problem.
  • The tree is not a BST, so no ordering property can be used; must traverse all nodes.

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