Trie Implementation and Operations

Tries Theory Hard
  • Fun fact: The Trie data structure is a popular data structure used in many applications in the software industry, including autocomplete features on search engines and text editors
  • Whenever you type in a search query or a piece of code, the system makes use of Tries to suggest potential completions based on the prefix of your input
  • This ability to make fast prefix queries makes Tries highly efficient for these kinds of functions

Implement the Trie class:

  • Trie(): Initializes the trie object.
  • void insert (String word): Inserts the string word into the trie.
  • boolean search (String word): Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
  • boolean startsWith (String prefix): Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.

Examples:

Input : ["Trie", "insert", "search", "search", "startsWith", "insert", "search"]

[ [] , ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"] ]


Output : [null, null, true, false, true, null, true]


Explanation :

Trie trie = new Trie()

trie.insert("apple")

trie.search("apple")  // return True

trie.search("app")   // return False

trie.startsWith("app") // return True

trie.insert("app")

trie.search("app")   // return True

Input : ["Trie" , "insert" , "insert" , "starsWith" , "search" ]

[ [] , "takeu" , "banana" , "bana" , "takeu" ]


Output : [null, null, null, true, true]


Explanation :

Trie trie = new Trie()

trie.insert("takeu")

trie.insert("banana")

trie.startsWith("bana") // return True

trie.search("takeu")   // return True

Input : ["Trie" , "insert" , "insert" , "starsWith" , "search" ]

[ [] , "caterpiller" , "cat" , "cat" , "cat" ]

Constraints

  • 1 <= word.length , prefix.length <= 2000
  • word and prefix consist only of lowercase English letters.
  • At most 3*104 calls in total will be made to insert, search and startsWith.

Hints

  • Insertion is performed character by character, ensuring that each character has an associated node. If a character does not exist in the current node’s children, a new node is created. Traversal continues until the entire word is inserted. Searching follows a similar traversal approach, checking if each character exists along the path and verifying if the terminal node is reached.
  • The startsWith function is slightly different, as it only checks if a given prefix exists without requiring it to be a full word. This is useful for autocomplete and dictionary applications. A well-optimized Trie provides fast lookup and insertion, outperforming hash-based methods when handling large dictionaries with shared prefixes.

Company Tags

Databricks Dropbox Uber Epic Games Zynga Goldman Sachs Seagate Technology Medtronic Chewy Broadcom Rakuten Snowflake Robinhood McKinsey & Company American Express MongoDB Square Wayfair Electronic Arts Western Digital Cloudflare Unity Technologies Alibaba ARM Bloomberg Google Microsoft Amazon Meta Apple Netflix Adobe