Optimal Card Game Score
Learn this problemProblem statement
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[]) → intExamples
Example 1
cards = [1,5,2]return = 3If 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 = 234Alice can force the large middle card into her total and finish with 234.