Parse @mentions in a chat message and return the mentioned user IDs in order

Quick Overview

Parse a chat message for @username mentions, match them against a list of registered users, and return each mentioned user's ID once, in order of first appearance. It tests string scanning, dictionary lookups and careful handling of punctuation, case sensitivity and repeated mentions.

Parse @mentions in a chat message and return the mentioned user IDs in order

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a chat `message` and a list of registered `users`. Find every valid @username mention in the message and return the matching user IDs in the order the mentions appear. A mention is an `@` followed by a username. Usernames contain only letters, digits and underscores. A mention is valid only when its username belongs to a registered user. If the same user is mentioned more than once, include their ID only once, at its first occurrence. Return an empty list when the message has no valid mentions. ### Function Signature ```python def extract_mention_ids(message: str, users: list[list]) -> list[int]: ``` ### Rules - Each user is a pair `[user_id, username]`. - The username of a mention is the longest run of ASCII letters, digits and underscores right after the `@`. So `@alice_x` mentions `alice_x` (never `alice`), and `@alice!` mentions `alice`. - Any `@` can start a mention, whatever character comes before it, so `x@al` contains a mention of `al`. An `@` with no username character right after it is not a mention. - Matching is case-sensitive: `@Alice` does not match the user `alice`. - Return the IDs ordered by the position of each user's first valid mention. ### Constraints - `1 <= len(message) <= 10^4` - `0 <= len(users) <= 1000` - Usernames consist of letters, digits and underscores. They are unique and case-sensitive. - `user_id` values are unique positive integers. ### Examples ```text Input: message = "Hey @alice and @bob, can you review this? Also cc @charlie", users = [[1, "alice"], [2, "bob"], [3, "charlie"], [4, "dave"]] Output: [1, 2, 3] ``` The message mentions alice, bob and charlie in that order, and all three are registered. The comma after `@bob` ends that username. dave is registered but never mentioned. ```text Input: message = "@alice please help @bob. @alice are you there? @unknown_user", users = [[10, "alice"], [20, "bob"], [30, "charlie"]] Output: [10, 20] ``` alice is mentioned twice, so ID 10 appears once, at her first mention. `unknown_user` is not a registered user, so that mention is ignored. ```text Input: message = "@al_x, @Al and @alice! mail me at x@al", users = [[5, "alice"], [7, "al"]] Output: [5, 7] ``` `@al_x` names `al_x`, which is not registered, and `@Al` does not match `al` because case matters. `@alice` gives 5, and the `@al` inside `x@al` gives 7.

Overview: Parse a chat message for @username mentions, match them against a list of registered users, and return each mentioned user's ID once, in order of first appearance. It tests string scanning, dictionary lookups and careful handling of punctuation, case sensitivity and repeated mentions.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a chat message and a list of registered users. Find every valid @username mention in the message and return the matching user IDs in the order the mentions appear.

A mention is an @ followed by a username. Usernames contain only letters, digits and underscores. A mention is valid only when its username belongs to a registered user. If the same user is mentioned more than once, include their ID only once, at its first occurrence. Return an empty list when the message has no valid mentions.

Function Signature

def extract_mention_ids(message: str, users: list[list]) -> list[int]:

Rules

  • Each user is a pair [user_id, username] .
  • The username of a mention is the longest run of ASCII letters, digits and underscores right after the @ . So @alice_x mentions alice_x (never alice ), and @alice! mentions alice .
  • Any @ can start a mention, whatever character comes before it, so x@al contains a mention of al . An @ with no username character right after it is not a mention.
  • Matching is case-sensitive: @Alice does not match the user alice .
  • Return the IDs ordered by the position of each user's first valid mention.

Constraints

  • 1 <= len(message) <= 10^4
  • 0 <= len(users) <= 1000
  • Usernames consist of letters, digits and underscores. They are unique and case-sensitive.
  • user_id values are unique positive integers.

Examples

Input: message = "Hey @alice and @bob, can you review this? Also cc @charlie", users = [[1, "alice"], [2, "bob"], [3, "charlie"], [4, "dave"]]
Output: [1, 2, 3]

The message mentions alice, bob and charlie in that order, and all three are registered. The comma after @bob ends that username. dave is registered but never mentioned.

Input: message = "@alice please help @bob. @alice are you there? @unknown_user", users = [[10, "alice"], [20, "bob"], [30, "charlie"]]
Output: [10, 20]

alice is mentioned twice, so ID 10 appears once, at her first mention. unknown_user is not a registered user, so that mention is ignored.

Input: message = "@al_x, @Al and @alice! mail me at x@al", users = [[5, "alice"], [7, "al"]]
Output: [5, 7]

@al_x names al_x, which is not registered, and @Al does not match al because case matters. @alice gives 5, and the @al inside x@al gives 7.