Rank songs by pairwise user preferences
Company: Confluent
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates the ability to aggregate pairwise user preferences, implement majority-based comparison rules with deterministic tie-breakers, and compute a global ranking via beat counts.
Constraints
- 1 <= n (number of users)
- 1 <= m (number of songs)
- Each pref[i] is a permutation of [0, 1, ..., m-1]
- Song IDs are 0-indexed (0 to m-1)
Examples
Input: (3, 3, [[0, 1, 2], [0, 2, 1], [1, 0, 2]])
Expected Output: [0, 1, 2]
Explanation: Song 0 is preferred over both 1 and 2 by a majority (beats 2). Song 1 beats song 2. Beat counts [2,1,0] give order 0,1,2.
Input: (1, 3, [[2, 0, 1]])
Expected Output: [2, 0, 1]
Explanation: With a single user, x beats y whenever that user ranks x before y. The popularity order mirrors the single preference list: 2, 0, 1.
Input: (2, 2, [[0, 1], [1, 0]])
Expected Output: [0, 1]
Explanation: Users split 1-1 on songs 0 and 1, so count_pref is exactly half (2*1==2==n). The tie is broken by x<y, so song 0 beats song 1. Beat counts [1,0].
Input: (1, 1, [[0]])
Expected Output: [0]
Explanation: Single song, single user. No pairs to compare; the only song is returned.
Input: (4, 3, [[0, 1, 2], [1, 2, 0], [2, 0, 1], [0, 2, 1]])
Expected Output: [0, 1, 2]
Explanation: Beat counts work out to [2,1,0]: song 0 beats both others, song 1 beats song 2, song 2 beats none. Final order 0,1,2.
Input: (2, 4, [[3, 2, 1, 0], [3, 2, 1, 0]])
Expected Output: [3, 2, 1, 0]
Explanation: Both users share the identical ranking, so every pairwise comparison is unanimous. Beat counts [0,1,2,3] for songs 0..3, producing the reversed order 3,2,1,0.
Hints
- Precompute, for each user, the position (rank index) of every song so you can answer 'does user i prefer x over y?' in O(1) by comparing positions.
- To decide if x beats y, count how many users place x before y; compare 2*count against n to handle strict-majority and exact-half cases without floating point. The exact-half case is broken by x < y.
- Once you have each song's beat count (number of distinct songs it beats), sort song IDs by (-beat_count, id) to order from most to least popular with ID as the tie-breaker.