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 10 pins in total (first + second < 10). That frame contributes first + second points.
  • Spare: Two throws clear all 10 pins (first < 10 and first + second == 10). That frame contributes 10 plus the pins knocked down on the single next throw.
  • Strike: All 10 pins fall on the frame's opening throw (first == 10), ending frames 1 through 9 immediately after one throw. That frame contributes 10 plus the sum of the next two throws.
  • Frame 10 bonus throws: Clearing all 10 pins in frame 10 awards 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 an 11th 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) <= 21
  • 0 <= rolls[i] <= 10 for every index i.
  • rolls always represents a legal, completed 10-frame bowling game.

Step-by-step walkthrough

  1. Restate the rules and why list length varies

    We receive rolls, a flat list of integers (0 to 10) 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 (10 on the frame's first roll): scores 10 + next two rolls, and the frame uses 1 roll (in frames 1–9).
    • Spare (10 total across two rolls): scores 10 + next one roll, and the frame uses 2 rolls.
    • Open frame (< 10 total 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 has 12 rolls, an all-open game has 20, and an all-spare game has 21. Frame k does not simply start at 2 * k.

  2. 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:

    1. [0, 10] is a spare, not a strike: seeing 10 in the list does not mean it was a strike unless you know whether that 10 landed on the first ball or the second ball of a frame.
    2. 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 of rolls treats the bonus throws at the end as Frame 11 (or Frame 12), reading out of bounds or inflating the score.

    Splitting rolls into 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. Keeping rolls flat makes reading ahead effortless.

  3. 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 pointer roll_index pointing 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 + 1 or roll_index + 2 for a bonus does not consume those rolls — roll_index still advances by just 1 (strike) or 2 (spare/open), so those future rolls get scored normally in their own frames too.

  4. Code the three frame cases cleanly

    Always test first_roll == 10 before testing first_roll + second_roll == 10: - If rolls contains [10, 0, ...], first_roll == 10 catches the strike first and advances roll_index by 1 (if we checked first + second == 10 first, 10 + 0 == 10 would misclassify the strike as a 2-roll spare!). - If rolls contains [0, 10, ...], first_roll == 0 skips the strike branch and lands in the spare branch, advancing roll_index by 2.

    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_score
    
  5. Why 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]): adds 10 + b1 + b2 (rolls[roll_index + 1] + rolls[roll_index + 2]), which is the exact 10th-frame score. - Spare in Frame 10 ([..., r1, r2, b1]): adds 10 + b1 (rolls[roll_index + 2]), exact again. - Open Frame 10 ([..., r1, r2]): adds r1 + r2 without reading roll_index + 2, exact again.

    Because for _frame in range(10) stops right after _frame == 9, the bonus rolls b1 and b2 are 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.
    
  6. 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 10 iterations, reading at most 3 list elements and performing constant-time arithmetic on each iteration (for a generalized N-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 mutate rolls.

Edge cases

  • All gutter balls ([0] * 20): 10 open frames summing to 0.
  • Perfect game ([10] * 12): 10 strikes each scoring 10 + 10 + 10 = 30 for 300; stopping after 10 loop iterations ensures rolls 10 and 11 are only read as Frame 10's bonus.
  • All spares ([5] * 21): 10 spares each scoring 10 + 5 = 15 for 150, reading the 21st roll as Frame 10's spare bonus.
  • 0 followed by 10 ([0, 10, 5, 0, ...]): first_roll == 0 ensures [0, 10] is scored as a spare (10 + 5), not a strike.
  • Back-to-back strikes ([10, 10, 3, 4, ...]): Frame 1 reads rolls[1] (10) and rolls[2] (3) across two subsequent frames for 23, because bonuses count the next two rolls, not the next frame.

Common mistakes

  • Using while roll_index < len(rolls) instead of for _frame in range(10): continues past Frame 10 whenever Frame 10 has strike or spare bonus balls, either crashing with IndexError or inflating the score.
  • Checking rolls[i] + rolls[i + 1] == 10 before rolls[i] == 10: when a strike is followed by a 0 ([10, 0, 5, ...]), 10 + 0 == 10 triggers 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_score to a running_totals list at the end of each resolved frame. To support unfinished games where rolls stops mid-match, verify before scoring each frame that roll_index < len(rolls) and that all required look-ahead indices (roll_index + 2 for strikes and spares, roll_index + 1 for open frames) are < len(rolls); as soon as a frame lacks a roll or a pending bonus throw, emit None for 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) in 0 <= r <= 10. Walk Frames 1–9 ensuring roll_index < len(rolls) and, whenever rolls[roll_index] < 10, that roll_index + 1 < len(rolls) and rolls[roll_index] + rolls[roll_index + 1] <= 10 (since standing pins do not reset mid-frame). In Frame 10, require 3 throws if rolls[i] == 10 (enforcing rolls[i + 1] + rolls[i + 2] <= 10 whenever rolls[i + 1] < 10), 3 throws if rolls[i] < 10 and rolls[i] + rolls[i + 1] == 10, or 2 throws if rolls[i] + rolls[i + 1] < 10, and finally verify the final index equals len(rolls) so neither missing nor extra throws slip through.
  • If a variant of the game lasted for N frames rather than 10, how would the time and memory bounds scale? Replacing range(10) with range(n_frames) performs constant-time pointer arithmetic on each of the N frames (over an input array of between N + 1 and 2N + 1 throws), so runtime scales linearly to O(N). Auxiliary memory remains O(1) when returning only the final integer total, or O(N) when returning the N-element list of cumulative frame totals.

Follow-up questions

Similar questions

More The Trade Desk interview questions

All 7 The Trade Desk questions →