Generate Secret Santa assignments with no self-gifts and recipient-side forbidden donors. Use bipartite matching for feasibility and explain the difference between varied valid assignments and uniform random sampling.
Generate a Secret Santa Matching with Recipient Exclusions
Company: Shopify
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Read participants from a CSV file, assign each person exactly one different person to give a gift to, and print the giver-to-recipient matches. Everyone must give and receive exactly once. The initial assignment should be randomized; clarify the required distribution before choosing a sampling algorithm. Email delivery is a possible later extension, not part of this task.
### Constraints and Clarifications
Each row contains a participant identifier, an email field treated as opaque data, and optional excluded giver identifiers. An exclusion on recipient R's row means R must not receive a gift from that giver; it is not automatically an exclusion on gifts R gives. Identifiers are unique and refer to participants in the group. Empty optional fields mean no exclusion. Use a proper CSV parser for quoted fields. No size bounds are supplied. Multiple valid matchings are allowed; report no solution if none exists.
### Part 1 — No Self-Gifts
Produce a randomized complete matching with no participant assigned to themselves, and print each giver and recipient.
#### What This Part Should Cover
Recognize the assignment as a permutation without fixed points, handle groups too small to match, and distinguish randomness from a guarantee of uniform sampling.
### Part 2 — Recipient Exclusions
Add the optional recipient-side restrictions. For participants A,B,C,D,E, suppose B excludes A; D excludes A,B,C; E excludes A,D; and the others have no extra exclusions. D can receive only from E because self-gifts are also forbidden.
#### What This Part Should Cover
Model allowed giver-to-recipient edges, find a perfect matching or establish failure, and avoid greedy choices that strand a constrained recipient.
### What a Strong Answer Covers
Correct CSV interpretation, one-to-one matching, exclusion direction, feasibility detection, a candid randomization policy, and separation of printed output from future email sending.
### Follow-up Questions
- Why does shuffling a list and rotating it generate only a subset of all possible valid assignments?
- How would you obtain a uniform valid assignment if that fairness requirement were mandatory?
Overview: Generate Secret Santa assignments with no self-gifts and recipient-side forbidden donors. Use bipartite matching for feasibility and explain the difference between varied valid assignments and uniform random sampling.
Read participants from a CSV file, assign each person exactly one different person to give a gift to, and print the giver-to-recipient matches. Everyone must give and receive exactly once. The initial assignment should be randomized; clarify the required distribution before choosing a sampling algorithm. Email delivery is a possible later extension, not part of this task.
Constraints and Clarifications
Each row contains a participant identifier, an email field treated as opaque data, and optional excluded giver identifiers. An exclusion on recipient R's row means R must not receive a gift from that giver; it is not automatically an exclusion on gifts R gives. Identifiers are unique and refer to participants in the group. Empty optional fields mean no exclusion. Use a proper CSV parser for quoted fields. No size bounds are supplied. Multiple valid matchings are allowed; report no solution if none exists.
Part 1 — No Self-Gifts
Produce a randomized complete matching with no participant assigned to themselves, and print each giver and recipient.
What This Part Should Cover Guidance
Recognize the assignment as a permutation without fixed points, handle groups too small to match, and distinguish randomness from a guarantee of uniform sampling.
Part 2 — Recipient Exclusions
Add the optional recipient-side restrictions. For participants A,B,C,D,E, suppose B excludes A; D excludes A,B,C; E excludes A,D; and the others have no extra exclusions. D can receive only from E because self-gifts are also forbidden.
What This Part Should Cover Guidance
Model allowed giver-to-recipient edges, find a perfect matching or establish failure, and avoid greedy choices that strand a constrained recipient.
What a Strong Answer Covers Guidance
Correct CSV interpretation, one-to-one matching, exclusion direction, feasibility detection, a candid randomization policy, and separation of printed output from future email sending.
Follow-up Questions Guidance
Why does shuffling a list and rotating it generate only a subset of all possible valid assignments?
How would you obtain a uniform valid assignment if that fairness requirement were mandatory?