Implement range-sum tree and sort merged lists
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: Implement range-sum tree and sort merged lists evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Range Sum of a Binary Tree
Constraints
- 0 <= number of nodes <= 10^4
- -10^5 <= node value <= 10^5
- -10^5 <= low <= high <= 10^5
- The tree is a general binary tree, not necessarily a BST.
- Use a 64-bit accumulator if node count and magnitudes can make the sum overflow 32-bit.
Examples
Input: ([10, 5, 15, 3, 7, None, 18], 7, 15)
Expected Output: 32
Explanation: Nodes are 10,5,15,3,7,18. Those in [7,15] are 10, 15, 7 -> 32.
Input: ([10, 5, 15, 3, 7, 13, 18, 1, None, 6], 6, 10)
Expected Output: 23
Explanation: Nodes in [6,10] are 10, 7, 6 -> 23.
Input: ([], 1, 100)
Expected Output: 0
Explanation: Empty tree sums to 0.
Input: ([5], 1, 10)
Expected Output: 5
Explanation: Single node 5 is within [1,10].
Input: ([5], 6, 10)
Expected Output: 0
Explanation: Single node 5 is outside [6,10].
Input: ([-10, -20, -5, -30, -15], -20, -5)
Expected Output: -50
Explanation: Nodes in [-20,-5] are -10,-20,-5,-15 (-30 excluded) -> -50.
Input: ([1, 1, 1, 1, 1], 1, 1)
Expected Output: 5
Explanation: All five duplicate values 1 are within [1,1].
Hints
- Because the tree is a general binary tree (not a BST), you cannot prune any subtree — you must inspect every node.
- A simple DFS or BFS that adds v whenever low <= v <= high works in O(n).
- If the input were a BST, you could skip the left subtree when a node's value < low, and the right subtree when value > high, turning each lookup into O(h) for the boundary walk.
Merge and Sort Two Unsorted Linked Lists
Constraints
- 0 <= len(list1), len(list2) <= 10^4
- -10^9 <= node value <= 10^9
- Duplicate values are allowed and must all be preserved.
- Either or both lists may be empty.
- Inputs are NOT guaranteed to be sorted, so a single linear two-pointer merge is insufficient on its own.
Examples
Input: ([3, 1, 2], [6, 4, 5])
Expected Output: [1, 2, 3, 4, 5, 6]
Explanation: All six values sorted ascending.
Input: ([], [])
Expected Output: []
Explanation: Both lists empty -> empty result.
Input: ([5], [])
Expected Output: [5]
Explanation: Second list empty; single value returned.
Input: ([], [7, 2])
Expected Output: [2, 7]
Explanation: First list empty; remaining values sorted.
Input: ([4, 4, 1], [4, 2])
Expected Output: [1, 2, 4, 4, 4]
Explanation: All three 4's are preserved (duplicates kept).
Input: ([-3, 0, 2], [-5, 1, 1])
Expected Output: [-5, -3, 0, 1, 1, 2]
Explanation: Negatives and duplicate 1's sorted ascending.
Input: ([1, 2, 3], [4, 5, 6])
Expected Output: [1, 2, 3, 4, 5, 6]
Explanation: Already-disjoint sorted ranges concatenate in order.
Hints
- The lists are unsorted, so you must order all values — collect every value from both lists, then sort.
- Total order is the multiset union of both lists' values; duplicates stay.
- If the lists were already sorted, you would not sort — instead walk two pointers and splice the smaller current node each step for O(n + m).