Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Trie Data Structure Implementation

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Trie Data Structure Implementation. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Quantcast may need to index keyword and prefix signals efficiently. Implement a trie with insert, search, and startsWith operations.

Create a Trie class with these methods:

  1. insert(word): Add a lowercase word to the trie.
  2. search(word): Return True only when the complete word has been inserted.
  3. startsWith(prefix): Return True when at least one inserted word begins with prefix.

For automated evaluation, implement process_trie_operations(operations). Each operation is a dictionary with op equal to insert, search, or startsWith, and a word field. The function must return results only for search and startsWith operations, in their original order.

Formal Specification

  • Input: A list of operation dictionaries, where each dictionary has op: str and word: str.
  • Output: A list of booleans produced by search and startsWith operations.
  • Words contain only lowercase English letters and are nonempty.

Constraints

  • 1 <= len(operations) <= 10^4
  • 1 <= len(word) <= 100
  • Words contain only lowercase English letters
  • At most 5 * 10^3 distinct words are inserted
  • Operations are processed in order

Function Signature

def process_trie_operations(operations):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output