As reported: ServiceNow Senior Software Engineer loop, round 1 (coding), Apr 2025: after a matrix search problem, the candidate discussed the approach for this log problem; a function that parses the log file into
(user, word count)entries was given.
Each log line has the form <timestamp> <user> <message words…>, separated by spaces. A user's total is the number of message words across all their lines. Return the n users with the largest totals, as [user, total] pairs in descending order of total.
Example
logs = ["10.10 John ABC def",
"10.11 Marie ABC",
"10.20 Marie Q W E",
"10.23 John ABC def"]
n = 1 -> [["John",4]]
n = 2 -> [["John",4],["Marie",4]]
Constraints: 1 ≤ logs.length ≤ 2 · 10^4; 1 ≤ n; user names have no spaces; a message may be empty.
Code runner format: line 1 logs (JSON array of strings), line 2 n. Implement topUsers(logs, n) returning (user, total) pairs; the harness prints them as [user, total] arrays.
Clarifications
- Ties in total are ordered by user name (by character code, so
"Bob"comes before"adam"). - If there are fewer than
nusers, return all of them.