Fewest Moves to Rebalance Cache Tokens Across a Server Tree
Last updated: November 29, 2026
Quick Overview
Servers form a binary tree with n nodes and n tokens spread across them. One move shifts one token along an edge. Return the minimum moves so every server holds exactly one token.
Meta
November 29, 20260
0
0 solved
Servers form a binary tree with n nodes and n tokens spread across them. One move shifts one token along an edge. Return the minimum moves so every server holds exactly one token.
A Meta onsite tree problem with a load balancing theme. The insight the interviewer is after is that each edge must carry exactly the net surplus of the subtree below it, so the answer is a sum of absolute flows computed in one post-order pass.
What the Interviewer Expects
- Define a subtree's excess as tokens minus nodes
- Observe the edge above a subtree carries abs(excess) moves
- Compute excess bottom-up
- Sum absolute flows over all edges
- Explain why no strategy can do better
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Why is the per-edge count a lower bound and also achievable?
- What if each server should hold its capacity rather than exactly one?
- What if moves along some edges cost more than others?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is a binary tree root where node.val is the token count, and the total token count equals the node count. Return the minimum number of single-edge token moves to reach one token per node....
High-Level Design
For a subtree, let excess = tokens - nodes. Positive means it must export that many tokens across the edge to its parent; negative means it must import. Either way, at least abs(excess) moves cros...