Algorithms Quiz: Medium: Trie Prefix Matching

A prefix tree stores words as paths from root. Given the set {cat, car, canine, cart}, which operation best guarantees a match for the prefix ‘ca’ while avoiding false positives beyond existing words?

  • Check if any word starts with ‘ca’ by counting children
  • Search for ‘ca’ exactly and return associated word
  • Look up a hash map of two-letter keys for prefixes
  • Traverse from root using ‘c’ then ‘a’ and check terminal flag
0 voters

Vote, then reply with how you reasoned it out. The answer goes up in about a day.

Algorithms Quiz answer: Option 4 (D).

Correct choice: Traverse from root using ‘c’ then ‘a’ and check terminal flag

Why:
Option 1 correctly traverses the trie following the prefix letters and then checks for a terminal node to ensure a valid word exists. Options 2 and 3 either require exact matches or do not confirm a complete word. Option 4 ignores the trie structure and uses a separate map, which defeats the purpose of the prefix tree for partial matching.

Go deeper:

Tries Prefix Tree

First-answer leaderboard

  1. @emmawalter5 - 15 (firsts) :trophy:
  2. @Apexcodes - 9 (firsts)
  3. @kirupa - 8 (firsts)
  4. @PipMuse - 5 (firsts)
  5. @adnanahmed - 2 (firsts)