Skip to content

Most Shared Account

01 · Question

You receive a list of (IP address, username) connections. IP addresses are unique, but a username may appear more than once.

Return the username with the most connections. If several usernames tie, return any of them.

Example:

  • Input: [("203.0.113.16", "mike"), ("198.51.100.25", "bob"), ("192.0.2.5", "mike")]
  • Output: "mike"

02 · Analysis

Use a frequency map from username -> connection count. Build the counts in one pass, then scan the map for the largest value.

The second pass visits at most as many usernames as there are connections, so the total remains O(n)O(n) time. The map uses O(U)O(U) space for UU unique usernames.

03 · Solution

Reference solution

1def most_shared_account(connections):
2 counts = {}
3 for _, username in connections:
4 counts[username] = counts.get(username, 0) + 1
5
6 best = None
7 for username, count in counts.items():
8 if best is None or count > counts[best]:
9 best = username
10 return best