Insert a word into a trie and confirm it is present.
Acceptance criteria
- 1.#out should show present.
DSA Tutorial · Tries
These 10 tasks go with the Tries (Prefix Trees) chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “A trie answers one question a hash map cannot: which words start with this? That is the whole reason it exists, and if you do not need prefixes you do not need a trie.” .
1.Insert and findexercise · easy · 10 min
Insert a word into a trie and confirm it is present.
Passes when: #out should show present.
2.Prefix but not a wordexercise · easy · 10 min · practice plan
Report that a prefix exists without being a complete word.
Passes when: #out should show prefix only.
3.Count childrenexercise · easy · 10 min · practice plan
Show how many branches the root has after two words.
Passes when: #out should show two branches.
4.Shared prefixexercise · easy · 10 min · practice plan
Add two words sharing a prefix and show the root branch count.
Passes when: #out should show one branch.
5.Collect wordsexercise · medium · 10 min · practice plan
Gather every word under a prefix and join them.
Passes when: #out should show car-card.
6.Fix the missing end flagbug fix · easy · 8 min
Without marking the last node, a stored word cannot be told from a prefix.
Passes when: #out should show present.
7.Fix the prefix treated as a wordbug fix · easy · 8 min · practice plan
Reaching a node does not mean a word ends there.
Passes when: #out should show prefix only.
8.Fix the node replaced on every insertbug fix · medium · 8 min · practice plan
Overwriting an existing child throws away the branch already built.
Passes when: #out should show two endings.
9.Fix the trie used with an objectbug fix · easy · 8 min · practice plan
A plain object inherits keys, so an unexpected prefix appears present.
Passes when: #out should show missing.
10.Fix the branch count read from the wrong nodebug fix · easy · 8 min · practice plan
Counting the root's children ignores where the words diverge.
Passes when: #out should show two endings.
Between them these tasks cover Insertion, Prefix search and End-of-word flags.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.