Determine whether player 1 wins an optimal game of taking numbers from either end of an array

Quick Overview

Decide whether the first player can finish with at least as many points as the second when both players take numbers from either end of an array and play optimally. It tests interval dynamic programming over subarrays and modeling a two-player zero-sum game as a score difference.

Determine whether player 1 wins an optimal game of taking numbers from either end of an array

Company: Nokia

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer array `nums`. Two players, player 1 and player 2, play a game with this array, and player 1 moves first. Both players start with a score of 0. On each turn, the current player takes one number from either end of the array (`nums[0]` or `nums[len(nums) - 1]`), which removes it from the array, and adds it to their score. The game ends when no numbers remain. Return `True` if player 1 can win the game. If the two scores are equal, player 1 is still the winner, so return `True` in that case too. Assume both players play optimally. ### Function Signature ```python def predict_the_winner(nums: list[int]) -> bool: ``` ### Rules - Player 1 wins when their final score is greater than or equal to player 2's final score. - Playing optimally means each player chooses the move that maximizes their own final score, knowing the other player does the same. ### Constraints - `1 <= len(nums) <= 20` - `0 <= nums[i] <= 10^7` ### Examples ```text Input: nums = [1, 5, 2] Output: False ``` Player 1 can take 1 or 2. Either way player 2 takes 5, and player 1 gets the last number. Player 1 ends with 3 and player 2 with 5. ```text Input: nums = [1, 5, 233, 7] Output: True ``` Player 1 takes 1. Player 2 must take 5 or 7, and in both cases 233 is then at an end for player 1 to take. Player 1 ends with 234 against player 2's 12. ```text Input: nums = [1, 2, 1] Output: True ``` Both ends are 1, so whatever player 1 does, player 2 gets the 2. The scores finish 2 to 2, and a tie counts as a win for player 1.

Overview: Decide whether the first player can finish with at least as many points as the second when both players take numbers from either end of an array and play optimally. It tests interval dynamic programming over subarrays and modeling a two-player zero-sum game as a score difference.

Nokia logo
Nokia
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array nums. Two players, player 1 and player 2, play a game with this array, and player 1 moves first. Both players start with a score of 0.

On each turn, the current player takes one number from either end of the array (nums[0] or nums[len(nums) - 1]), which removes it from the array, and adds it to their score. The game ends when no numbers remain.

Return True if player 1 can win the game. If the two scores are equal, player 1 is still the winner, so return True in that case too. Assume both players play optimally.

Function Signature

def predict_the_winner(nums: list[int]) -> bool:

Rules

  • Player 1 wins when their final score is greater than or equal to player 2's final score.
  • Playing optimally means each player chooses the move that maximizes their own final score, knowing the other player does the same.

Constraints

  • 1 <= len(nums) <= 20
  • 0 <= nums[i] <= 10^7

Examples

Input: nums = [1, 5, 2]
Output: False

Player 1 can take 1 or 2. Either way player 2 takes 5, and player 1 gets the last number. Player 1 ends with 3 and player 2 with 5.

Input: nums = [1, 5, 233, 7]
Output: True

Player 1 takes 1. Player 2 must take 5 or 7, and in both cases 233 is then at an end for player 1 to take. Player 1 ends with 234 against player 2's 12.

Input: nums = [1, 2, 1]
Output: True

Both ends are 1, so whatever player 1 does, player 2 gets the 2. The scores finish 2 to 2, and a tie counts as a win for player 1.