FastPrepTop K Most Talkative Chat Users

Top K Most Talkative Chat Users

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Each chat-log line has the form timestamp username message text, separated by ASCII spaces. The timestamp and username are the first two tokens; the remaining text may contain spaces.

A user's talkativeness is their number of log lines. Return up to k usernames ordered by decreasing message count, breaking ties by lexicographically smaller username.

Malformed lines with fewer than three tokens are ignored.

Function

topTalkativeUsers(logs: String[], k: int) → String[]

Examples

Example 1

logs = ["1 alice hi","2 bob hello","3 alice again"]k = 2return = ["alice","bob"]

Alice has two messages and Bob has one.

Example 2

logs = ["1 z x","2 a y"]k = 1return = ["a"]

The counts tie, so a wins lexicographically.

Example 3

logs = []k = 5return = []

No log lines produce no users.

Constraints

  • 0 <= logs.length <= 200000.
  • 0 <= k <= 100000.
  • Valid timestamps and usernames contain no spaces.

More Google problems

See Google hiring insights
public String[] topTalkativeUsers(String[] logs, int k) {
    // Write your code here.
}
logs["1 alice hi","2 bob hello","3 alice again"]
k2
expected["alice", "bob"]
Checking account…