Hashtag Trie Supporting Insert, Erase, and Prefix Counts
Last updated: November 29, 2026
Quick Overview
Implement a structure for hashtags that supports insert(word), erase(word) which removes one copy, count_equal(word) returning how many copies are stored, and count_prefix(p) returning how many stored copies start with p.
Meta
November 29, 20260
0
0 solved
Implement a structure for hashtags that supports insert(word), erase(word) which removes one copy, count_equal(word) returning how many copies are stored, and count_prefix(p) returning how many stored copies start with p.
Instagram and Threads track hashtag usage, and a counting trie is a compact way to answer how many tags share a prefix as users type. Interviewers use this to check you can maintain counts on every node, handle deletion correctly, and free memory when a branch empties.
What the Interviewer Expects
- Store a pass-through count and an end count on each node
- Make erase a no-op when the word is not present
- Prune nodes whose pass count drops to zero
- Keep all operations linear in the word length
- Write a few calls to verify counts after mixed inserts and erases
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the top hashtags for a prefix, not just a count?
- How would you shard this if hashtags do not fit on one machine?
- What if counts must decay over time for trending?
- How does memory compare with a hash map of all prefixes?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Words are non-empty strings of lowercase letters and digits.
- Multiple copies are allowed, so counts can exceed one.
eraseremoves a single copy and only if one exists.- All four operations sh...
High-Level Design
Each node keeps pas, the number of stored copies whose path goes through it, and end, the number of copies that end exactly at it. insert walks and creates nodes, incrementing pas on every nod...