← TikTok Interview Insights

TikTok·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

TikTok software engineer coding round, one question the whole session. The problem was binary tree max path sum, classic but with enough edge cases to trip you up if you're not careful about how you handle negatives.

Questions Asked (1)

Q1

Given the root of a binary tree where node values can be positive, negative, or zero, return the maximum path sum across all possible paths. A path can start and end at any nodes and must contain at least one node.

Algorithms & Data Structures
Author's notes

The basic idea clicked pretty fast: recurse through the tree, track the best gain you can get from each subtree, and at every node consider whether using both children forms a better global answer.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order DFS that returns the maximum gain from each node to its parent, while updating a global maximum with the best path sum through that node. At each node, compute the best downward path (node value plus max of left/right gains, ignoring negative gains) and update the global max with node value plus both positive gains.

Pro tip: Emphasize that negative gains should be treated as 0 to avoid dragging down the path sum, and clarify that the path can bend at a node (using both children) but cannot branch further when returning to the parent.

1. Clarify problem and constraints

Confirm that a path can start and end at any nodes, must contain at least one node, and that node values can be negative. Ask about tree size and whether recursion depth is a concern.

2. Define recursive function

Define a helper function that returns the maximum gain from the current node down to any node in its subtree, considering only one branch. This gain is the node's value plus the maximum of 0, left gain, and right gain.

3. Update global maximum

At each node, compute the best path sum that passes through the node by adding the node's value to the positive gains from left and right subtrees. Update a global maximum with this value.

4. Handle base cases and recursion

For null nodes, return 0. Recursively compute left and right gains, then apply the logic from steps 2 and 3. Ensure the global maximum is updated before returning the gain.

5. Analyze complexity and edge cases

State that time complexity is O(n) and space complexity is O(h) for recursion stack. Discuss edge cases like all negative values, single node, and skewed trees.

Key Points to Mention

  • Post-order traversal (DFS) to process children before parent
  • Global variable to track maximum path sum
  • Ignoring negative gains by taking max(0, gain)
  • Path can bend at a node but cannot branch when returning to parent
  • Time complexity O(n) and space complexity O(h)
  • Handling edge cases: all negative values, single node, skewed tree

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