TRIE-P1

A trie

Practice 1 of 5Tries8 tests

The problem

Write a trie class (lessons 1 and 2). insert(word) stores a word. search(word) returns True if the word was inserted. starts_with(prefix) returns True if at least one inserted word starts with the prefix.

t = Trie()
t.insert("apple")
t.search("apple")       # True
t.search("app")         # False: only a prefix
t.starts_with("app")    # True
t.insert("app")
t.search("app")         # True

Words and prefixes are non-empty and use the letters a to z. The same word may be inserted more than once.

Limits

  • Up to 30,000 words and 60,000 searches, each word at most 7 letters. Every operation should take O(L) time for a word of length L, whatever the number of stored words.

Builds on TRIE-1 Prefix trees and TRIE-2 Insert, search and delete.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

8 tests, not run yet
Test results
#TestResultReads · time
1Word and prefixNot run
2Shared pathsNot run
3Empty trieNot run
4Longer than any wordNot run
5Inserted twiceNot run
6Word inside a wordNot run
7One letter wordsNot run
830,000 wordsNot run