Prefix Counter Trie Supporting Erase for a Tag Registry

Last updated: November 29, 2026

Quick Overview

Implement a trie supporting insert(word), count_equal(word), count_prefix(prefix) and erase(word), where words may be inserted multiple times and erase removes one copy.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Implement a trie supporting insert(word), count_equal(word), count_prefix(prefix) and erase(word), where words may be inserted multiple times and erase removes one copy.

A Google technical screen question about a tag registry where labels are added and removed often and dashboards ask how many tags start with a prefix. The interviewer checks multiset semantics and that erase keeps every pass-through count consistent.

What the Interviewer Expects
  • Store pass-through and end counts at each node
  • Update counts along the full path on insert and erase
  • Guard erase against words not present
  • Optionally prune nodes whose pass count drops to zero
  • Give per-operation complexity
Key Topics to Cover
trie
multiset
counting
deletion
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 make erase reclaim memory?
  • How would you support listing the k most common words under a prefix?
  • How would you make this thread safe with fine-grained locks?
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

insert(w) adds one copy. count_equal(w) returns copies of w. count_prefix(p) returns the number of stored copies (with multiplicity) starting with p. erase(w) removes one copy if any exist...

High-Level Design

Each node holds pass (number of stored copies whose path goes through or ends at this node) and end (copies ending exactly here). Insert walks the word, creating nodes as needed, incrementing `pas...


Submit Your Answer
Markdown supported