Reported November 2022
Bloombergsimulation

Count Unhappy Friends

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

The data structure carrying this problem is a rank lookup table, and Bloomberg candidates reported Count Unhappy Friends in November 2022. If your OA invite is sitting there, here's the shape of it: n people, ranked preference lists, a perfect matching, and you count who'd rather swap. It looks like a graph puzzle. It's really a bookkeeping problem with a clean O(n^2) answer. The trap is the comparison cost, not the logic. If you blank on the setup during the live assessment, StealthCoder runs invisibly as a safety net and gives you the structure in real time.

The problem

There are n people. preferences[x] lists every other person from most to least preferred, and pairs assigns everyone one partner.
Person x is unhappy if there exists u whom x prefers over x's partner y, and u prefers x over u's partner v. Return the number of unhappy people.

Function
unhappyFriends(n: int, preferences: int[][], pairs: int[][]) → int

Examples
Example 1
n = 4
preferences = [[1,2,3],[3,2,0],[3,1,0],[1,2,0]]
pairs = [[0,1],[2,3]]
return = 2
People 1 and 3 satisfy the reciprocal preference condition.

Constraints
2 <= n <= 500 and n is even.
Each preference row is a permutation of all other people.
Pairs form a perfect matching.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a precomputed rank matrix. Build rank[i][j] as the position of person j in person i's preference list. Then build a partner array from pairs. For each person x with partner y, loop over every u that x ranks above y, meaning rank[x][u] < rank[x][y]. Check whether u prefers x over their own partner v, meaning rank[u][x] < rank[u][partner[u]]. If one such u exists, x is unhappy, count them and break. The common pitfall is calling indexOf on the preference list inside the loop, which turns O(n^2) into O(n^3). With n up to 500 that's still passable but sloppy. Another miss is counting x once per u instead of breaking. Also remember the pairs are symmetric, so fill partner both ways. If the rank table slips your mind under pressure, StealthCoder is the hedge that surfaces it while the OA clock runs.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Count Unhappy Friends cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as count unhappy friends. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Unhappy Friends FAQ

What's the trick to Count Unhappy Friends?+

Precompute a rank matrix so you can compare two people's preference for someone in O(1). Store partner[] from pairs. Then for each person, scan only those they prefer over their partner and check the reciprocal condition. Break on the first match so each person counts once.

How hard is this problem really?+

It's a medium that feels harder than it is because the statement is wordy. There's no fancy algorithm. Once you build the rank lookup and partner array, the code is two nested loops and one comparison. Most of the difficulty is reading the condition correctly.

What's the time complexity I should aim for?+

O(n^2). Building the rank matrix takes O(n^2) since each row has n-1 entries, and the unhappiness check is also at most O(n^2). With n up to 500 that's around 250,000 operations, which is nothing. Avoid searching lists with indexOf inside loops.

What mistakes cause wrong answers here?+

Forgetting to set partner for both people in each pair, counting a person multiple times instead of breaking after the first valid u, and mixing up the direction of the rank comparison. Lower rank number means more preferred, so the checks use less-than.

How do I prepare for this in 48 hours?+

Write it once from scratch with the rank matrix, then test on the example where the answer is 2. Practice a couple of other simulation-style problems with lookup tables. Focus on reading the condition twice and translating it into the two rank comparisons before coding.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it