Google questions

Most Active Users

GoogleMedium

A messaging service logs who talked to whom. Each log entry is a string of two user names joined by one hyphen, such as "ann-bo". Names are non-empty, case-sensitive, and contain only letters and digits.

Implement top_users(pairs) where pairs is the list of log entries.

Part 1: Rank users by activity

Every entry adds 1 to the activity count of both users in it.

Return the names of all users that appear in at least one entry, ordered by count from highest to lowest. Users with equal counts are ordered by name, ascending, using ordinary string comparison.

  1. Example 1
    pairs
    ["ann-bo","bo-cy","ann-cy","bo-dee","bo-eli"]
    Output
    ["bo","ann","cy","dee","eli"]

    Why: bo is in 4 pairs, ann and cy in 2 each, and dee and eli in 1 each. Ties are ordered by name, so the result is bo, ann, cy, dee, eli.

Constraints

  • 0 <= len(pairs) <= 10^5
  • pairs[i].split('-')[0] != pairs[i].split('-')[1]
  • For i != j, pairs[i].split('-') != pairs[j].split('-') and pairs[i].split('-') != pairs[j].split('-')[::-1]
  • 1 <= len(name) <= 20 for every name name in pairs[i].split('-')
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Most Active Users: Google interview question | LoopCoach