Card Game: Best First-Player Score
Updated
Problem statement
A row of cards lies on a table. You are given the row as the array cards: cards[0] is on the far left, and the last element is on the far right. Each card shows a positive whole number, and the row has an even number of cards.
Two players take turns, starting with the first player. On a turn, a player picks up the card at either end of the row and adds its number to their own total. Both totals start at 0, and the game ends when no cards are left.
Both players can see every card from the start, and each one always plays to finish with the highest total they can. Return the first player's total at the end of the game.
Function
maxScore(cards: int[]) → longExamples
Example 1
cards = [1,9,10,5,6,4]return = 18The cards add up to 35. One best line of play: the first player takes 4, the second takes 6, the first takes 5, the second takes 10, the first takes 9, and the second takes the last card, 1. The first player ends with 4 + 5 + 9 = 18 and the second with 17. Starting with 1 instead would let the second player hold the first player to 17.
Example 2
cards = [3,9,1,2]return = 11Taking the bigger end first is a trap here. If the first player takes 3, the second player takes 9, and the first player ends with only 3 + 2 = 5. If the first player takes 2, the row is [3, 9, 1] and 9 is in the middle. Whichever end the second player takes, 9 becomes an end card on the first player's turn, so the first player ends with 2 + 9 = 11.
Example 3
cards = [4,1,1,4,2,2]return = 9The cards at even positions add up to 4 + 1 + 2 = 7, and the cards at odd positions also add up to 1 + 4 + 2 = 7, so keeping to one set of positions gives only 7. Choosing well on every turn does better. One best line: the first player takes 4 from the left, the second takes 2 from the right, the first takes 1, the second takes 1, the first takes 4, and the second takes the last 2. The first player ends with 4 + 1 + 4 = 9.
Example 4
cards = [1000000000,1000000000,1000000000,1000000000,1000000000,1000000000]return = 3000000000Each player ends with three cards, so the first player has 3 * 1000000000 = 3000000000. This is more than a 32-bit integer can hold (about 2.1 * 10^9), so the answer needs a 64-bit type.
Constraints
2 <= cards.length <= 1000cards.lengthis even.1 <= cards[i] <= 10^9
More Shopify Online Assessment questions
- Macaron Order with Set DiscountsOA · Seen Nov 2026
- Polynomial CoefficientsOA · Seen Nov 2026
- Choose Items for a BackpackOA · Seen May 2026
- Filter Words Containing a LetterOA · Seen Aug 2025
- Paint HouseOA · Seen Aug 2025
- Longest Increasing SubsequenceOA · Seen May 2025
- Find the Compromised ServerOA · Seen Apr 2025
- Fix the First Robot InstructionOA · Seen Dec 2024
- Validate a Directed GraphOA · Seen Mar 2018
- Min StackOA
Similar questions at other companies
- Maximum Game Score with Fixed Forward JumpsStripe · OAStripe interview questions
- 0/1 KnapsackConfluent · OAConfluent interview questions
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3Infosys · OAInfosys interview questions
- Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
- Maximum Subarray SumStraive · OA
- Longest Increasing Price SubsequenceOpenAI · OAOpenAI interview questions
- Lowest-Cost Shirt Color ScheduleElevenLabs · OAElevenLabs interview questions
- Maximum Game Board ScoreJP Morgan Chase · OAJP Morgan Chase interview questions
- API Bandwidth AllocationArista Networks · OAArista Networks interview questions
- Maximum Points by Deleting ElementsIBM · OAIBM interview questions