Quick 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.

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

  1. 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.
  2. 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.
  3. 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.

Merge Preference Lists into One Ranking

hardAirbnbMachine Learning Engineer

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

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

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.

Examples

Example 1
Input
([['a']],)
Output
['a']
Notes
Minimum input: one list holding one item.
Example 2
Input
([['zeta', 'alpha', 'mid', 'beta']],)
Output
['zeta', 'alpha', 'mid', 'beta']
Notes
A single list fixes every position, so the ranking is that list unchanged even though it is unsorted.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def merge_preferences(lists: list[list[str]]) -> list[str]:
Sample call
merge_preferences([['a']])
Language
Case 1
Input
([['a']],)
Expected
['a']
Case 2
Input
([['zeta', 'alpha', 'mid', 'beta']],)
Expected
['zeta', 'alpha', 'mid', 'beta']

Browse More Questions

Show the approach

Approach

Treat every distinct item as a node and add a directed edge from each item to the item directly after it in every list. Adjacent pairs are enough: an item's predecessors in a list are all placed exactly when the item directly above it is placed, because that item could only be placed after its own predecessors. For every item keep a count of incoming edges whose source is not yet placed; a pair repeated in several lists adds one edge per copy and each copy is removed once, so the count stays exact. Put every item with count 0 into a min-heap ordered by string comparison. Repeatedly pop the smallest item, append it to the ranking, and decrement the count of each successor, pushing a successor when its count reaches 0.

Invariant: the heap holds exactly the items that the rules allow to be placed next, so each pop is the rule's choice and the ranking is the unique one the rules describe. If the heap empties while some items remain, each remaining item has an unplaced item ranked above it; following those predecessors among the remaining items must revisit an item, so the constraints contain a cycle, no ranking can keep every list's order, and the function returns [] instead of the partial ranking.

Edge cases: a single item; a single list, whose order is returned unchanged; only singleton lists, which give sorted order; repeated pairs across lists; items that are prefixes of others or made of digits, which follow string order; and a cycle that leaves some items placeable, which still returns [].

Time complexity:
O(N log N), where N is the total number of entries; each string comparison reads at most 20 characters
Space complexity:
O(N)