# The Numbered Chair

> A standing list. Position n holds one entry.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a dict mapping player names to scores, return the player name at the n-th position when sorted by score descending (tie-break by player name ascending). n is 1-based. If n exceeds the number of players, return None.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **sorting dictionary entries by value** and extracting a specific rank. It checks whether candidates understand key-value pair sorting and 1-based vs 0-based indexing.

---

### Break down the requirements

#### Step 1: Sort entries by score descending

Convert the dictionary to a list of (name, score) pairs sorted by score in descending order.

#### Step 2: Return the name at position n

The position is 1-based, so the nth place corresponds to index n-1 in the sorted list.

---

### The solution

**Sort by value, index by rank**

```python
def nth_place(scores, n):
    pairs = []
    for name in scores:
        pairs.append((scores[name], name))
    pairs.sort(reverse=True)
    result = pairs[n - 1][1]
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(k log k) where k is the number of players, for sorting.
> 
> **Space:** O(k) for the sorted pairs list.

> **Interviewers Watch For**
>
> Do you sort by `(score, name)` or just `score`? Sorting by score alone is correct here, but mentioning tie-breaking strategy shows thoughtfulness.

> **Common Pitfall**
>
> Using 0-based indexing for a 1-based rank. Accessing `pairs[n]` instead of `pairs[n-1]` returns the wrong player.

---

## Common follow-up questions

- What if two players are tied? _(Tests how to define rank: dense ranking, standard competition ranking, or arbitrary.)_
- How would you find the top 3 without sorting the entire dict? _(Tests using a heap for O(k log 3) instead of O(k log k).)_
- What if the scores dictionary is updated frequently? _(Tests maintaining a sorted structure like a balanced BST or sorted list with bisect.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_numbered_chair)
- [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.