FastPrepTrie: Add and Search Words
Problems/Trie: Add and Search Words

Trie: Add and Search Words

Adobe logoAdobe● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interviewer asked: "Write TrieNode/Trie to add a word, question around save 26 size of children, used map."

The report only mentions adding words, so there is nothing to check unless the trie can also be read. Here, add tells you whether the word was new, and you also answer search and prefix questions. Words use lowercase letters. You get the calls as a list of operations instead of a class.

The judged task matches the report at about 70%.

Build a trie (prefix tree) that stores words, and run a list of operations on it. The trie starts empty. Each string in operations is one of:

  • "add word": store word. The result is true if word was not stored before, and false if it was already stored.
  • "search word": the result is true if word itself has been added.
  • "prefix p": the result is true if at least one added word starts with p. A word counts as a prefix of itself.

Return one boolean per operation, in order.

Function

runTrie(operations: String[]) → boolean[]

Examples

Example 1

operations = ["add apple","search apple","search app","prefix app","add app","search app"]return = [true,true,false,true,true,true]

apple is new, so the first result is true, and searching for it is true. app has not been added yet, so search app is false, but apple starts with app, so prefix app is true. After add app (new, true), search app is true.

Example 2

operations = ["add cat","add cat","prefix cats","prefix c"]return = [true,false,false,true]

The second add cat returns false because cat is already stored. No stored word starts with cats, while cat starts with c.

Example 3

operations = ["search a","prefix a","add a","prefix a"]return = [false,false,true,true]

The trie is empty at first, so both queries are false. After adding a, the prefix a matches the word a itself.

Constraints

  • 1 <= operations.length <= 2 * 10^4
  • Each operation is "add w", "search w", or "prefix w", with exactly one space.
  • Each w has 1 to 50 lowercase English letters.

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public boolean[] runTrie(String[] operations) {
    // Write your code here.
}
Checking account…