Solve Two Array Optimization Problems
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This pair of problems evaluates array manipulation, partitioning, and sum-optimization competencies, focusing on handling element removals, contiguous partitions, and maximizing subarray sums.
Part 1: Split a Gold Chain Fairly
Constraints
- 0 <= len(weights) <= 2000
- -10^9 <= weights[i] <= 10^9
Examples
Input: ([1, 2, 1],)
Expected Output: True
Explanation: Remove index 1 to get [1, 1]. Cutting after k = 0 gives two parts with sums 1 and 1.
Input: ([1, 3, 2, 2],)
Expected Output: False
Explanation: No choice of removed link and cut position produces two equal-weight non-empty parts.
Input: ([1, 1, 1],)
Expected Output: True
Explanation: Removing any one link leaves [1, 1], which splits equally at k = 0.
Input: ([5, 5],)
Expected Output: False
Explanation: After removing one link, only one link remains, so you cannot cut into two non-empty parts.
Hints
- Use prefix sums so you can compute the weight of a left segment in O(1) time after a hypothetical removal.
- For a fixed removed index `r`, a cut index `k` maps differently depending on whether `k < r` or `k >= r`.
Part 2: Split a Gold Chain Fairly - Return All Valid Pairs
Constraints
- 0 <= len(weights) <= 2000
- -10^9 <= weights[i] <= 10^9
- The number of valid answers can be O(n^2)
Examples
Input: ([1, 2, 1],)
Expected Output: [(1, 0)]
Explanation: Only removing index 1 leaves [1, 1], and cutting after k = 0 is balanced.
Input: ([1, 1, 1],)
Expected Output: [(0, 0), (1, 0), (2, 0)]
Explanation: Any removed index leaves [1, 1], and the only cut k = 0 works.
Input: ([0, 0, 0, 0],)
Expected Output: [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)]
Explanation: Every removal and every possible cut produces equal sums because all sums are 0.
Input: ([2, 2, 2, 2],)
Expected Output: []
Explanation: After any removal, the remaining total is 6, but no cut produces left sum 3.
Input: ([7, 7],)
Expected Output: []
Explanation: After removing one link, only one element remains, so no valid cut exists.
Hints
- The follow-up only changes what you return, not the core check for whether a specific `(r, k)` works.
- For each removed index `r`, scan every possible cut `k` in the reconnected chain and compute the left sum with prefix sums.
Part 3: Maximum-Sum Subarray with Equal Endpoints
Constraints
- 0 <= len(a) <= 200000
- -10^9 <= a[i] <= 10^9
Examples
Input: ([2, -1, 2],)
Expected Output: (0, 2)
Explanation: The endpoints match and the subarray sum is 3, which is better than any single-element choice.
Input: ([5, -10, 5, 5],)
Expected Output: (2, 3)
Explanation: The best valid subarray is [5, 5] from indices 2 to 3, with sum 10.
Input: ([3, 1, 4],)
Expected Output: (2, 2)
Explanation: No longer subarray has equal endpoints, so the best choice is the single element 4.
Input: ([5, -10, 5],)
Expected Output: (0, 0)
Explanation: The best sum is 5, achieved by both (0, 0) and (2, 2); tie-breaking chooses the smaller i.
Input: ([],)
Expected Output: None
Explanation: There is no valid pair in an empty array.
Hints
- The sum from `i` to `j` can be written as `prefix[j+1] - prefix[i]`.
- For each value `x`, while scanning from left to right, keep the occurrence of `x` with the smallest prefix sum before it.
Part 4: Maximum-Sum Subarray with Equal Endpoints Using O(1) Extra Space
Constraints
- 0 <= len(a) <= 5000
- -10^9 <= a[i] <= 10^9
Examples
Input: ([2, -1, 2],)
Expected Output: (0, 2)
Explanation: The best valid subarray has equal endpoints 2 and sum 3.
Input: ([5, -10, 5, 5],)
Expected Output: (2, 3)
Explanation: The subarray from 2 to 3 has equal endpoints and sum 10, which is optimal.
Input: ([3, 1, 4],)
Expected Output: (2, 2)
Explanation: Only length-1 subarrays are valid here, and index 2 gives the largest sum.
Input: ([5, -10, 5],)
Expected Output: (0, 0)
Explanation: The best sum is 5, and tie-breaking prefers the earlier index.
Input: ([],)
Expected Output: None
Explanation: There is no valid answer for an empty array.
Hints
- Without a hashmap, you can still try every starting index `i` and extend `j` to the right with a running sum.
- A subarray is only a candidate when `a[j] == a[i]`, but you do not need extra arrays to keep track of sums.