Key Sum Management
Updated
Problem statement
Key 1 is the root of an initial tree with n keys. Every key starts with value 0, and a key's level is its distance from the root.
The first n - 1 rows of operations contain the two endpoints of the initial tree edges. The remaining q rows are operations:
Ro p v: add a new keyvas a child ofp. Its value starts at0, andvis the current node count plus one.Re level z: for every keyucurrently at that exact level, letxbeu's value immediately before this operation. Assignx + ztouand every key currently inu's subtree.
How the tree changes
1. Initial tree
| Level | Left subtree | Middle | Right subtree |
|---|---|---|---|
| 0 | Key 1 · value X1 | ||
| 1 | Key 2 · X2 | — | Key 3 · X3 |
| 2 | Key 4 · X4, Key 5 · X5 | — | Key 6 · X6, Key 7 · X7 |
| 3 | Key 8 · X8 under key 4 | — | — |
2. Rotation
Ro 1 9adds key9as a new child of key1. Its value starts at0; every existing edge and value stays unchanged.
| Level | Left subtree | New middle key | Right subtree |
|---|---|---|---|
| 0 | Key 1 · value X1 | ||
| 1 | Key 2 · X2 | Key 9 · 0 | Key 3 · X3 |
| 2 | Keys 4, 5 · unchanged | — | Keys 6, 7 · unchanged |
| 3 | Key 8 · unchanged | — | — |
3. Rekey
Re 1 4starts at every level-1key. Each selected key's previous value plus4is copied through its whole subtree.
| Level | Key 2 subtree | Key 9 subtree | Key 3 subtree |
|---|---|---|---|
| 0 | Key 1 stays X1 | ||
| 1 | Key 2 · X2 + 4 | Key 9 · 4 | Key 3 · X3 + 4 |
| 2 | Keys 4, 5 · X2 + 4 | — | Keys 6, 7 · X3 + 4 |
| 3 | Key 8 · X2 + 4 | — | — |
Return the sum of all key values after all operations.
Function
keySumManagement(operations: String[][], n: int, q: int) → longExamples
Example 1
operations = [["1", "2"], ["2", "4"], ["1", "3"], ["4", "5"], ["Re", "1", "10"], ["Ro", "4", "6"], ["Re", "2", "4"], ["Re", "3", "4"]]n = 5q = 4return = 70The final values of keys 1 through 6 are [0, 10, 10, 14, 18, 18], whose sum is 70.
Example 2
operations = [["2", "1"], ["3", "1"], ["4", "2"], ["5", "1"], ["Ro", "4", "6"], ["Re", "2", "91277"], ["Re", "1", "50944"], ["Ro", "1", "7"], ["Re", "1", "17666"]]n = 5q = 5return = 360716After the final Rekey operation, keys 2 through 6 each have value 68610, key 7 has value 17666, and key 1 remains 0. Their sum is 360716.
Constraints
- 1 ≤ n, q ≤ 10^5
- 1 ≤ z ≤ 10^5, where z is the number added in the Rekey query.
- All other inputs satisfy the constraints and problem requirements.
- In the Rotation query, the new key v always equals the current number of nodes in the tree + 1.
More Rubrik interview questions
- Colored Rooted TreeONSITE INTERVIEW · Seen Nov 2026
- Delete and Freeze GameOA · Seen Nov 2026
- Delete K Subsets for Maximum ScoreOA · Seen Nov 2026
- Distribute Elements Into Two Arrays IIOA · Seen Nov 2026
- Minimum Replacements to Sort the ArrayOA · Seen Nov 2026
- All Balanced Strings from Minimum Parenthesis InsertionsONSITE INTERVIEW · Seen Nov 2026
- Insert into a Left-Complete Binary TreeONSITE INTERVIEW · Seen Oct 2026
- Thread-Safe Request Counts by AttributeONSITE INTERVIEW · Seen Oct 2026
- Minimum Swaps To Binary PalindromeOA · Seen Apr 2026
- Get Minimum OperationsOA · Seen Nov 2024
Similar questions at other companies
- Count Palindromic Ancestor PathsUber · OAUber interview questions
- Decision Tree Regression from ScratchNVIDIA · OANVIDIA interview questions
- Maximum Sum of a k-Node Downward PathHudson River Trading · OAHudson River Trading interview questions
- Maximum-Scoring Consecutive Tree LevelsJane Street · PHONE SCREENJane Street interview questions
- Repeated BST Range Sum and AverageMeta · ONSITE INTERVIEWMeta interview questions
- Lowest Common Ancestor with a Changing RootInfosys · OAInfosys interview questions
- Palindromic Paths to the RootRippling · OARippling interview questions
- Minimum Tree Diameter After Removing K LeavesMathWorks · OAMathWorks interview questions
- Part 2: Batch Access ReviewScale AI · OAScale AI interview questions
- Maximum Signal Walk in a Server TreeTower Research Capital · OA