Ten-Pin Bowling Score Calculator
Medium · Coding & Algorithms · Software Engineer · Onsite
Problem Statement
Write calculate_score(rolls) to compute the final score of a finished ten-pin bowling match from rolls, an ordered list of pin counts knocked down on each throw.
A match spans 10 frames, beginning each frame with 10 standing pins and allowing up to two regular throws:
- Open frame: Two throws knock down fewer than
10pins in total (first + second < 10). That frame contributesfirst + secondpoints. - Spare: Two throws clear all
10pins (first < 10andfirst + second == 10). That frame contributes10plus the pins knocked down on the single next throw. - Strike: All
10pins fall on the frame's opening throw (first == 10), ending frames1through9immediately after one throw. That frame contributes10plus the sum of the next two throws. - Frame 10 bonus throws: Clearing all
10pins in frame10awards extra throws solely to supply pending bonuses — two bonus throws after an opening strike, or one bonus throw after a two-ball spare. Bonus throws never create an11th frame.
Return the integer sum of all 10 frame scores.
Examples
Example 1
Input: rolls = [10, 7, 3, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Output: 40
Frame 1 is a strike (10 + 7 + 3 = 20), Frame 2 is a spare (10 + 4 = 14), Frame 3 is an open frame (4 + 2 = 6), and Frames 4 through 10 knock down 0 pins (20 + 14 + 6 = 40).
Example 2
Input: rolls = [4, 5, 8, 1, 6, 4, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6, 2]
Output: 54
Frames 1 and 2 are open (4 + 5 = 9 and 8 + 1 = 9), Frame 3 is a spare (10 + 3 = 13), Frame 4 is open (3 + 2 = 5), Frames 5 through 9 score 0, and Frame 10 starts with a strike followed by bonus throws 6 and 2 (10 + 6 + 2 = 18), totaling 9 + 9 + 13 + 5 + 18 = 54.
Constraints
12 <= len(rolls) <= 210 <= rolls[i] <= 10for every indexi.rollsalways represents a legal, completed10-frame bowling game.
Step-by-step walkthrough
Restate the rules and why list length varies
We receive
rolls, a flat list of integers (0to10) giving the pins knocked down on each throw of a completed 10-frame bowling game, and we must return the total score across all 10 frames:- Strike (
10on the frame's first roll): scores10 + next two rolls, and the frame uses 1 roll (in frames 1–9). - Spare (
10total across two rolls): scores10 + next one roll, and the frame uses 2 rolls. - Open frame (
< 10total across two rolls): scores the sum of its 2 rolls, with no bonus. - 10th frame bonus balls: a strike in frame 10 grants 2 extra rolls and a spare grants 1 extra roll, solely to supply pending bonuses.
Because strikes use 1 roll while other frames use 2 (and frame 10 can have 2 or 3 rolls),
len(rolls)is not fixed: a perfect game has12rolls, an all-open game has20, and an all-spare game has21. Framekdoes not simply start at2 * k.- Strike (
Why looping over individual rolls fails
A tempting first attempt is
for i, pins in enumerate(rolls): if pins == 10: .... That breaks in two classic ways:[0, 10]is a spare, not a strike: seeing10in the list does not mean it was a strike unless you know whether that10landed on the first ball or the second ball of a frame.- Bonus balls in the 10th frame look like extra frames: in Example 2 (
[..., 10, 6, 2]) or a 12-strike perfect game ([10] * 12), looping until the end ofrollstreats the bonus throws at the end as Frame 11 (or Frame 12), reading out of bounds or inflating the score.
Splitting
rollsinto 10 frame sublists in a first pass works, but when two strikes happen in a row ([10], [10], [4, 2]), Frame 1's two bonus rolls span across two different later frames. Keepingrollsflat makes reading ahead effortless.Key idea: count 10 frames with a frame-start pointer and look-ahead
Instead of looping over rolls, loop exactly 10 times (
for _frame in range(10)) while maintaining a look-ahead pointerroll_indexpointing to the first roll of the current frame:rolls: [ 10, | 7, 3, | 4, 2, | 0, 0 ... ] index: 0 1 2 3 4 F1 |---F2---| |---F3---| Frame 1 (roll_index = 0): rolls[0] == 10 (Strike) score += 10 + rolls[1] + rolls[2] = 10 + 7 + 3 = 20 (peeks at 1 & 2; advances roll_index by 1 -> 1) Frame 2 (roll_index = 1): rolls[1] + rolls[2] == 10 (Spare) score += 10 + rolls[3] = 10 + 4 = 14 (peeks at 3; advances roll_index by 2 -> 3) Frame 3 (roll_index = 3): rolls[3] + rolls[4] == 6 (Open) score += 4 + 2 = 6 (no peek; advances roll_index by 2 -> 5)Notice the key separation: peeking at
roll_index + 1orroll_index + 2for a bonus does not consume those rolls —roll_indexstill advances by just1(strike) or2(spare/open), so those future rolls get scored normally in their own frames too.Code the three frame cases cleanly
Always test
first_roll == 10before testingfirst_roll + second_roll == 10: - Ifrollscontains[10, 0, ...],first_roll == 10catches the strike first and advancesroll_indexby1(if we checkedfirst + second == 10first,10 + 0 == 10would misclassify the strike as a 2-roll spare!). - Ifrollscontains[0, 10, ...],first_roll == 0skips the strike branch and lands in the spare branch, advancingroll_indexby2.def calculate_score(rolls: List[int]) -> int: total_score = 0 roll_index = 0 for _frame in range(10): first_roll = rolls[roll_index] if first_roll == 10: strike_bonus = rolls[roll_index + 1] + rolls[roll_index + 2] total_score += 10 + strike_bonus roll_index += 1 else: second_roll = rolls[roll_index + 1] frame_pins = first_roll + second_roll if frame_pins == 10: spare_bonus = rolls[roll_index + 2] total_score += 10 + spare_bonus roll_index += 2 else: total_score += frame_pins roll_index += 2 return total_scoreWhy the 10th frame works automatically — trace Example 1 and Example 2
Does Frame 10 need special
if frame == 9:logic? Check all three possibilities when_frame == 9: - Strike in Frame 10 ([..., 10, b1, b2]): adds10 + b1 + b2(rolls[roll_index + 1] + rolls[roll_index + 2]), which is the exact 10th-frame score. - Spare in Frame 10 ([..., r1, r2, b1]): adds10 + b1(rolls[roll_index + 2]), exact again. - Open Frame 10 ([..., r1, r2]): addsr1 + r2without readingroll_index + 2, exact again.Because
for _frame in range(10)stops right after_frame == 9, the bonus rollsb1andb2are never visited as a frame start!Trace 1 — Example 1: [10, 7, 3, 4, 2, 0, ..., 0] (19 rolls) Frame 1 (i=0): 10 (Strike) -> + (10 + 7 + 3) = 20, next i = 1 Frame 2 (i=1): 7 + 3 == 10 (Spare)-> + (10 + 4) = 34, next i = 3 Frame 3 (i=3): 4 + 2 == 6 (Open) -> + 6 = 40, next i = 5 Frames 4..10 : 0 + 0 == 0 (Open) -> + 0 each = 40 Trace 2 — Example 2: [4, 5, 8, 1, 6, 4, 3, 2, 0, ..., 0, 10, 6, 2] (21 rolls) Frames 1..2 (i=0, 2): Open 4+5 and 8+1 -> 9 + 9 = 18, next i = 4 Frame 3 (i=4) : Spare 6+4 + rolls[6] (3) -> + 13 = 31, next i = 6 Frame 4 (i=6) : Open 3+2 -> + 5 = 36, next i = 8 Frames 5..9 (i=8..16): Open 0+0 each -> + 0 = 36, next i = 18 Frame 10 (i=18) : Strike 10 + rolls[19] + rolls[20] (6 + 2) -> + 18 = 54 Loop ends after 10 iterations; bonus rolls at indices 19 and 20 never start a frame.Complete solution
from typing import Any, Dict, List, Optional, Set, Tuple def calculate_score(rolls: List[int]) -> int: total_score = 0 # roll_index points to the first ball of the current frame inside `rolls` # (advances by 1 after a strike, or by 2 after a spare or open frame). roll_index = 0 # Loop over exactly 10 frames so any bonus balls at the end of the 10th frame # are only read via look-ahead and never scored as an 11th or 12th frame. for _frame in range(10): first_roll = rolls[roll_index] # Check strike FIRST: 10 on the first ball ends the frame immediately (1 roll). # Checking spare first could misclassify [10, 0] as a 2-roll spare. if first_roll == 10: strike_bonus = rolls[roll_index + 1] + rolls[roll_index + 2] total_score += 10 + strike_bonus roll_index += 1 else: second_roll = rolls[roll_index + 1] frame_pins = first_roll + second_roll if frame_pins == 10: # Spare: all 10 pins knocked down across two rolls -> bonus is the next 1 roll. spare_bonus = rolls[roll_index + 2] total_score += 10 + spare_bonus roll_index += 2 else: # Open frame (< 10 pins in two rolls): no look-ahead bonus. total_score += frame_pins roll_index += 2 return total_score✅ Submit now — all tests should pass.
Intuition & how it works
Think of a bowling scorecard as 10 boxes (each box is a frame, one turn at the lane): a strike (10 pins on the frame's first ball) fills its whole box in 1 roll, while a spare (10 pins across two balls) or an open frame (fewer than 10 pins across two balls) uses 2 rolls. Whenever you roll a strike or spare, that box earns a bonus by peeking ahead at the next two rolls or next one roll in the flat rolls list — without skipping over those rolls when the next box begins.
Looping directly over for pins in rolls fails because a 10 on the second ball ([0, 10]) is a spare rather than a strike, and bonus balls thrown at the end of the 10th frame get miscounted as an 11th and 12th frame. Splitting the list into 10 sublists first also gets awkward when consecutive strikes force a bonus to reach across two later frames. Instead, we count 10 frames in a fixed for loop and maintain a look-ahead pointer (roll_index, the array index where the current frame begins) that peeks at roll_index + 1 / roll_index + 2 for bonuses and steps forward by 1 on a strike or 2 otherwise.
Let's trace Example 1 (rolls = [10, 7, 3, 4, 2, 0, ...] with zeros for frames 4–10):
- Frame 1 (roll_index = 0): rolls[0] == 10 (strike), so we add 10 + rolls[1] + rolls[2] = 10 + 7 + 3 = 20 and advance roll_index by 1 to 1.
- Frame 2 (roll_index = 1): rolls[1] + rolls[2] = 7 + 3 = 10 (spare), so we add 10 + rolls[3] = 10 + 4 = 14 (total_score = 34) and advance roll_index by 2 to 3.
- Frame 3 (roll_index = 3): rolls[3] + rolls[4] = 4 + 2 = 6 (open frame), so we add 6 (total_score = 40) and advance roll_index by 2 to 5; frames 4–10 each add 0, finishing at 40.
Complexity
- Time: O(1) for a standard 10-frame game. The loop executes exactly
10iterations, reading at most3list elements and performing constant-time arithmetic on each iteration (for a generalizedN-frame game,O(N)time). - Space: O(1) extra space. We maintain only integer variables (
total_score,roll_index,first_roll,second_roll,frame_pins) and never copy or mutaterolls.
Edge cases
- All gutter balls (
[0] * 20): 10 open frames summing to0. - Perfect game (
[10] * 12): 10 strikes each scoring10 + 10 + 10 = 30for300; stopping after 10 loop iterations ensures rolls10and11are only read as Frame 10's bonus. - All spares (
[5] * 21): 10 spares each scoring10 + 5 = 15for150, reading the 21st roll as Frame 10's spare bonus. 0followed by10([0, 10, 5, 0, ...]):first_roll == 0ensures[0, 10]is scored as a spare (10 + 5), not a strike.- Back-to-back strikes (
[10, 10, 3, 4, ...]): Frame 1 readsrolls[1](10) androlls[2](3) across two subsequent frames for23, because bonuses count the next two rolls, not the next frame.
Common mistakes
- Using
while roll_index < len(rolls)instead offor _frame in range(10): continues past Frame 10 whenever Frame 10 has strike or spare bonus balls, either crashing withIndexErroror inflating the score. - Checking
rolls[i] + rolls[i + 1] == 10beforerolls[i] == 10: when a strike is followed by a0([10, 0, 5, ...]),10 + 0 == 10triggers the spare branch and wrongly consumes two rolls. - Treating a strike bonus as "the next frame's pins": if Frame 2 is also a strike, Frame 2 only has one roll, so Frame 1's second bonus ball must come from Frame 3.
- Summing
sum(rolls)and adding bonuses for all 10 frames: double-counts the bonus balls thrown in Frame 10, since those extra balls exist only as Frame 10's bonus.
Follow-up questions
- How would you adapt the scorer to return frame-by-frame cumulative totals, including for games still in progress? Keep the same 10-frame loop and append
total_scoreto arunning_totalslist at the end of each resolved frame. To support unfinished games whererollsstops mid-match, verify before scoring each frame thatroll_index < len(rolls)and that all required look-ahead indices (roll_index + 2for strikes and spares,roll_index + 1for open frames) are< len(rolls); as soon as a frame lacks a roll or a pending bonus throw, emitNonefor that frame and every subsequent frame. - What checks are needed to verify that an input array represents a legal, complete sequence of ten-pin throws? Confirm first that every element is an integer (excluding
bool) in0 <= r <= 10. Walk Frames 1–9 ensuringroll_index < len(rolls)and, wheneverrolls[roll_index] < 10, thatroll_index + 1 < len(rolls)androlls[roll_index] + rolls[roll_index + 1] <= 10(since standing pins do not reset mid-frame). In Frame 10, require 3 throws ifrolls[i] == 10(enforcingrolls[i + 1] + rolls[i + 2] <= 10wheneverrolls[i + 1] < 10), 3 throws ifrolls[i] < 10androlls[i] + rolls[i + 1] == 10, or 2 throws ifrolls[i] + rolls[i + 1] < 10, and finally verify the final index equalslen(rolls)so neither missing nor extra throws slip through. - If a variant of the game lasted for
Nframes rather than10, how would the time and memory bounds scale? Replacingrange(10)withrange(n_frames)performs constant-time pointer arithmetic on each of theNframes (over an input array of betweenN + 1and2N + 1throws), so runtime scales linearly toO(N). Auxiliary memory remainsO(1)when returning only the final integer total, orO(N)when returning theN-element list of cumulative frame totals.
Follow-up questions
- How would you adapt the scorer to return frame-by-frame cumulative totals, including for games still in progress?
- What checks are needed to verify that an input array represents a legal, complete sequence of ten-pin throws?
- If a variant of the game lasted for `N` frames rather than `10`, how would the time and memory bounds scale?
Similar questions
- Quota-Aware In-Memory File Store
- Piecewise-Linear Interpolation on Sorted Arrays
- DOM Button Click Listener and Event Target
- Navigating a Data Science Internship Behavioral Screen
- Conversion Probability Modeling for Real-Time Ad Auctions
- Versioned In-Memory Recipe Store
More The Trade Desk interview questions
- DOM Button Click Listener and Event Target Easy
- Navigating a Data Science Internship Behavioral Screen Easy
- Conversion Probability Modeling for Real-Time Ad Auctions Easy Pro
- Versioned In-Memory Recipe Store Medium Pro
- Quota-Aware In-Memory File Store Medium Pro
- Piecewise-Linear Interpolation on Sorted Arrays Easy Pro