Extract Hashtags and Mentions From Video Captions

Last updated: November 29, 2026

Quick Overview

Given a TikTok video caption, return the distinct hashtags and @mentions it contains, lowercased and in first-seen order. A tag must start at the beginning of the caption or after whitespace; hashtags use letters, digits and underscores, and mentions may also contain dots but never end with one.

TikTok
Coding & Algorithms
Software Engineer
TikTok
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given a TikTok video caption, return the distinct hashtags and @mentions it contains, lowercased and in first-seen order. A tag must start at the beginning of the caption or after whitespace; hashtags use letters, digits and underscores, and mentions may also contain dots but never end with one.

This is a typical warm-up in TikTok's online assessment or first technical screen, framed around the caption pipeline that feeds hashtag pages and creator notifications. The interviewer checks that you pin down tokenization rules before coding and handle the boundary cases (embedded @ in emails, doubled #, trailing punctuation) without reaching for a fragile regex.

What the Interviewer Expects
  • Ask what characters are legal and what counts as a tag boundary before writing code
  • Single left-to-right scan with an index, no backtracking
  • Case-insensitive de-duplication that preserves first-seen order
  • Correct handling of emails, repeated # and trailing dots on mentions
  • Clean separation between the two token types
Key Topics to Cover
string parsing
tokenization
hash sets
order-preserving dedupe
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you support non-Latin hashtags such as #美食 or Arabic script?
  • If captions arrive at 50k per second, where would this run and how would you test it?
  • How would you also return the character offsets so the client can highlight tags?
  • Would a regex be acceptable here, and what would it look like?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input is one caption string. Output is two lists: hashtags and mentions, lowercased, de-duplicated, in the order they first appear. A tag is valid only if its sigil (# or @) is at index 0 or follo...

High-Level Design

Scan the string once with an index. When I hit a sigil at a legal boundary, I extend a second pointer while characters are in the allowed set for that sigil, slice the body, normalize it, and add it t...


Submit Your Answer
Markdown supported