Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Tries

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Tries

Implement Trie (Prefix Tree)

intermediatelayer A5 · Tries5 tests

Implement a trie: a tree where each edge is a character, so words that share a prefix share a path.

  • insert(word) — add a word
  • search(word) — true only if that exact word was inserted
  • startsWith(prefix) — true if any inserted word begins with prefix
  • Every operation must run in O(length of the string), independent of how many words are stored — so no scanning a list of words
  • Words are lowercase letters. Inserting the same word twice is harmless

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen