Merge Preference Lists Into One Order Consistent With Every List
Company: Airbnb
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
You are given several preference lists. Each list ranks some items from most preferred to least preferred. Merge them into a single ranking that contains every item appearing in any list exactly once and keeps the order of every list: whenever item `x` comes before item `y` in some input list, `x` must come before `y` in the merged ranking.
When more than one item could be placed next, place the smallest one. If the lists contradict each other so that no ranking keeps every list's order, return an empty list.
### Function Signature
```python
def merge_preferences(lists: list[list[str]]) -> list[str]:
```
### Rules
- Within one list each item appears at most once; the same item may appear in several lists.
- An item can be placed next once every item that some list ranks above it has been placed.
- Among the items that can be placed next, choose the smallest under Python string comparison.
- Items that no list ranks against each other have no required order between them; the smallest-first rule decides.
- If items remain but none can be placed, return `[]`.
### Constraints
- `1 <= len(lists) <= 1000`
- `1 <= len(lists[i])` for every `i`
- The total number of entries across all lists is at most `10^5`.
- Items are 1 to 20 characters long and contain only lowercase English letters and digits.
### Examples
#### Example 1
```text
Input: lists = [["b","d"],["a","d"],["c"]]
Output: ["a","b","c","d"]
```
Nothing is ranked above `a`, `b` or `c`, so any of them can go first and the smallest, `a`, is placed. Then `b` is the smallest available item. `d` becomes available once `a` and `b` are placed, but `c` is smaller, so `d` comes last.
#### Example 2
```text
Input: lists = [["tea","coffee","juice"],["coffee","water"],["tea","water","juice"]]
Output: ["tea","coffee","water","juice"]
```
The lists require `tea` before `coffee`, `coffee` before `water`, and `water` before `juice`, so only one ranking keeps every list's order.
#### Example 3
```text
Input: lists = [["x","y"],["y","x"]]
Output: []
```
One list ranks `x` above `y` and the other ranks `y` above `x`.
Overview: A coding problem about merging several ranked preference lists into one order that keeps every list's relative ranking, placing the smallest available item first and returning an empty list when the lists contradict each other. It tests ordering constraints, conflict detection and deterministic tie breaking.
Read the full Airbnb Machine Learning Engineer interview experience this question came from
You are given several preference lists. Each list ranks some items from most preferred to least preferred. Merge them into a single ranking that contains every item appearing in any list exactly once and keeps the order of every list: whenever item `x` comes before item `y` in some input list, `x` must come before `y` in the merged ranking.
When more than one item could be placed next, place the smallest one. If the lists contradict each other so that no ranking keeps every list's order, return an empty list.
Implement `merge_preferences(lists)`, where `lists` is a list of lists of strings, and return the merged ranking as a list of strings.
### Rules
- Within one list each item appears at most once; the same item may appear in several lists.
- An item can be placed next once every item that some list ranks above it has been placed.
- Among the items that can be placed next, choose the smallest under Python string comparison. Python compares two strings character by character by character code, and a prefix comes before any longer string that starts with it. Since items contain only lowercase letters and digits, every digit comes before every letter, `"a"` comes before `"ab"`, and `"10"` comes before `"9"`.
- Items that no list ranks against each other have no required order between them; the smallest-first rule decides.
- If items remain but none can be placed, return `[]`, even when some items were already placed.
### Examples
#### Example 1
```text
Input: lists = [["b","d"],["a","d"],["c"]]
Output: ["a","b","c","d"]
```
Nothing is ranked above `a`, `b` or `c`, so any of them can go first and the smallest, `a`, is placed. Then `b` is the smallest available item. `d` becomes available once `a` and `b` are placed, but `c` is smaller, so `d` comes last.
#### Example 2
```text
Input: lists = [["x","y"],["y","x"]]
Output: []
```
One list ranks `x` above `y` and the other ranks `y` above `x`, so no ranking keeps both orders.
### Constraints
- `1 <= len(lists) <= 1000`
- `1 <= len(lists[i])` for every `i`
- The total number of entries across all lists is at most `10^5`.
- Items are 1 to 20 characters long and contain only lowercase English letters and digits.
- Within one list each item appears at most once.
Constraints
- 1 <= len(lists) <= 1000
- 1 <= len(lists[i]) for every i
- The total number of entries across all lists is at most 10^5.
- Items are 1 to 20 characters long and contain only lowercase English letters and digits.
- Within one list each item appears at most once; the same item may appear in several lists.
Examples
Input: ([['a']],)
Expected Output: ['a']
Explanation: Minimum input: one list holding one item.
Input: ([['zeta', 'alpha', 'mid', 'beta']],)
Expected Output: ['zeta', 'alpha', 'mid', 'beta']
Explanation: A single list fixes every position, so the ranking is that list unchanged even though it is unsorted.
Hints
- At any moment, an item is ready only if every item that some list ranks above it is already in the ranking. Work out which items are ready before choosing the next one.
- The set of ready items changes after every placement, and an item that just became ready can be smaller than items that were ready earlier.
- If items remain and none of them is ready, the lists contradict each other, and the answer is [] even if part of a ranking was already built.