Count Toutiao Search Queries Sharing Each Typed Prefix

Last updated: November 29, 2026

Quick Overview

Implement a structure over logged search queries that supports insert, erase of one occurrence, counting exact matches, and counting how many stored queries start with a given prefix.

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

0

0

0 solved


Implement a structure over logged search queries that supports insert, erase of one occurrence, counting exact matches, and counting how many stored queries start with a given prefix.

Toutiao and Douyin search use prefix statistics to decide when the suggestion box should appear and which candidates are worth ranking. This is a typical online assessment warm-up: it checks that you can write a trie with counters from scratch and keep counts consistent when items are removed.

What the Interviewer Expects
  • Implement a trie with per-node pass counts and end counts
  • Support duplicate inserts correctly
  • Make erase a no-op for absent words
  • Prune empty branches to keep memory bounded
  • Analyze time per operation in terms of word length
Key Topics to Cover
trie
prefix counting
data structure design
string processing
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 return the top three completions per prefix in O(prefix length)?
  • How would you shard this trie across machines for billions of queries?
  • What changes if queries contain Chinese characters instead of ASCII?
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

Operations: insert(word), count_equal(word), count_prefix(prefix), and erase(word) which removes one occurrence and returns whether anything was removed. Duplicates count separately. Words are...

High-Level Design

Each trie node keeps pass_cnt, the number of stored words whose path passes through it, and end_cnt, the number that end exactly there. Insert increments pass counts along the path and the end cou...


Submit Your Answer
Markdown supported