Check pivot after removing one element
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates array-manipulation skills, cumulative-sum/prefix-sum reasoning, and robustness in handling negative values and duplicates, testing algorithmic problem-solving competency in the Coding & Algorithms domain and focusing on practical application rather than purely conceptual understanding.
Constraints
- `2 <= len(nums) <= 2 * 10^5`
- `-10^9 <= nums[i] <= 10^9`
- The total sum can exceed 32-bit integer range, so sums should be handled with 64-bit arithmetic.
Examples
Input: ([1, 2],)
Expected Output: True
Explanation: Remove either element. The remaining single-element array has index 0 as a pivot because both side sums are 0.
Input: ([2, 1, 1, 2],)
Expected Output: True
Explanation: Remove one of the middle `1`s to get `[2, 1, 2]`. Index 1 is a pivot because the left sum and right sum are both 2.
Input: ([2, 1, 3, 1, 2],)
Expected Output: False
Explanation: No matter which single element is removed, the resulting 4-element array has no pivot index.
Input: ([1, 3, 2, 1, 2],)
Expected Output: True
Explanation: Remove the first `1` to get `[3, 2, 1, 2]`. Index 1 is a pivot because the left sum is 3 and the right sum is also 3.
Input: ([0, -1, 1, 0],)
Expected Output: True
Explanation: Remove `-1` to get `[0, 1, 0]`. Index 1 is a pivot because the left sum and right sum are both 0.
Input: ([3, -1, 2],)
Expected Output: False
Explanation: Removing any one element leaves a 2-element array, and none of the possible results has equal left and right sums at any index.
Hints
- Fix an index `i` that stays in the array and imagine it becomes a pivot after one removal. Compare `left_sum` and `right_sum` around `i` before removing anything.
- If `left_sum - right_sum = d`, then removing a value `d` from the left side or a value `-d` from the right side would balance the two sides. Use frequency maps while scanning.