Find Circular Gift Exchange Chains - Problem

Given a SecretSanta table that represents gift exchanges between employees, find all circular chains of gift exchanges.

A circular chain is defined as a series of exchanges where:

  • Each employee gives a gift to exactly one other employee
  • Each employee receives a gift from exactly one other employee
  • The exchanges form a continuous loop (e.g., employee A gives to B, B gives to C, and C gives back to A)

Return the chain_id, chain_length, and total_gift_value for each circular chain, ordered by chain length and total gift value in descending order.

Table Schema

SecretSanta
Column Name Type Description
giver_id PK int ID of the employee giving the gift
receiver_id PK int ID of the employee receiving the gift
gift_value int Value of the gift being exchanged
Primary Key: (giver_id, receiver_id)
Note: Each row represents a gift exchange. The combination of giver_id and receiver_id is unique.

Input & Output

Example 1 — Multiple Circular Chains
Input Table:
giver_id receiver_id gift_value
1 2 20
2 3 30
3 1 40
4 5 25
5 4 35
Output:
chain_id chain_length total_gift_value
1 3 90
2 2 60
💡 Note:

Two circular chains are formed:

  • Chain 1: Employees 1→2→3→1 with length 3 and total value 20+30+40=90
  • Chain 2: Employees 4→5→4 with length 2 and total value 25+35=60

Results ordered by chain length (3,2) then total value (90,60) in descending order.

Example 2 — Single Large Chain
Input Table:
giver_id receiver_id gift_value
1 2 100
2 3 150
3 4 200
4 1 250
Output:
chain_id chain_length total_gift_value
1 4 700
💡 Note:

One circular chain is formed with all 4 employees: 1→2→3→4→1

Chain length is 4 and total gift value is 100+150+200+250=700

Example 3 — Self-Exchange
Input Table:
giver_id receiver_id gift_value
1 1 50
Output:
chain_id chain_length total_gift_value
1 1 50
💡 Note:

Employee 1 gives a gift to themselves, forming a circular chain of length 1 with total value 50

Constraints

  • 1 ≤ giver_id, receiver_id ≤ 1000
  • 1 ≤ gift_value ≤ 10000
  • (giver_id, receiver_id) is unique for each row
  • Each employee in a valid circular chain gives exactly one gift and receives exactly one gift

Visualization

Tap to expand
Find Circular Gift Exchange Chains INPUT: SecretSanta Table A B C D E giver | receiver | value A --> B | $50 B --> C | $30 C --> D | $40 D --> E | $25 E --> A | $35 ALGORITHM STEPS 1 Build Graph Create adjacency list from giver-receiver pairs 2 Find Cycles (DFS) Use recursive DFS with visited tracking 3 Validate Chains Each node: exactly 1 in, exactly 1 out edge 4 Aggregate Results Calculate chain_length, sum total_gift_value Cycle Detection: A B C D E OK FINAL RESULT A B C D E Chain 1 Output chain_id: 1 chain_length: 5 total_value: $180 Valid Circular Chain! ORDER BY: chain_length DESC total_gift_value DESC Key Insight: A valid circular gift exchange chain requires every participant to give exactly one gift AND receive exactly one gift. Use recursive CTE or DFS to detect cycles, ensuring the chain forms a complete loop back to the starting node. Time Complexity: O(V + E) where V = employees, E = gift exchanges. TutorialsPoint - Find Circular Gift Exchange Chains | Optimal Solution
Asked in
Amazon 12 Microsoft 8 Google 6
23.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen