# The Price Bander

> Different prices, different treatment.

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

Domain: Python · Difficulty: easy · Seniority: L4 · Asked in: Gemini

## Problem

A catalog service segments products into price brackets before it renders them. Given `prices`, a dict mapping product names to prices, return a new dict mapping each name to its bracket: 'low' for anything under 10, 'mid' for anything under 50, and 'high' otherwise.

## Example

Input:

```
prices = {"pen":2,"book":15,"apple":1.5,"laptop":999}
```

Output:

```
{"pen":"low","book":"mid","apple":"low","laptop":"high"}
```

## Worked solution and explanation

### It looks like labeling, it is really about overlap

Strip the catalog costume and this is a nested-band classification: the bands overlap, because every price under 10 is also under 50. So the answer to 'which band?' depends entirely on which comparison you reach first. Anyone can write three if branches. The candidates who fail are the ones who reach for the widest condition first, or reach for `<=` at a cutoff. Reverse the order and every cheap item comes back tagged 'mid'; slip one `<=` and a price of exactly 10 lands in 'low'. Nothing throws an error, the numbers just come out quietly wrong.

> **Check narrowest-first, not widest-first**
>
> Because the bands are nested, order is the whole game. Ask the NARROWEST question first (`price < 10`). Once that fails you already know the price is at least 10, so the next check (`price < 50`) can be a bare comparison instead of a range.

---

### Walking to the answer

#### Step 1: Scan each product and its price

Iterate the name/price pairs. Nothing here needs the values sorted or the keys touched, so a straight pass over the dict is enough.

#### Step 2: Resolve the band in order

Run the cutoffs from tightest to loosest: `price < 10` is 'low', then `price < 50` is 'mid', otherwise 'high'. The elif chain guarantees the first matching band wins, which is exactly what the overlap demands.

#### Step 3: Emit a new dict

Write name -> band into a fresh dict and return it. Don't mutate the input; the graders reuse it.

---

### The solution

**Ordered threshold classification**

```python
def price_tiers(prices):
    tiers = {}
    for name, price in prices.items():
        if price < 10:
            tiers[name] = 'low'
        elif price < 50:
            tiers[name] = 'mid'
        else:
            tiers[name] = 'high'
    return tiers
```

> **Common pitfall**
>
> A price of exactly 10 must be 'mid', and exactly 50 must be 'high'. The half-open cutoffs (`< 10`, `< 50`) put the boundary value into the HIGHER band. Swap either to `<=` and the boundary silently slides down a tier, which is the single most common way this one breaks.

> **Interviewers watch for**
>
> The tell of seniority is the candidate who says out loud 'the bands overlap, so order matters' before writing anything, and who names what happens at exactly 10 and 50 without being asked. Reversing the two conditions is the fast reject.

> **Cost**
>
> **Time:** O(n) over n products, one comparison chain each. Space: O(n) for the output dict. No sort, no second pass; the ordered elif chain is what keeps it single-pass.

---

## Common follow-up questions

- What if the bands and their cutoffs were passed in as config? _(Tests generalizing the hard-coded chain into an ordered scan of (cutoff, label) pairs, still narrowest-first.)_
- How would you also report how many products landed in each band? _(Tests maintaining a counter dict alongside the classification in the same pass.)_
- Does anything change if prices can be negative? _(Tests that negative prices still resolve to 'low' under the same ordering.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_price_bander)
- [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). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.