Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Implement Trie (Prefix Tree)

Medium
triedesignstring

Implement a trie supporting insertion, exact word search, and prefix search.

Input: one operation per line —

  • insert w — insert word w
  • search w — print true if w was inserted
  • startsWith p — print true if any inserted word starts with p

Output: one line per operation — insert prints null; the others print true/false.

Example 1
Input:
insert apple
search apple
search app
startsWith app
insert app
search app
Output:
null
true
false
true
null
true

Constraints

  • 1 ≤ word length ≤ 2000
  • Up to 3·10^4 operations; lowercase letters.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.