Optimal First-Player Card Score
Reported by candidates from OpenAI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The OpenAI OA reported in September 2026 looks like a friendly card game, and it's a trap if you reach for the obvious move. Two players pick from either end of an even-length array, both play optimally, and you return the first player's score. The hinted pattern is greedy, but greedy is exactly what fails here. This is interval dynamic programming. If you've got the OA coming up in the next day or two, learn the recurrence below. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.
The problem
Two players play a game with an even-length array cards of positive values. Both players see the full sequence and play optimally. The players alternate turns, and the first player moves first. On each turn, the current player removes either the leftmost or the rightmost remaining card and adds its value to their score. Return the final score of the first player. Function maxScore(cards: int[]) → long Examples Example 1 cards = [1,9,10,5,6,4] return = 18 Optimal play gives the first player a final score of 18. Any move is evaluated against the opponent's best reply. Example 2 cards = [4,7,2,9] return = 16 The first player takes 9. Whatever end the second player chooses, the first player then takes 7. Constraints 2 <= cards.length <= 2000 cards.length is even. 1 <= cards[i] <= 10^9 The answer fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The edge case that breaks the naive solution: always grabbing the larger end card. Take [4,7,2,9]. Greedy takes 9, then the opponent takes 7 and you get 2 or 4. Your total is 11 or 13, not 16. Wait, check it against the expected output of 16: you take 9, the opponent picks between 4 and 7, and you take the other end card left over. The point is that the opponent's reply matters, so you can't decide locally. Define dp[i][j] as the best score difference (current player minus opponent) on cards i..j. Then dp[i][j] = max(cards[i] - dp[i+1][j], cards[j] - dp[i][j-1]). Base case dp[i][i] = cards[i]. With total S and difference D, the first player's score is (S + D) / 2. That's O(n^2) for n up to 2000, which is fine. Use 64-bit ints, since values reach 10^9. StealthCoder is the hedge if the recurrence slips away live.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Optimal First-Player Card Score cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as stone game. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass OpenAI's OA.
OpenAI reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Optimal First-Player Card Score FAQ
What's the trick in the OpenAI Optimal First-Player Card Score problem?+
Stop thinking greedy. Model the game as a score difference over a subarray. dp[i][j] is the best (mine minus theirs) on cards i..j, and you pick an end then subtract the opponent's best result on what remains. Convert the difference to a score at the end using the total sum.
Why does greedy fail on this one?+
Taking the bigger end card ignores what you expose to the opponent. A big card can sit right behind a small one, so grabbing the small one lets the opponent take the big one. Optimal play needs both players' replies evaluated, which is what the interval DP does.
How do I get the first player's score from the DP result?+
If dp[0][n-1] is the difference (first minus second) and S is the sum of all cards, then first plus second equals S. So first = (S + diff) / 2. Use long, since the sum can reach about 2000 times 10^9.
Will O(n^2) pass for 2000 cards?+
Yes. That's about four million states, each with constant work. You can store a full 2D table of longs or roll it down to a 1D array by iterating on interval length. Either way it's comfortably within what this input size is designed for.
How do I prepare for this in 48 hours?+
Write the interval DP from memory twice. Trace Example 2, [4,7,2,9], by hand to confirm 16. Then do one similar problem, like a stone game variant. Focus on the base case and iteration order, since filling by interval length is where people usually slip.