Optimal Card Game Score
Reported by candidates from Spotnana's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The trap in this Spotnana OA, reported July 2026, is the greedy instinct. Grab the bigger end every turn and you'll fail [1,5,2], where the greedy answer is 2 and the real one is 3. It's a two-player pick-from-either-end game with optimal play, so it's interval DP, not a loop. The report also says the interviewer wanted production-ready code written from scratch with explicit custom tests. If you blank on the recurrence mid-assessment, StealthCoder runs invisibly on your desktop and gives you a solution as a safety net.
The problem
A quick note: this problem is backed by a real Spotnana backend interview report. The report directly gave the two-player card game, the choice from either end, optimal play, and Alice's objective, but it did not include an exact function interface, examples, or input limits. The core task match is about 95%. Alice and Bob play a card game with an array cards. Alice moves first. On each turn, the current player chooses either the first or the last remaining card and adds its value to their own score. Both players play optimally. Return the maximum total score Alice can obtain. Interview Follow-up The interviewer expected production-ready code written from scratch, including edge-case handling and explicit custom tests. Function maxAliceScore(cards: int[]) → int Examples Example 1 cards = [1,5,2] return = 3 If Alice takes 1, Bob takes 5, then Alice gets 2. If Alice takes 2, Bob takes 5, then Alice gets 1. Alice's best total is 3. Example 2 cards = [1,5,233,7] return = 234 Alice can force the large middle card into her total and finish with 234.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: 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]. Alice's score is (total + dp[0][n-1]) / 2. The pitfall is tracking both players' scores separately, which gets messy. The difference form keeps it clean. Greedy fails on [1,5,2] because taking 2 first hands Bob the 5. Edge cases the interviewer likely probes: empty array returns 0, single card, two cards, negative values if allowed, and odd versus even length. Write your own tests for each. Space can drop to O(n) with a 1D rolling array, but O(n^2) is fine. If the recurrence slips away live, StealthCoder is the hedge that gets you unstuck without anyone seeing it.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Optimal Card Game 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as predict the winner. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Spotnana's OA.
Spotnana reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Optimal Card Game Score FAQ
What's the trick to Optimal Card Game Score?+
Don't think greedy. Use interval DP on score difference. dp[i][j] is the best lead the current player can get on the subarray i..j. Take an end card, then subtract the opponent's best lead on what remains. Alice's score is (sum + dp[0][n-1]) / 2.
Why does greedy fail here?+
Taking the larger end ignores what you expose to the opponent. In [1,5,2], greedy takes 2 first, Bob takes 5, and Alice ends with 3 only if she picks 1 first... actually she gets 3 by taking 1 then 2. Greedy gives 2 plus 1, so it's about future access.
What edge cases should I test?+
Empty array, one card, two cards, all equal values, odd and even lengths, and the given examples [1,5,2] and [1,5,233,7]. The report says the interviewer expected explicit custom tests, so write them out in code and not just in your head.
What's the complexity, and can I optimize it?+
Time is O(n^2) and space is O(n^2) with a full table. You can cut space to O(n) by iterating by interval length and keeping one row. Mention this as a follow-up, but get the correct 2D version working first.
How do I prepare for this in 48 hours?+
Implement the difference-based interval DP from scratch twice, without looking. Then write five custom tests and trace the second example by hand. Also learn the memoized recursive version, since it's easier to reconstruct under pressure.