Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE

Problem: A trie stores the words "apple", "app", "apply", "ape", "banana". How many stored words start with the prefix "app" (including a word equal to the prefix)?

  1. A. 4
  2. B. 2
  3. C. 5
  4. D. 3

Correct answer: D. 3

A trie answers prefix queries by walking the prefix and counting the words below that node. Counting stored words that merely share the first letter 'a' overcounts with 'ape'.

More Coding Interview Problems questions

Exams that ask this topic