Construct a circular gift exchange across families
Construct a Circular Gift Exchange Across Families
You are given a list of sub-families, where each sub-family is a list of unique person IDs. Your task is to construct a circular gift-exchange chain that includes every person exactly once. The constraints are:
- No one gives a gift to someone in the same sub-family. This means in the chain order, adjacent people (giver → receiver) must belong to different sub-families.
- The arrangement must form a single cycle. If the output order is
p0, p1, ..., p(n-1), then gifts gop0→p1, p1→p2, ..., p(n-2)→p(n-1), p(n-1)→p0.
People are distinct across all sub-families. If multiple valid answers exist, any one is acceptable. If it's impossible to form such a chain, indicate that.
Example:
Input: [[A1, A2], [B1, B2], [C1]]
Possible Output: [A1, B1, A2, B2, C1] (implying A1→B1→A2→B2→C1→A1)
Similar LeetCode problem: [Perform String Shifts
AI interview prep
Core dilemma
Constructing a Hamiltonian cycle in a graph where nodes are people and edges are restricted by sub-family membership, while ensuring all nodes are visited exactly once.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Construct a circular gift exchange across families
Construct a Circular Gift Exchange Across Families
You are given a list of sub-families, where each sub-family is a list of unique person IDs. Your task is to construct a circular gift-exchange chain that includes every person exactly once. The constraints are:
- No one gives a gift to someone in the same sub-family. This means in the chain order, adjacent people (giver → receiver) must belong to different sub-families.
- The arrangement must form a single cycle. If the output order is
p0, p1, ..., p(n-1), then gifts gop0→p1, p1→p2, ..., p(n-2)→p(n-1), p(n-1)→p0.
People are distinct across all sub-families. If multiple valid answers exist, any one is acceptable. If it's impossible to form such a chain, indicate that.
Example:
Input: [[A1, A2], [B1, B2], [C1]]
Possible Output: [A1, B1, A2, B2, C1] (implying A1→B1→A2→B2→C1→A1)
Similar LeetCode problem: [Perform String Shifts