KatInterview

Merge Mutual Connections Into Earliest-Confirmed Pairs

Arrays & HashingAuto-graded · 18 test cases
MediumAuto-gradedArrays & Hashinghash-mapgroupingnormalization

Last 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, or None if 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

  1. Only events whose accept_date is not None count. Skip events where it is None. Any other value (including an empty string) is treated as a real date.
  2. For each counted event, build the pair (smaller_id, larger_id) from its two ids, regardless of who sent the invite.
  3. If a pair has several counted events, use the smallest accept_date among them. Dates are ISO-formatted YYYY-MM-DD strings, so ordinary string comparison orders them chronologically.
  4. 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.
  5. Sort the returned list ascending by (requester_id, accepter_id). The order is by pair, not by date.
  6. If no event counts, return an empty list.
  7. 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_date is either None or a YYYY-MM-DD string.
  • 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.