WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Data Structures & Algorithms · Expert · question 79 of 100

What is the stable marriage problem, and how can it be solved using the Gale-Shapley algorithm?

📕 Buy this interview preparation book: 100 Data Structures & Algorithms questions & answers — PDF + EPUB for $5

The stable marriage problem is a classic problem in mathematics and computer science that deals with finding a stable matching between two sets of people with different preferences. In this problem, there are n men and n women, and each person has a list of preferences ranking the members of the opposite sex from best to worst.

The goal is to match the men and women in such a way that there are no "rogue" pairs who would prefer to be with each other than with their assigned partner. In other words, the matching should be stable, meaning that there should be no two pairs (man-woman) who prefer each other to their assigned partners.

The Gale-Shapley algorithm, also known as the Deferred Acceptance algorithm, is a popular algorithm for solving the stable marriage problem. It works by having each man propose to the woman he most prefers who has not yet rejected him, and the women then choose the best proposal among those they have received so far. If a woman rejects a proposal, she cannot receive any more proposals from that man, but if she accepts a proposal, she is provisionally matched with that man.

The algorithm then continues with the men proposing to the women they most prefer among those who have not rejected them, and the women choosing the best proposal among those they have received so far. This process continues until every woman is provisionally matched with a man.

The Gale-Shapley algorithm guarantees that the matching it produces is stable, meaning that there is no pair of people who would both prefer to be with each other than with their current partner. Moreover, the algorithm produces a matching that is optimal for the proposing side, meaning that no man can be matched with a woman he prefers less than the one he is matched with.

The time complexity of the Gale-Shapley algorithm is O(n2), where n is the number of men and women. This makes the algorithm relatively efficient, even for large datasets.

Example: Suppose there are three men (M1, M2, M3) and three women (W1, W2, W3), and their preference lists are as follows:

M1: W1 > W2 > W3
M2: W2 > W1 > W3
M3: W3 > W2 > W1

W1: M2 > M3 > M1
W2: M1 > M3 > M2
W3: M2 > M1 > M3

The algorithm proceeds as follows:

The resulting stable matching is as follows:

This matching is stable because there is no pair of people who would both prefer to be with each other than with their current partner.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Data Structures & Algorithms interview — then scores it.
📞 Practice Data Structures & Algorithms — free 15 min
📕 Buy this interview preparation book: 100 Data Structures & Algorithms questions & answers — PDF + EPUB for $5

All 100 Data Structures & Algorithms questions · All topics