End-Picking Number Game: Can the First Player Win Under Optimal Play?
Company: Waymo
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Two players alternately take a number from either end of a row and add it to their score; decide whether the first player can finish with at least as many points as the second under optimal play. Tests minimax reasoning and memoized game-state search.
Constraints
- 1 <= len(nums) <= 20
- 0 <= nums[i] <= 10^7
- Every total and every score difference fits in a signed 32-bit integer (absolute value at most 2 * 10^8)
Examples
Input: ([2, 9, 4],)
Expected Output: False
Input: ([3, 7, 2, 2],)
Expected Output: True
Hints
- Always grabbing the larger end is not enough: a small grab now can hand your opponent a much larger number next turn.
- Whatever moves have been made, the numbers left always form one contiguous block nums[i..j]. What single value about that block would tell the player to move which end to take?
- Measure a block by the best margin (your total minus your opponent's) the player to move can guarantee on it. After you take one end, your opponent faces a smaller block with the roles swapped.