FastPrepOptimal First-Player Card Score
Problems/Optimal First-Player Card Score

Optimal First-Player Card Score

OpenAI logoOpenAI● MediumFULLTIMENEW GRADOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source supplies the even positive card sequence, two optimal end picks, and first-player score; only safe size and value bounds are completed for practice. The judged core task is about a 99% match.

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.

More OpenAI problems

  • Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
  • Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
All OpenAI interview questionsSee OpenAI hiring insights
Saved
class Solution {
    public long maxScore(int[] cards) {
        // write your code here
    }
}
Checking account…