Yahoo Interview Question
Software Engineer / DevelopersDraw a directed graph with men on one side and women on the other side... Edges carry preferences as their weight... Now, all you need to do is, pick all the edges having max preferences!
I would suggest the following
Round 1 : we review the 1st preference of all men and women. All the matches get married. All these people get married with their 1st choice. They're happily married and we can forget about them
Round 2 : we review the 1 and 2 preferences of all men and women. All the matches get married. In each couple, one of the partner gets married with his 2nd choice. The other gets married with his 1st choice or 2nd choice.
Round n : we review the n first preferences of all men and women. All the matches get married. In each couple, one of the partner gets married with his n choice. These marriages can't be broken because neither the man nor the woman can find a better partner.
All the ones that are not married yet are not interesting for them (they're ranked after their n-preference).
All the ones that are already married will never switch for them, as they're already found a partner that ranks better than n in their preference list.
What is the aim here :
1] Get as many as possible, ppl married
2] Get as many as possible, ppl married with their highest choice
In 1] case, some1 might get married to his/her second choice just to avoid deadlock and have more married couples
In 2] case, if there are perfect matches they will get married, irrespective of if some1 stays single.
SM!
- Anonymous September 23, 2009