Confirmed Connections from Invites and Replies
Arrays & HashingAuto-graded · 16 test casesLast updated
Match reply events to earlier directed invites, normalize each user pair, and keep the earliest valid reply date per pair, sorted by pair.
A social app records two kinds of events.
- Invites: each is a row
[invite_id, sender_id, receiver_id, invite_date]. - Replies: each is a row
[replier_id, sender_id, reply_date]. It meansreplier_idaccepted an invite thatsender_idsent to them.
Dates are ISO strings in YYYY-MM-DD format, so comparing them as strings gives the same result as comparing them chronologically.
Implement confirmed_connections(invites, replies), which returns the list of confirmed friendships.
Rules
- Valid reply. A reply
[replier_id, sender_id, reply_date]is valid only if at least one invite exists withsender_idequal to the reply'ssender_id,receiver_idequal to the reply'sreplier_id, andinvite_date <= reply_date. The invite must go in the same direction the reply refers to. An invite sent the opposite way (replier to sender) does not validate it. - Ignore invalid replies. Replies with no matching invite, or whose only matching invites are dated after the reply, are ignored.
- Unordered pairs. Each friendship is an unordered pair. Let
user_a = min(sender_id, replier_id)anduser_b = max(sender_id, replier_id). Valid replies in either direction between the same two users belong to the same pair. - Earliest date. For each pair,
friendship_dateis the earliestreply_dateamong all of its valid replies. - Output. Return one dictionary per pair with keys
"user_a","user_b"and"friendship_date", sorted ascending by(user_a, user_b). If no reply is valid, return an empty list.
Duplicate invites, or invites in both directions, must never produce duplicate output rows.
Constraints
0 <= len(invites), len(replies) <= 10^5- User ids are integers.
- Dates are valid
YYYY-MM-DDstrings. - The
friendship_datevalue in the output is the original date string. - Each pair appears at most once in the output.
Examples
Example 1
Input: confirmed_connections([[1, 101, 102, '2025-08-01'], [2, 102, 101, '2025-08-02'], [3, 103, 104, '2025-08-05'], [4, 105, 106, '2025-08-10'], [5, 105, 106, '2025-08-12'], [6, 107, 108, '2025-08-15']], [[102, 101, '2025-08-03'], [104, 103, '2025-08-06'], [106, 105, '2025-08-11'], [108, 107, '2025-08-14'], [106, 105, '2025-08-13']])
Output: [{'user_a': 101, 'user_b': 102, 'friendship_date': '2025-08-03'}, {'user_a': 103, 'user_b': 104, 'friendship_date': '2025-08-06'}, {'user_a': 105, 'user_b': 106, 'friendship_date': '2025-08-11'}]
Hints
Hint 1
Index the invites by their directed pair (sender, receiver). You only need to know whether some invite in that direction is on or before the reply date, so storing the earliest date per directed pair is enough.
Hint 2
Validate each reply with a single lookup of (sender_id, replier_id) in that structure. The opposite direction (replier, sender) is a different key and must not count.
Hint 3
Normalize the key as (min, max) of the two ids, keep the smallest valid reply date per key in a dictionary, then sort the keys and build the output dictionaries.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A reply creates a friendship, and an invite only serves as evidence that the reply is legitimate.
- Earliest invite per directed pair. Walk through the invites and store, for each
(sender_id, receiver_id), the smallestinvite_date. An invite on or before a reply exists exactly when the earliest invite in that direction is on or before the reply date. Collapsing duplicates to one entry also means nothing gets counted twice. - Validate each reply. For a reply
[replier_id, sender_id, reply_date], look up(sender_id, replier_id). If the key is missing, or its earliest date is later thanreply_date, skip the reply. - Normalize the pair. Use
(min(a, b), max(a, b))as the key so replies in both directions land on the same pair. - Keep the minimum. For each pair, keep the smallest valid
reply_date. - Format. Sort the pairs and emit one dictionary per pair with keys
user_a,user_bandfriendship_date.
Why it is correct
- Looking up the same-direction key enforces rule 1, and skipping failures enforces rule 2.
- The normalized key guarantees one row per unordered pair (rule 3), even if both users sent invites or both replied.
- Taking the minimum over valid replies enforces rule 4.
- Sorting the keys gives the required output order.
Complexity
Let I be the number of invites, R the number of replies and P the number of output pairs.
- Time:
O(I + R + P log P). - Space:
O(I + P).
Reference solution
from typing import Dict, List
def confirmed_connections(invites: List[List], replies: List[List]) -> List[Dict]:
# Earliest invite date for each directed (sender, receiver) pair
first_invite = {}
for _invite_id, sender_id, receiver_id, invite_date in invites:
key = (sender_id, receiver_id)
if key not in first_invite or invite_date < first_invite[key]:
first_invite[key] = invite_date
earliest = {}
for replier_id, sender_id, reply_date in replies:
first = first_invite.get((sender_id, replier_id))
if first is None or first > reply_date:
continue
pair = (min(sender_id, replier_id), max(sender_id, replier_id))
if pair not in earliest or reply_date < earliest[pair]:
earliest[pair] = reply_date
return [
{"user_a": a, "user_b": b, "friendship_date": d}
for (a, b), d in sorted(earliest.items())
]