# Dice Roll Scoring

> The pattern rewards the patient.

Canonical URL: <https://datadriven.io/problems/dice_roll_scoring>

Domain: Python · Difficulty: medium · Seniority: L5

## Problem

Given exactly 5 dice values (integers 1-6), return the best score using these priorities: five-of-a-kind = 50; four-of-a-kind = 40; full-house (three of one value + two of another) = 25; three-of-a-kind = sum of all 5 dice; otherwise = sum of the dice.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **frequency counting** and **conditional logic** based on count distributions. It reveals whether you can translate a multi-rule scoring system into clean, ordered conditionals.

---

### Break down the requirements

#### Step 1: Count the frequency of each die value

Build a frequency map of the 5 dice values.

#### Step 2: Determine the hand type from the frequency distribution

Sort the frequency values in descending order to identify the pattern: [5] for five-of-a-kind, [4,1] for four-of-a-kind, [3,2] for full house, [3,1,1] for three-of-a-kind.

#### Step 3: Return the appropriate score

Five-of-a-kind returns 50, four-of-a-kind returns 40, full house returns 25, and all other hands return the sum of all dice.

---

### The solution

**Frequency distribution matching for hand evaluation**

```python
def score_dice(dice):
    counts = {}
    for d in dice:
        if d not in counts:
            counts[d] = 0
        counts[d] += 1
    freq = sorted(counts.values(), reverse=True)
    dice_sum = 0
    for d in dice:
        dice_sum += d
    if freq == [5]:
        return 50
    if freq == [4, 1]:
        return 40
    if freq == [3, 2]:
        return 25
    return dice_sum
```

> **Time and Space Complexity**
>
> **Time:** O(1). The input is always 5 dice, so all operations are constant time.
> 
> **Space:** O(1). The frequency dict has at most 6 entries (values 1 through 6).

> **Interviewers Watch For**
>
> Using the sorted frequency distribution as the pattern key rather than writing complex nested if-else chains checking specific values. This approach is clean and extensible to new hand types.

> **Common Pitfall**
>
> Forgetting that three-of-a-kind and the default case both return the sum of all dice. The only distinction matters for the scoring formula, not the return value.

---

## Common follow-up questions

- What if you needed to support straights (sequential values)? _(Tests detecting sorted consecutive sequences in the dice values.)_
- How would you rank two hands against each other? _(Tests defining a total ordering on hand types with tiebreakers.)_
- What if the number of dice is variable? _(Tests generalizing the frequency pattern matching beyond fixed-size input.)_
- How would you compute the expected score over all possible rolls? _(Tests probability: enumerate all 6^5 outcomes or use combinatorics to weight each hand type.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/dice_roll_scoring)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.