The Count Unhappy Friends interview question is a simulation-based problem that explores the dynamics of relationships and preferences within a group. You are given a group of friends, each with a ranked list of preferences for other friends. These friends are paired up into pairs. A person is considered "unhappy" if they were paired with , but they actually prefer some person over , AND that person also prefers over their own assigned partner . The goal is to count how many such individuals exist in the current pairing configuration.
Bloomberg and other tech companies use the Count Unhappy Friends coding problem to assess a candidate's ability to handle multi-dimensional data structures and nested logic. It specifically tests how well you can translate complex English rules into efficient code. While it doesn't require advanced algorithms, it demands precise indexing and a clear understanding of the "Stable Marriage" concept in a simplified, non-bipartite setting.
This problem primarily uses an Array and Simulation approach. To make the preference lookups efficient, a common Simulation interview pattern involves preprocessing the preference lists into a 2D matrix or a hash map. Instead of searching through a list each time to see if prefers over , you can store the rank of each friend in a rank[x][u] matrix, where a smaller value indicates a higher preference.
Imagine four friends: 0, 1, 2, and 3.
Whenever a problem involves "preferences" or "rankings," try to convert those rankings into an O(1) lookup table immediately. Mapping friend_id to its index in a preference list is a standard trick to speed up comparison logic.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Average Waiting Time | Medium | Solve | |
| Find The First Player to win K Games in a Row | Medium | Solve | |
| Find the Winner of an Array Game | Medium | Solve | |
| Maximum Profit of Operating a Centennial Wheel | Medium | Solve | |
| Min Max Game | Easy | Solve |