Ball picking game: take one or two from the front
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Google. It tests dynamic programming, game theory and minimax.
Balls stand in a single line. Each ball has an integer value, which may be positive, zero, or negative. Two players take turns, and player 1 moves first. Both start with a score of 0.
On each turn, the player to move must take either one or two balls from the front of the line and adds their values to their score. A player cannot pass, so when one ball is left the player to move must take it, even if its value is negative. The game ends when the line is empty.
Both players play optimally, and each wants to maximize their own final score.
Implement:
def final_scores(balls: list[int]) -> tuple[int, int]:
"""Return (player 1 score, player 2 score) under optimal play."""
def first_player_wins(balls: list[int]) -> bool:
"""Return True if player 1 ends with at least as many points as player 2."""
Constraints
0 <= len(balls) <= 10^5-10^4 <= balls[i] <= 10^4
Example 1
balls = [2, -1, 6]
final_scores(balls) -> (2, 5)
first_player_wins(balls) -> False
If player 1 takes 2, player 2 takes -1, 6 for 5. If player 1 takes 2, -1 (1 point), player 2 takes 6. Player 1 picks the first line, and still loses 2 to 5.
Example 2
balls = [4, -9, 1, 3]
final_scores(balls) -> (7, -8)
Player 1 takes 4. Player 2 must now start with -9, and takes -9, 1. Player 1 takes 3.
Be ready to explain how your answer changes if a player may take up to k balls per turn, or may take from either end of the line.
Clarifying questions
- With an empty line, are both scores 0, and does player 1 "win" with a tie at 0?
- Does "each player maximizes their own score" give the same play as maximizing the difference between the two scores? (It does here, since every ball goes to someone.)
- Can the values be large enough that the scores need more than 32 bits?
- Should
final_scoresreturn the scores of one optimal line of play, if several give the same scores?
What a strong answer covers
- A dynamic program from the back:
best[i]is the most the player to move can score from ballsi..n-1. Taking one ball givesballs[i] + (suffix[i + 1] - best[i + 1]), taking two givesballs[i] + balls[i + 1] + (suffix[i + 2] - best[i + 2]), andbest[i]is the larger. - Uses suffix sums so the opponent's best and the player's remainder come out in O(1). That is O(n) time and O(1) extra space with rolling variables.
- Player 1's score is
best[0]and player 2's istotal - best[0];first_player_winscompares them. - Edge cases: an empty line, one ball (forced, even if negative), two balls, all negative values, and zeros.
- Tests: both examples, a brute-force minimax on small random lines, and a long line to check it runs in linear time without recursion.
Follow-ups
- A player may take up to
kballs per turn: the same recurrence overkchoices, kept at O(n) with a sliding window maximum. - Balls can be taken from either end: a dynamic program over intervals, O(n^2).
- Return the moves of an optimal line of play.
- Three or more players.
Where it's asked
What it tests
dynamic programming · game theory · minimax
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
