KatInterview

Confirmed Connections from Invites and Replies

Arrays & HashingAuto-graded · 16 test cases
MediumAuto-gradedArrays & Hashinghash-mapgroupingdeduplication

Last 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 means replier_id accepted an invite that sender_id sent 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

  1. Valid reply. A reply [replier_id, sender_id, reply_date] is valid only if at least one invite exists with sender_id equal to the reply's sender_id, receiver_id equal to the reply's replier_id, and invite_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.
  2. Ignore invalid replies. Replies with no matching invite, or whose only matching invites are dated after the reply, are ignored.
  3. Unordered pairs. Each friendship is an unordered pair. Let user_a = min(sender_id, replier_id) and user_b = max(sender_id, replier_id). Valid replies in either direction between the same two users belong to the same pair.
  4. Earliest date. For each pair, friendship_date is the earliest reply_date among all of its valid replies.
  5. 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-DD strings.
  • The friendship_date value 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.