KatInterview

Most Connected User Tracker with Variable Top-K Queries

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure Designhash-sethash-mapheap

Last updated

Maintain distinct-contact counts for users from undirected messages, answer the top user in O(1), and return a top-k list with deterministic tie-breaking.

You are building a small tracker for a messaging service. Messages are undirected: a message between a and b counts as a contact for both users. A user's score is the number of different other users they have exchanged at least one message with.

Implement a function that simulates the tracker. It receives a list of operations and a parallel list of argument lists. For each operation, it appends one entry to the result list.

Operations

  • "register" with arguments [a, b]: record a message between users a and b (both strings).
    • Both users become known to the tracker, even if they have no contacts yet.
    • Repeated messages between the same pair count only once toward each user's score. (a, b) and (b, a) are the same pair.
    • If a == b, the user becomes known, but the message adds no contact and their score does not change.
    • Result entry: None.
  • "most_active_user" with arguments []: return the known user with the highest score.
    • Ties are broken by choosing the lexicographically smallest user name (ordinary string comparison).
    • If no user is known yet, return None.
  • "top_k" with arguments [k]: return a list of the k best known users.
    • Order by score descending, then by name ascending for equal scores.
    • If k is larger than the number of known users, return all known users in that order.
    • If k <= 0 or no users are known, return an empty list.

The value of k may differ from call to call. The operations must be processed in the given order, and operations[i] uses arguments[i].

Return the list of result entries, one per operation, in order.

Constraints

  • operations and arguments have the same length.
  • User names are non-empty strings and are case-sensitive.
  • Operation names are exactly "register", "most_active_user", or "top_k".
  • k is an integer and may be zero, negative, or larger than the number of users.
  • Aim for O(1) work per most_active_user and an efficient top_k whose cost does not require sorting every user when k is small.

Examples

Example 1

Input: track_contacts(['most_active_user'], [[]])

Output: [None]

Example 2

Input: track_contacts(['register', 'register', 'most_active_user', 'top_k'], [['alice', 'bob'], ['alice', 'carol'], [], [3]])

Output: [None, None, 'alice', ['alice', 'bob', 'carol']]

Example 3

Input: track_contacts(['register', 'register', 'register', 'most_active_user', 'top_k'], [['z', 'a'], ['b', 'z'], ['a', 'b'], [], [2]])

Output: [None, None, None, 'a', ['a', 'b']]

Example 4

Input: track_contacts(['register', 'register', 'register', 'top_k', 'top_k'], [['x', 'y'], ['x', 'y'], ['x', 'x'], [10], [0]])

Output: [None, None, None, ['x', 'y'], []]

Hints

Hint 1

Keep, for each user, a set of distinct contacts. Add each side to the other's set, and only increase scores when the pair is genuinely new and the two names differ.

Hint 2

Scores only ever go up, and each register changes at most two users. So the current best user can only be replaced by one of those two users, which lets you keep the best user up to date with a couple of comparisons.

Hint 3

Compare users with the key (-score, name). For top_k, you can pick the k smallest keys with a heap-based selection, which costs O(n log k) instead of sorting everything.