101
0/304
Loading content...
A workplace gifting platform stores directed gift transfers between employees.
Table: SecretSanta
Business guarantees:
Task: Identify all distinct circular gift chains. A circular chain is a closed directed loop where each participant has exactly:
For every detected chain, compute:
Output requirements:
Supported submission environments:
SecretSanta:
| giver_id | receiver_id | gift_value |
|----------|-------------|------------|
| 1 | 2 | 20 |
| 2 | 3 | 30 |
| 3 | 1 | 40 |
| 4 | 5 | 25 |
| 5 | 4 | 35 |[
{"chain_id":1,"chain_length":3,"total_gift_value":90},
{"chain_id":2,"chain_length":2,"total_gift_value":60}
]There are two disjoint loops. The 3-person loop ranks ahead of the 2-person loop because chain_length is the primary sort key.
SecretSanta:
| giver_id | receiver_id | gift_value |
|----------|-------------|------------|
| 10 | 11 | 7 |
| 11 | 10 | 5 |
| 20 | 21 | 8 |
| 21 | 22 | 9 |
| 22 | 23 | 10 |
| 30 | 31 | 15 |
| 31 | 32 | 5 |
| 32 | 30 | 25 |[
{"chain_id":1,"chain_length":3,"total_gift_value":45},
{"chain_id":2,"chain_length":2,"total_gift_value":12}
]Only closed loops are counted. The chain 20->21->22->23 is open and therefore excluded.
SecretSanta:
| giver_id | receiver_id | gift_value |
|----------|-------------|------------|
| 100 | 101 | 20 |
| 101 | 102 | 30 |
| 102 | 100 | 40 |
| 200 | 201 | 50 |
| 201 | 202 | 10 |
| 202 | 200 | 30 |
| 300 | 301 | 60 |
| 301 | 300 | 20 |[
{"chain_id":1,"chain_length":3,"total_gift_value":90},
{"chain_id":2,"chain_length":3,"total_gift_value":90},
{"chain_id":3,"chain_length":2,"total_gift_value":80}
]The first two loops tie on length and value, so the tie is broken by the smallest participant id in each chain (100 before 200).
Constraints