Replace Each BST Score With Its Running Sum From the Top

Last updated: November 29, 2026

Quick Overview

Given a BST of ranking scores, replace each node's value with the sum of all values in the tree greater than or equal to it, in place.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a BST of ranking scores, replace each node's value with the sum of all values in the tree greater than or equal to it, in place.

A Meta technical-screen BST question where the trick is to traverse in reverse in-order (right, node, left) and carry a running sum. Interviewers check that you do not recompute subtree sums per node.

What the Interviewer Expects
  • Identify reverse in-order traversal as descending order
  • Carry a single running sum across the traversal
  • Update values in place
  • Handle negatives and empty trees
  • State O(n) time, O(h) space
Key Topics to Cover
BST
reverse in-order traversal
running sum
in-place update
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Do it with O(1) extra space (Morris traversal).
  • Return a new tree without modifying the input.
  • What if the tree has duplicate values?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: BST root with distinct integer values (clarify duplicates).
  • Output: the same tree with node.val = sum of all original values >= node.val.
  • Structure must not change....
High-Level Design

Visiting nodes from largest to smallest makes the answer a prefix sum: when we reach a node, we have already seen exactly the values larger than it.

Reverse in-order (right subtree, node, left subtre...


Submit Your Answer
Markdown supported