Count Unhappy Friends in a Pairing Given Ranked Preference Lists

Quick Overview

Given each friend's ranked preference list and a fixed pairing, count how many friends would rather be with someone who also prefers them over their own partner. It tests turning a two-sided preference condition into fast rank comparisons and counting each person only once.

Count Unhappy Friends in a Pairing Given Ranked Preference Lists

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of `preferences` for `n` friends, where `n` is always even. For each person `i`, `preferences[i]` contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from `0` to `n-1`. All the friends are divided into pairs. The pairings are given in a list `pairs`, where `pairs[i] = [x_i, y_i]` denotes that `x_i` is paired with `y_i` and `y_i` is paired with `x_i`. This pairing may leave some of the friends unhappy. A friend `x` is unhappy if `x` is paired with `y` and there exists a friend `u` who is paired with `v` such that: - `x` prefers `u` over `y`, and - `u` prefers `x` over `v`. Return the number of unhappy friends. ### Function Signature ```python def count_unhappy_friends(n: int, preferences: list[list[int]], pairs: list[list[int]]) -> int: ``` ### Rules - `preferences[i]` lists every friend other than `i`, from most preferred to least preferred. - Count people, not reasons. A friend who is unhappy because of several different `u` still adds 1 to the answer. ### Constraints - `2 <= n <= 500` - `n` is even. - `preferences.length == n` - `preferences[i].length == n - 1` - `0 <= preferences[i][j] <= n - 1` - `preferences[i]` does not contain `i`. - All values in `preferences[i]` are unique. - `pairs.length == n / 2` - `pairs[i].length == 2` - `0 <= x_i, y_i <= n - 1` - Each person is contained in exactly one pair. ### Examples ```text Input: n = 4, preferences = [[1,2,3],[3,2,0],[3,1,0],[1,2,0]], pairs = [[0,1],[2,3]] Output: 2 ``` Friend 1 is unhappy because they prefer friend 3 over their partner 0, and friend 3 prefers friend 1 over their partner 2. For the same reason friend 3 is unhappy, while friends 0 and 2 are paired with their first choice and are happy. ```text Input: n = 2, preferences = [[1],[0]], pairs = [[1,0]] Output: 0 ``` Both friends are paired with their only choice, so no one is unhappy. ```text Input: n = 4, preferences = [[2,3,1],[0,2,3],[0,1,3],[0,1,2]], pairs = [[0,1],[2,3]] Output: 3 ``` Friend 0 prefers both 2 and 3 over partner 1, and both 2 and 3 rank 0 first, so friends 0, 2 and 3 are unhappy (0 is counted once). Friend 1 is paired with their first choice and is happy.

Overview: Given each friend's ranked preference list and a fixed pairing, count how many friends would rather be with someone who also prefers them over their own partner. It tests turning a two-sided preference condition into fast rank comparisons and counting each person only once.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of preferences for n friends, where n is always even.

For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1.

All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [x_i, y_i] denotes that x_i is paired with y_i and y_i is paired with x_i.

This pairing may leave some of the friends unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v such that:

  • x prefers u over y , and
  • u prefers x over v .

Return the number of unhappy friends.

Function Signature

def count_unhappy_friends(n: int, preferences: list[list[int]], pairs: list[list[int]]) -> int:

Rules

  • preferences[i] lists every friend other than i , from most preferred to least preferred.
  • Count people, not reasons. A friend who is unhappy because of several different u still adds 1 to the answer.

Constraints

  • 2 <= n <= 500
  • n is even.
  • preferences.length == n
  • preferences[i].length == n - 1
  • 0 <= preferences[i][j] <= n - 1
  • preferences[i] does not contain i .
  • All values in preferences[i] are unique.
  • pairs.length == n / 2
  • pairs[i].length == 2
  • 0 <= x_i, y_i <= n - 1
  • Each person is contained in exactly one pair.

Examples

Input: n = 4, preferences = [[1,2,3],[3,2,0],[3,1,0],[1,2,0]], pairs = [[0,1],[2,3]]
Output: 2

Friend 1 is unhappy because they prefer friend 3 over their partner 0, and friend 3 prefers friend 1 over their partner 2. For the same reason friend 3 is unhappy, while friends 0 and 2 are paired with their first choice and are happy.

Input: n = 2, preferences = [[1],[0]], pairs = [[1,0]]
Output: 0

Both friends are paired with their only choice, so no one is unhappy.

Input: n = 4, preferences = [[2,3,1],[0,2,3],[0,1,3],[0,1,2]], pairs = [[0,1],[2,3]]
Output: 3

Friend 0 prefers both 2 and 3 over partner 1, and both 2 and 3 rank 0 first, so friends 0, 2 and 3 are unhappy (0 is counted once). Friend 1 is paired with their first choice and is happy.