Apply Range Overwrite Queries
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's competency in array manipulation and range-update algorithms, focusing on efficient handling of batch assignments and overlapping updates.
Constraints
- 0 <= n <= 200000
- 0 <= q <= 200000, where q = len(queries)
- -10^9 <= nums[i], value <= 10^9
- For every query `(left, right, value)`, `0 <= left <= right < n` when `n > 0`
Examples
Input: ([1, 2, 3, 4, 5], [(1, 3, 9), (2, 4, 7)])
Expected Output: [1, 9, 7, 7, 7]
Explanation: After `(1,3,9)`, the array becomes `[1, 9, 9, 9, 5]`. After `(2,4,7)`, positions 2 through 4 are overwritten, giving `[1, 9, 7, 7, 7]`.
Input: ([5, 6, 7], [])
Expected Output: [5, 6, 7]
Explanation: There are no queries, so the array stays unchanged.
Input: ([0, 0, 0, 0], [(0, 3, 1), (1, 2, 5), (2, 2, -3)])
Expected Output: [1, 5, -3, 1]
Explanation: Apply the queries in order: `[1,1,1,1]`, then `[1,5,5,1]`, then `[1,5,-3,1]`.
Input: ([], [])
Expected Output: []
Explanation: An empty array with no queries remains empty.
Input: ([8, 8, 8, 8, 8, 8], [(0, 5, 1), (2, 4, 2), (1, 3, 3), (5, 5, 9)])
Expected Output: [1, 3, 3, 3, 2, 9]
Explanation: The full-array overwrite is partially replaced by later queries, and the last query changes only index 5 to 9.
Hints
- Try processing the queries from last to first. The first time you assign an index in reverse order, you already know its final value.
- Use a disjoint-set / union-find style 'next unfilled index' structure so you can skip positions that have already been finalized.