Determine Whether Queries Can Zero Array
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates understanding of range-based array operations and feasibility under bounded per-index decrements, testing skills in interval reasoning, resource allocation across ranges, and constraint propagation.
Constraints
- 0 <= n <= 2 * 10^5
- 0 <= len(queries) <= 2 * 10^5
- 0 <= nums[i] <= 10^9
- 0 <= val <= 10^9
- If n > 0, then every query satisfies 0 <= l <= r < n
Examples
Input: ([2, 0, 2], [[0, 2, 1], [0, 2, 1], [1, 1, 3]])
Expected Output: True
Explanation: Indices 0 and 2 are covered by two queries of value 1, giving total capacity 2 each. Index 1 already starts at 0. So every index can be reduced to exactly 0.
Input: ([4, 1, 3], [[0, 1, 2], [1, 2, 1]])
Expected Output: False
Explanation: Index 0 can be decreased by at most 2 total, but it needs 4. Therefore it is impossible.
Input: ([], [])
Expected Output: True
Explanation: An empty array is already all zeros.
Input: ([5], [[0, 0, 2], [0, 0, 3]])
Expected Output: True
Explanation: The single element receives total decrement capacity 2 + 3 = 5, which is exactly enough.
Input: ([0, 2, 0, 1], [[1, 1, 1], [1, 2, 1]])
Expected Output: False
Explanation: Index 3 is never covered by any query, so it cannot be reduced from 1 to 0.
Input: ([1, 3, 2, 1], [[0, 1, 1], [1, 3, 2], [2, 2, 1]])
Expected Output: True
Explanation: The total capacities are [1, 3, 3, 2], which are each at least the corresponding values [1, 3, 2, 1].
Hints
- For each index, you only need to know the total decrement capacity provided by all queries that cover it.
- Use a difference array to add `val` to every range `[l, r]` in O(1) per query, then recover per-index totals with a prefix sum.