# The Title Ladder

> Job titles and the salary tier they belong to.

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

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

Given workers (list of dicts with worker_id and salary) and titles (list of dicts with worker_id and title), find the maximum salary. Return the sorted list of distinct titles held by any worker earning that maximum salary.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **multi-dataset joining** in pure Python. Finding records linked by a shared key and filtering by an aggregate condition mirrors JOIN + GROUP BY + HAVING in SQL.

---

### Break down the requirements

#### Step 1: Find the maximum salary across all workers

Iterate through worker records to find the top salary.

#### Step 2: Collect worker IDs earning the top salary

There may be multiple workers tied at the max salary.

#### Step 3: Look up their titles from the title records

Join on `worker_id` and collect unique titles held by the top earners.

---

### The solution

**Two-pass filter with set-based join**

```python
def top_earner_titles(workers: list, titles: list) -> list:
    max_salary = 0
    for w in workers:
        if w["salary"] > max_salary:
            max_salary = w["salary"]
    top_ids = set()
    for w in workers:
        if w["salary"] == max_salary:
            top_ids.add(w["worker_id"])
    unique_titles = set()
    for t in titles:
        if t["worker_id"] in top_ids:
            unique_titles.add(t["title"])
    result = sorted(unique_titles)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(w + t) where w is the number of worker records and t is the number of title records.
> 
> **Space:** O(k + u) where k is the number of top earners and u is the number of unique titles.

> **Interviewers Watch For**
>
> Using a set for `top_ids` gives O(1) lookups during the title join. Using a list would make the join O(w * t).

> **Common Pitfall**
>
> Assuming only one worker has the top salary. The problem says "highest earners" (plural), so you must handle ties.

---

## Common follow-up questions

- How would you handle workers with no title records? _(Tests awareness of left join semantics vs. inner join.)_
- What if you needed the top N salaries instead of just the max? _(Tests sorting workers and using a cutoff or heap.)_
- How would you write this as a SQL query? _(Tests translating the Python logic to JOIN + WHERE + subquery.)_

## Related

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