Count User Mentions in Chat Messages
Updated
Problem statement
You are given the chat messages of a team as an array messages. Each message is a list of tokens separated by single spaces. A token that starts with @ is a mention: the @ is followed by one or more user ids separated by commas, so the token "@ann,bob" mentions both ann and bob. Every other token is an ordinary word.
For every user who is mentioned at least once, count how many messages mention them. If one message mentions the same user more than once, in the same token or in different tokens, it still adds only 1 to that user's count.
Return the counts as strings of the form "id=count", sorted by count from highest to lowest. Users with the same count are sorted by id in ascending lexicographic (dictionary) order. Users who are never mentioned do not appear, so if nobody is mentioned, return an empty array.
Function
countMentions(messages: String[]) → String[]Examples
Example 1
messages = ["hi @ann,bob lunch at noon","@bob can you review","thanks @carl @bob"]return = ["bob=3","ann=1","carl=1"]- Message 1 mentions
annandbobwith one@. - Message 2 mentions
bob; message 3 mentionscarlandbob. bobis mentioned in 3 messages,annandcarlin 1 each. The tie betweenannandcarlis broken by id, soanncomes first.
Example 2
messages = ["@dev1,dev2,dev1 ping @dev2","@dev2 done"]return = ["dev2=2","dev1=1"]- Message 1 names
dev1twice anddev2twice, but each user counts once per message. - Message 2 mentions
dev2again, sodev2has 2 anddev1has 1.
Example 3
messages = ["no mentions here","just text"]return = []- No message has a token that starts with
@, so nobody is mentioned and the result is empty.
Constraints
1 <= messages.length <= 10001 <= messages[i].length <= 100- Each message is a list of tokens separated by single spaces, with no space at the start or the end.
- A word token is one or more lowercase English letters or digits.
- A mention token is
@followed by one or more user ids separated by single commas, with no empty id. - Every user id is 1 to 8 lowercase English letters or digits.
More Ramp interview questions
- Count Overlapping Square PairsOA · Seen Nov 2026
- Accept or Decline QueriesOA · Seen Sep 2026
- Recipe Management SystemOA · Seen Mar 2026
- Task Manager with Timed AssignmentsOA · Seen Jan 2026
- Bank Requests with Delayed CashbackOA · Seen Dec 2025
- Convert Snake Case Variables to Camel CaseOA · Seen Apr 2025
- Correct Apartment Bedroom CountsOA · Seen Feb 2025
- Retryable URL MazePHONE SCREEN · Seen Oct 2026
- Detect Recurring Transactions with Tunable TolerancesONSITE INTERVIEW · Seen Jul 2026
- Find a User’s Airport from Flight HistoryPHONE SCREEN · Seen Sep 2025
Similar questions at other companies
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Frequently Bought TogetherAmazon · OA
- Aggregate Paginated Project Run RecordsReplit · OA
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- Filter Anagrams of a TargetMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Deduplicate Logs: Keep FirstGoogle · ONSITE INTERVIEWGoogle interview questions
- Remove Later Anagrams From a ListBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Most Voted CandidateExpedia · ONSITE INTERVIEWExpedia interview questions