Limited Time Offer:Up to 20% off Hello Interview Premium
Up to 20% off Hello Interview Premium 🎉
Hello Interview
Your Dashboard
System Design
Code
Low Level Design
Behavioral
AI Coding
New
ML System Design
Salary Negotiation
Interview Guides
Blog
System Design
Low Level Design
AI Coding
Behavioral
New
Interview Questions
Success Stories
System Design
Low-Level Design
New
Ask The Community
Discord
Mock Interviews
1:1 Mentorship
Refer a Friend
Pricing
Sign in / Sign up
Search
⌘K
Pricing

Tutor

Trie

Implement Trie Methods

medium

max (21)341224132;341225102;21365487109
Count: 10
abcValid triangle requires:a + b > c AND a + c > b AND b + c > a(every pair must sum to more than the third side)3511SOURCE23211SOURCE23UNREACHABLE$100$100$100$5000SRC123DST$100$100$1000SRC123DST01233141Threshold: 4Answer: 32 reachable01234231118Threshold: 2Answer: 01 reachable1102233321432263321
DESCRIPTION

Implement the search, startsWith, and delete methods of a Trie.

  • search(word) returns true if the word is in the Trie, and false otherwise.
  • startsWith(prefix) returns true if any word in the Trie starts with the given prefix, and false otherwise.
  • delete(word) removes the word from the Trie, and does not return a value.

The creation of the Trie and the insert method are already implemented for you.

The test cases include two parameters:

  • initialWords: a list of words to add to the Trie,
  • commands: a list of commands to run. Each command is a tuple, where the first element is "search", "startsWith", or "delete", and the second element is the word or prefix.

The test cases will create the Trie with the initial words, and then run the commands in order, and compare the output to the expected output. Note we only compare the output of search and startsWith commands, not delete commands.

Input:

initialWords = ["apple", "app", "apartment"]
commands = [
["search", "apple"],
["search", "apartment"],
["search", "appl"],
["delete", "app"],
["search", "app"],
]

Output: [True, True, False, False]

Explanation:

Trie.search("apple") -> True
Trie.search("apartment") -> True
Trie.search("appl") -> False
Trie.delete("app") -> None # Return value not checked
Trie.search("app") -> False

Explanation

Search

  • To search for a word in a trie, we start from the root node and check if the current node has a child corresponding to the first character of the word.
  • If it does, we move to the child node and repeat the process with the next character.
  • If we reach the end of the word, we check if the current node is a leaf node. If it is, the word exists in the trie.
  • If the word does not exist in the trie, we return false.
Solution
Python
Language
def search(self, word: str) -> bool:
node = self.root
for char in word:
if char not in node.children:
return False
node = node.children[char]
return node.is_end_of_word

Mark as read

Next: Two-Pointer Overview

Your account is free and you can post anonymously if you choose.

Unlock Premium Coding Content

Interactive algorithm visualizations
Guided Practice
Recent interview questions
Learn More
Reading Progress

On This Page

Explanation

Search

Starts With

Delete

Questions
Meta SWE Interview QuestionsAmazon SWE Interview QuestionsGoogle SWE Interview QuestionsOpenAI SWE Interview QuestionsEngineering Manager (EM) Interview Questions
Learn
Learn System DesignLearn DSALearn BehavioralLearn ML System DesignLearn Low Level DesignGuided Practice
Links
FAQPricingGift PremiumHello Interview Premium
Legal
Terms and ConditionsPrivacy PolicySecurity
Contact
About UsProduct Support

7511 Greenwood Ave North Unit #4238 Seattle WA 98103


© 2026 Optick Labs Inc. All rights reserved.

Login to track your progress