Count Distinct Message Mentions
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
ZipRecruiter reported this one in October 2022, and it looks like a string-parsing warm-up until you read the constraints. Both members and messages go up to 100000, so scanning every message for every member is dead on arrival. The real task is hash-table counting with per-message dedup, plus a custom sort on the output. If the OA lands in your inbox soon, this is the shape to expect. StealthCoder sits invisibly on your screen as a safety net if you blank on the parsing details mid-assessment.
The problem
Given member IDs and messages, a mention group is a whitespace-delimited token beginning with @; its remainder may contain comma-separated IDs. Count each member at most once per message, ignore unknown or unprefixed IDs, and include zero-count members. Return id=count strings sorted by count descending, then ID ascending. Function countMemberMentions(members: String[], messages: String[]) → String[] Examples Example 1 members = ["id1","id2","id8"] messages = ["@id1 @id8,id8","hello @id8"] return = ["id8=2","id1=1","id2=0"] id8 is counted once in each message despite its duplicate first-message mention. Example 2 members = ["a","b"] messages = ["a @b"] return = ["b=1","a=0"] The unprefixed a is ignored. Constraints 1 <= members.length,messages.length <= 100000 Member IDs contain letters and digits and contain no commas or spaces.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a hash map from member ID to count, initialized to zero so silent members still show up. For each message, split on whitespace. For each token starting with @, strip the @, split the remainder on commas, and add every ID that's a known member to a per-message set. After the message, increment the count once for each ID in that set. That's the dedup trick, and it's where most wrong answers come from. The pitfalls: counting duplicates like @id8,id8, counting unprefixed tokens like the bare a in example 2, and counting IDs not in members. Then sort by count descending, ID ascending, and format as id=count. Total work is linear in message length plus an n log n sort. If the tokenizing rules get fuzzy under pressure, StealthCoder is the hedge during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Count Distinct Message Mentions cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Distinct Message Mentions FAQ
What's the trick in Count Distinct Message Mentions?+
Use a hash map of member counts and a fresh set per message. Add each valid mentioned ID to the set, then bump counts once per set entry. That handles duplicates like @id8,id8 without extra logic.
Why does brute force fail here?+
With up to 100000 members and 100000 messages, checking every member against every message is around 10 billion operations. Parsing each message once and looking IDs up in a hash map keeps it near linear.
How do I handle tokens like @id1,id2,id3?+
Split on whitespace first. If a token starts with @, drop that character and split the rest on commas. Every piece is a candidate ID. Only the @ marks a mention group, so the later IDs don't need their own prefix.
Do members with zero mentions appear in the output?+
Yes. Initialize every member to zero before reading messages. Example 1 returns id2=0 at the end. Unknown IDs never get added, so they can't sneak into the result.
How should I prep for this in 48 hours?+
Practice tokenizing strings, per-item dedup with sets, and sorting with a two-key comparator. Write the comparator by hand: count descending, then ID ascending. Test with both examples, especially the unprefixed a case.