The Predict the Winner problem gives you an array of numbers. Two players alternately pick a number from either end. The player who picks the number gains its value. Determine if Player 1 can guarantee a win (score ≥ Player 2's score). This Predict the Winner coding problem is a game theory DP problem where each player plays optimally. The array, math, recursion, game theory, and dynamic programming interview pattern is the core.
Cisco, Uber, Microsoft, Meta, Amazon, Google, and Bloomberg ask this because it tests minimax reasoning — each player optimizes their own score while minimizing the opponent's gain. The DP solution avoids exponential recursion through memoization.
Interval DP (minimax). dp[i][j] = maximum score difference (current player's score minus opponent's score) for the subarray nums[i..j]. Transition: dp[i][j] = max(nums[i] - dp[i+1][j], nums[j] - dp[i][j-1]). Player 1 wins if dp[0][n-1] >= 0.
The recurrence reflects: take nums[i] and the opponent plays optimally on [i+1..j] (giving them advantage dp[i+1][j], so your net is nums[i] - dp[i+1][j]).
nums=[1,5,2]. dp[0][0]=1, dp[1][1]=5, dp[2][2]=2. dp[0][1]=max(1-5, 5-1)=max(-4,4)=4 (Player takes 5). dp[1][2]=max(5-2, 2-5)=3 (Player takes 5). dp[0][2]=max(1-dp[1][2], 2-dp[0][1])=max(1-3, 2-4)=max(-2,-2)=-2. Player 1 cannot win. Return false.
Predict the Winner introduces the "score difference" DP formulation for two-player games. The key insight: instead of tracking both players' scores separately, track the score difference. The current player maximizes this difference; the recurrence naturally handles both players' optimal play. Practice this on "Stone Game" variants — they all use the same interval DP with score-difference formulation.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Stone Game | Medium | Solve | |
| Stone Game VII | Medium | Solve | |
| Stone Game III | Hard | Solve | |
| Stone Game V | Hard | Solve | |
| Stone Game II | Medium | Solve |