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.
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.
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).