Construct a circular gift exchange across families
Company: Wave
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
You are given a “big family” composed of several **sub-families**. Each sub-family is a list of unique person IDs/names.
Example input:
- `[[A1, A2], [B1, B2], [C1]]`
You need to produce a **circular gift-exchange chain** that includes **every person exactly once**, such that:
1. **No one gives a gift to someone in the same sub-family.**
- In the chain order, this means **adjacent people** (giver → receiver) must come from **different** sub-families.
2. The arrangement must form **one cycle** (a single circular chain).
- If the output order is `p0, p1, ..., p(n-1)`, then gifts go `p0→p1, p1→p2, ..., p(n-2)→p(n-1), p(n-1)→p0`.
### Input
- A list of sub-families, where each sub-family is a list of strings (or integers) representing people.
### Output
- Return **any** valid circular ordering of all people (or equivalently, the directed gift pairs), **or** indicate that it is **impossible**.
### Notes / Clarifications
- People are distinct across all sub-families.
- The chain must include everyone exactly once.
- If there are multiple valid answers, any one is acceptable.
### Example (one possible valid output)
For `[[A1, A2], [B1, B2], [C1]]`, a valid circular ordering could be:
- `[A1, B1, A2, B2, C1]`
This implies: `A1→B1→A2→B2→C1→A1`, and no adjacent pair is from the same sub-family.
Quick Answer: This question evaluates ability to model constrained permutations and apply graph-theoretic and combinatorial reasoning to produce a single circular ordering where adjacent elements come from different sub-groups.