Parse a chat message into mention, emoji, bold, code and text tokens

Quick Overview

Write a single-pass parser that splits a chat message into mention, emoji, bold, code and plain text tokens. It tests index-based string scanning, delimiter matching, merging adjacent text into one token, and edge cases such as a lone @ sign or an empty delimiter pair.

Parse a chat message into mention, emoji, bold, code and text tokens

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Implement a parser for Slack-style message markup. Given a string `message`, scan it and return a list of tokens, where each token is a two-element list `[type, content]`. The parser recognizes these patterns: - A user mention starts with `@` and continues with letters, digits or underscores. `@alice` produces `["mention", "alice"]`. - An emoji is enclosed in colons. `:smile:` produces `["emoji", "smile"]`. - Bold text is enclosed in single asterisks. `*bold text*` produces `["bold", "bold text"]`. - Inline code is enclosed in backticks. `` `code here` `` produces `["code", "code here"]`. - Any text that does not match one of these patterns is plain text, with type `"text"`. Adjacent plain text is merged into a single token, and tokens whose content would be an empty string are omitted. ### Function Signature ```python def parse_message(message: str) -> list[list[str]]: ``` ### Rules - Scan the message from left to right and return the tokens in the order they appear. - An `@` followed by one or more letters, digits or underscores starts a mention. The mention takes the longest such run, so it ends at the first other character (a space, a comma, the end of the string). An `@` that is not followed by a letter, digit or underscore is plain text. - `:`, `*` and `` ` `` open an emoji, a bold span and a code span. The span ends at the next occurrence of the same character, and the token content is everything between the two delimiters, taken literally. Delimiters and `@` inside a span are not parsed again. - A pair with nothing between the delimiters (such as `**`) produces no token. - Consecutive plain-text characters form one `"text"` token. This includes text on both sides of a lone `@` or an empty pair. No token in the output has empty content. ### Constraints - `1 <= len(message) <= 1000` - `message` consists of printable ASCII characters. - Formatting delimiters (`*`, `` ` ``, `:`) are always properly paired: whenever the scan reaches one of them outside a span, the same character appears again later in the message. - User mentions contain only letters, digits and underscores after the `@` symbol. ### Examples ```text Input: message = "Hello @alice, check :wave: and *important* stuff" Output: [["text","Hello "],["mention","alice"],["text",", check "],["emoji","wave"],["text"," and "],["bold","important"],["text"," stuff"]] ``` The mention stops at the comma because a comma is not a letter, digit or underscore. Each formatted span becomes its own token, and the text between spans keeps its spaces. ```text Input: message = "Run `npm install` then ping @dev_team :rocket:" Output: [["text","Run "],["code","npm install"],["text"," then ping "],["mention","dev_team"],["text"," "],["emoji","rocket"]] ``` The backtick pair becomes a code token with its inner space kept. The single space between the mention and the emoji is still a text token of its own. ```text Input: message = "Meet @ 5**pm, `*not bold*` @ann_2!" Output: [["text","Meet @ 5pm, "],["code","*not bold*"],["text"," "],["mention","ann_2"],["text","!"]] ``` The first `@` is followed by a space, so it stays in the text, and the empty `**` pair produces no token, which lets "5" and "pm" join one text run. The asterisks inside the backticks belong to the code content and are not parsed as bold.

Overview: Write a single-pass parser that splits a chat message into mention, emoji, bold, code and plain text tokens. It tests index-based string scanning, delimiter matching, merging adjacent text into one token, and edge cases such as a lone @ sign or an empty delimiter pair.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Implement a parser for Slack-style message markup. Given a string message, scan it and return a list of tokens, where each token is a two-element list [type, content].

The parser recognizes these patterns:

  • A user mention starts with @ and continues with letters, digits or underscores. @alice produces ["mention", "alice"] .
  • An emoji is enclosed in colons. :smile: produces ["emoji", "smile"] .
  • Bold text is enclosed in single asterisks. *bold text* produces ["bold", "bold text"] .
  • Inline code is enclosed in backticks. `code here` produces ["code", "code here"] .
  • Any text that does not match one of these patterns is plain text, with type "text" .

Adjacent plain text is merged into a single token, and tokens whose content would be an empty string are omitted.

Function Signature

def parse_message(message: str) -> list[list[str]]:

Rules

  • Scan the message from left to right and return the tokens in the order they appear.
  • An @ followed by one or more letters, digits or underscores starts a mention. The mention takes the longest such run, so it ends at the first other character (a space, a comma, the end of the string). An @ that is not followed by a letter, digit or underscore is plain text.
  • : , * and ` open an emoji, a bold span and a code span. The span ends at the next occurrence of the same character, and the token content is everything between the two delimiters, taken literally. Delimiters and @ inside a span are not parsed again.
  • A pair with nothing between the delimiters (such as ** ) produces no token.
  • Consecutive plain-text characters form one "text" token. This includes text on both sides of a lone @ or an empty pair. No token in the output has empty content.

Constraints

  • 1 <= len(message) <= 1000
  • message consists of printable ASCII characters.
  • Formatting delimiters ( * , ` , : ) are always properly paired: whenever the scan reaches one of them outside a span, the same character appears again later in the message.
  • User mentions contain only letters, digits and underscores after the @ symbol.

Examples

Input: message = "Hello @alice, check :wave: and *important* stuff"
Output: [["text","Hello "],["mention","alice"],["text",", check "],["emoji","wave"],["text"," and "],["bold","important"],["text"," stuff"]]

The mention stops at the comma because a comma is not a letter, digit or underscore. Each formatted span becomes its own token, and the text between spans keeps its spaces.

Input: message = "Run `npm install` then ping @dev_team :rocket:"
Output: [["text","Run "],["code","npm install"],["text"," then ping "],["mention","dev_team"],["text"," "],["emoji","rocket"]]

The backtick pair becomes a code token with its inner space kept. The single space between the mention and the emoji is still a text token of its own.

Input: message = "Meet @ 5**pm, `*not bold*` @ann_2!"
Output: [["text","Meet @ 5pm, "],["code","*not bold*"],["text"," "],["mention","ann_2"],["text","!"]]

The first @ is followed by a space, so it stays in the text, and the empty ** pair produces no token, which lets "5" and "pm" join one text run. The asterisks inside the backticks belong to the code content and are not parsed as bold.