KatInterview

Top Five Senders and Receivers From Signed Transfer Logs

Arrays & HashingAuto-graded · 22 test cases
EasyAuto-gradedArrays & Hashinghash-mapcountingheap

Last updated

Count senders and receivers from signed transfer records in a single pass, then pick the five most frequent on each side with a deterministic tie-break.

Top Five Senders and Receivers From Signed Transfer Logs

You are given a collection of transfer records. Each record is a triple (record_id, pair, amount):

  • record_id is an integer identifier. It plays no part in the answer.
  • pair is a string of the form "a:b", where a and b are integer user ids.
  • amount is an integer that may be negative, zero, or positive.

Direction rule

  • If amount >= 0 (zero included), the record is read as written: user a is the sender and user b is the receiver.
  • If amount < 0, the direction is reversed: user b is the sender and user a is the receiver.

Only the direction matters. The size of the amount is never used.

Task

Implement a function that returns two lists:

  1. The ids of the 5 users who sent the most transfers.
  2. The ids of the 5 users who received the most transfers.

Both lists are ordered by transfer count descending. Users with equal counts are ordered by user id ascending.

Every record counts exactly once toward one sender and once toward one receiver. Records are never deduplicated, even if record_id values repeat. A self-transfer such as "3:3" counts as one send and one receive for user 3.

Only users with a count of at least 1 on a given side can appear in that side's list. If fewer than 5 such users exist, return the shorter list without padding. For empty input, return two empty lists.

The return value is a pair (top_senders, top_receivers).

Aim for O(n) time and O(u) extra space, where u is the number of distinct users. Process each record as it arrives and do not keep per-record data around.

Constraints

  • 0 <= n <= 10^6 records.
  • User ids are integers, parsed from the pair string (which has exactly one :).
  • amount is an integer; amount == 0 is treated as non-negative.
  • The input may be any iterable of records, so make a single pass over it.
  • The output must be deterministic and follow the stated tie-break exactly.

Examples

Example 1

Input: top_senders_and_receivers([[1001, '1:2', 50], [1002, '2:1', -20], [1003, '3:1', 10], [1004, '1:3', -5], [1005, '4:5', 15]])

Output: [[1, 3, 4], [1, 2, 5]]

Example 2

Input: top_senders_and_receivers([[1, '10:20', 100], [2, '20:30', -50], [3, '10:30', 0], [4, '30:10', -10], [5, '20:10', 25]])

Output: [[10, 20, 30], [20, 30, 10]]

Hints

Hint 1

Apply the direction rule as soon as you parse a record, so that afterwards you only deal with a (sender, receiver) pair.

Hint 2

Keep two hash-map counters, one for sends and one for receives. Each record increments exactly one entry in each.

Hint 3

Rank by the key (-count, user_id). Taking the 5 smallest keys gives the required order directly, and a heap-based selection avoids sorting all users.