Merge Mutual Connections Into Earliest-Confirmed Pairs
Arrays & HashingAuto-graded · 18 test casesLast updated
Tests normalizing undirected pairs, filtering pending rows, and keeping the earliest date per pair from a list of event dictionaries, with deterministic sorted output.
Merge Mutual Connections Into Earliest-Confirmed Pairs
A social app records every connection invite as an event. Each event is a dictionary with these keys:
requester_id: the integer id of the user who sent the invite.accepter_id: the integer id of the user who received it.request_date: the date the invite was sent, as a string.accept_date: the date the invite was accepted, as a string, orNoneif the invite is still pending.
A connection between two users is mutual and undirected: an accepted invite from user 2 to user 3 and an accepted invite from user 3 to user 2 describe the same connection.
Write a function that takes the list of events and returns one record per distinct connected pair, together with the date the connection first became active.
Rules
- Only events whose
accept_dateis notNonecount. Skip events where it isNone. Any other value (including an empty string) is treated as a real date. - For each counted event, build the pair
(smaller_id, larger_id)from its two ids, regardless of who sent the invite. - If a pair has several counted events, use the smallest
accept_dateamong them. Dates are ISO-formattedYYYY-MM-DDstrings, so ordinary string comparison orders them chronologically. - Return a list of dictionaries, one per distinct pair, each with exactly these keys:
requester_id: the smaller id of the pair.accepter_id: the larger id of the pair.accept_date: the earliest accepted date for the pair.
- Sort the returned list ascending by
(requester_id, accepter_id). The order is by pair, not by date. - If no event counts, return an empty list.
- Do not filter or special-case events where both ids are equal; they are handled like any other pair
(a, a).
Constraints
0 <= len(friend_events) <= 10^5- User ids are integers.
- Every event has all four keys;
accept_dateis eitherNoneor aYYYY-MM-DDstring. - The input list must not be required to be sorted in any way.
Examples
Example 1
Input: merge_confirmed_pairs([{'requester_id': 1, 'accepter_id': 2, 'request_date': '2024-01-01', 'accept_date': '2024-01-02'}, {'requester_id': 2, 'accepter_id': 3, 'request_date': '2024-01-03', 'accept_date': '2024-01-05'}, {'requester_id': 3, 'accepter_id': 2, 'request_date': '2024-01-04', 'accept_date': '2024-01-05'}, {'requester_id': 2, 'accepter_id': 1, 'request_date': '2024-01-06', 'accept_date': '2024-01-07'}, {'requester_id': 4, 'accepter_id': 1, 'request_date': '2024-01-08', 'accept_date': '2024-01-09'}])
Output: [{'requester_id': 1, 'accepter_id': 2, 'accept_date': '2024-01-02'}, {'requester_id': 1, 'accepter_id': 4, 'accept_date': '2024-01-09'}, {'requester_id': 2, 'accepter_id': 3, 'accept_date': '2024-01-05'}]
Example 2
Input: merge_confirmed_pairs([{'requester_id': 5, 'accepter_id': 2, 'request_date': '2024-02-01', 'accept_date': '2024-02-04'}, {'requester_id': 2, 'accepter_id': 5, 'request_date': '2024-02-02', 'accept_date': '2024-02-03'}, {'requester_id': 5, 'accepter_id': 2, 'request_date': '2024-02-03', 'accept_date': None}])
Output: [{'requester_id': 2, 'accepter_id': 5, 'accept_date': '2024-02-03'}]
Example 3
Input: merge_confirmed_pairs([{'requester_id': 10, 'accepter_id': 11, 'request_date': '2024-03-01', 'accept_date': None}, {'requester_id': 12, 'accepter_id': 10, 'request_date': '2024-03-02', 'accept_date': '2024-03-04'}])
Output: [{'requester_id': 10, 'accepter_id': 12, 'accept_date': '2024-03-04'}]
Hints
Hint 1
Two events between the same people in opposite directions should land on the same key. Think about how to turn (a, b) and (b, a) into one canonical form.
Hint 2
A dictionary keyed by the canonical pair can hold the best date seen so far. Update it only when a strictly smaller date arrives.
Hint 3
Skip pending events before touching the dictionary, and sort the dictionary's keys at the end so the output order does not depend on input order.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The connection is undirected, so the first step is to give both directions of the same pair one canonical form: (smaller_id, larger_id). Once every accepted event is mapped to that key, the task becomes "keep the minimum date per key".
- Walk through the events and skip any whose
accept_dateisNone. - Compute the canonical key by sorting the two ids.
- Store the date in a dictionary, replacing the stored value only if the new date is strictly smaller.
- After the pass, sort the dictionary keys and build one output record per key.
Why it is correct
- Every accepted pair appears: each counted event writes to the dictionary under its canonical key.
- No pair appears twice: dictionary keys are unique, and both directions of a connection share the same canonical key.
- The date is right: the stored value is only ever lowered, so after the loop it equals the minimum accepted date for that pair, which is when the connection first became active.
- Pending events are ignored: they are skipped before any dictionary update.
- Output is deterministic: sorting the keys fixes the order independent of input order. Because ISO dates compare lexicographically in chronological order, string comparison gives the correct minimum.
Complexity
- Time: O(n + k log k), where n is the number of events and k the number of distinct pairs (one linear pass plus sorting the keys).
- Space: O(k) for the dictionary and the output.
Reference solution
from typing import List, Dict
def merge_confirmed_pairs(friend_events: List[Dict]) -> List[Dict]:
earliest = {}
for event in friend_events:
accept_date = event.get("accept_date")
if accept_date is None:
continue
low, high = sorted((event["requester_id"], event["accepter_id"]))
key = (low, high)
if key not in earliest or accept_date < earliest[key]:
earliest[key] = accept_date
return [
{
"requester_id": low,
"accepter_id": high,
"accept_date": earliest[(low, high)],
}
for low, high in sorted(earliest)
]