The Title Ladder

A medium Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
medium
Seniority
L3
Asked in
DoorDash

Problem

List the job titles held by whoever earns the top salary in workers, each title once and in the order it first appears in titles. Several workers can share that top salary, one worker can hold more than one title, and no workers means no titles.

Job titles and the salary tier they belong to.

Example

Input:

workers = [{"salary":80000,"worker_id":1},{"salary":120000,"worker_id":2},{"salary":120000,"worker_id":3}], titles = [{"title":"Junior Engineer","worker_id":1},{"title":"Senior Engineer","worker_id":2},{"title":"Tech Lead","worker_id":3}]

Output:

["Senior Engineer","Tech Lead"]

Worked solution and explanation

What this really is

This is a semi-join behind an aggregate, dressed up as an HR lookup. Anyone can find the top salary. What separates candidates is whether they treat that maximum as a value many workers can hit or as a single worker. Track the argmax instead of the value and the visible case loses 'Tech Lead', because two people sit at 120000. Then the second half catches the rest: a title shared by two top earners has to come out once, in the order titles first lists it.

Step 1: Settle the empty roster first

With no workers there is no top salary, and max on an empty sequence raises. One early return keeps the main path clean and answers the edge in the shape the caller expects: [].

Step 2: Find the top salary as a value

Compute top_salary once, then collect every worker_id whose salary equals it into top_ids. That set is the whole tie story: it holds one id or ten, and nothing downstream needs to care which.

Step 3: Walk titles once, filtering and collapsing

Each title record either belongs to a top earner or it does not. Keep the ones that do, but only the first time a title string shows up: a seen set rejects repeats while result keeps the order titles gave you.

Two passes over workers, one over titles
def top_earning_titles(workers: list[dict], titles: list[dict]) -> list[str]:
    if not workers:
        return []

    top_salary = max(worker["salary"] for worker in workers)

    top_ids = set()
    for worker in workers:
        if worker["salary"] == top_salary:
            top_ids.add(worker["worker_id"])

    seen = set()
    result = []
    for record in titles:
        title = record["title"]
        if record["worker_id"] in top_ids and title not in seen:
            seen.add(title)
            result.append(title)
    return result
Unordered collapse
Collecting titles into a set and returning list(unique_titles) dedups correctly, but the order is whatever the hash table produced on that run. The answer can change between executions.
First-appearance collapse
A seen set paired with a result list dedups just as cheaply and keeps the order titles supplied, so the output is a function of the input and nothing else.

Common follow-up questions