Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Word Search in Trie in DSA Javascript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Word Search in Trie
Start at root node
↓
For each letter in word
↓
Check if letter child exists?
No→Word NOT found
Yes↓
Move to child node
↓
Repeat for next letter
↓
After last letter, check isEnd?
No→Word NOT found
Yes↓
Word FOUND in Trie
Start from root, check each letter's child node exists, move down nodes, finally check if last node marks word end.
Execution Sample
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEnd = false;
  }
}

function searchWord(root, word) {
  let node = root;
  for (const char of word) {
    if (!node.children[char]) return false;
    node = node.children[char];
  }
  return node.isEnd;
}
Searches a word in a Trie by traversing nodes for each letter and checking end marker.
Execution Table
StepOperationCurrent Node LetterChild Exists?Move To NodeVisual State
1Start at rootrootN/Arootroot (children: a, b)
2Check letter 'b'rootYesnode for 'b'root -> b
3Check letter 'a'bYesnode for 'a'root -> b -> a
4Check letter 'd'aYesnode for 'd'root -> b -> a -> d
5End of word reacheddN/AN/Anode 'd' isEnd = true
6Return resultdN/AN/AWord FOUND
7Start at rootrootN/Arootroot (children: a, b)
8Check letter 'b'rootYesnode for 'b'root -> b
9Check letter 'a'bYesnode for 'a'root -> b -> a
10Check letter 't'aNoN/ANo child 't' under 'a'
11Return resultaN/AN/AWord NOT found
💡 Search ends when all letters checked and isEnd true (word found) or missing child (word not found)
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
noderootnode for 'b'node for 'a'node for 'd'node for 'd'node for 'd'
letterN/A'b''a''d'N/AN/A
isEndN/AN/AN/Atruetruetrue
Key Moments - 3 Insights
Why do we check if the child node exists before moving?
Because if the child node for a letter does not exist (see Step 10), the word cannot be in the Trie, so we stop searching.
Why do we check isEnd only after the last letter?
Because isEnd marks if a complete word ends at that node. Without it, the prefix might exist but not the full word (see Step 5).
What happens if the word is empty?
The search starts and ends at root. If root.isEnd is true, empty word is found; else not. This is a special case not shown here.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the current node letter at Step 4?
A'b'
B'a'
C'd'
D'root'
💡 Hint
Check the 'Current Node Letter' column at Step 4 in the execution_table.
At which step does the search find that a child node does NOT exist?
AStep 9
BStep 11
CStep 10
DStep 8
💡 Hint
Look for 'Child Exists?' column with 'No' in the execution_table.
If the word searched was 'bad', what would be the value of isEnd at the final node?
Atrue
Bfalse
Cundefined
Dnull
💡 Hint
Refer to variable_tracker row for 'isEnd' after Step 5.
Concept Snapshot
Word Search in Trie:
- Start at root node
- For each letter, check if child node exists
- Move to child node if exists
- After last letter, check isEnd flag
- Return true if isEnd true, else false
Full Transcript
This visualization shows how to search a word in a Trie data structure. We start at the root node and check each letter of the word one by one. For each letter, we look if the current node has a child node matching that letter. If yes, we move to that child node and continue. If at any point the child node does not exist, the word is not in the Trie. After checking all letters, we verify if the last node marks the end of a word using the isEnd flag. If it does, the word is found; otherwise, it is not. The execution table traces these steps with the current node, child existence, and visual state of the Trie. The variable tracker shows how the node pointer and isEnd flag change during the search. Key moments clarify why we check child nodes and the isEnd flag. The quiz tests understanding of node letters, missing children, and end flags.

Practice

(1/5)
1. What is the main purpose of using a Trie data structure in word search?
easy
A. To efficiently store and search words by their prefixes
B. To sort words alphabetically
C. To store words in a hash table
D. To perform arithmetic operations on words

Solution

  1. Step 1: Understand Trie structure

    A Trie stores characters of words in a tree where each node represents a character.
  2. Step 2: Identify Trie usage

    It allows quick prefix-based search by traversing nodes matching characters.
  3. Final Answer:

    To efficiently store and search words by their prefixes -> Option A
  4. Quick Check:

    Trie = prefix search [OK]
Hint: Trie is all about prefix-based fast word search [OK]
Common Mistakes:
  • Confusing Trie with hash tables
  • Thinking Trie sorts words
  • Assuming Trie does arithmetic
2. Which of the following is the correct way to check if a word exists in a Trie in JavaScript?
easy
A. Use a loop to traverse nodes matching each character and check end flag
B. Use Array.includes(word) on the Trie object
C. Use JSON.stringify on Trie and search substring
D. Use Math.max on character codes of the word

Solution

  1. Step 1: Understand Trie search method

    Search involves traversing nodes for each character and checking if the word ends at last node.
  2. Step 2: Eliminate incorrect options

    Array.includes and JSON.stringify are not suitable for Trie structure. Math.max is unrelated.
  3. Final Answer:

    Use a loop to traverse nodes matching each character and check end flag -> Option A
  4. Quick Check:

    Traverse nodes + check end = correct search [OK]
Hint: Traverse each character node and check end flag [OK]
Common Mistakes:
  • Using array methods on Trie
  • Trying to convert Trie to string
  • Applying math functions on words
3. What will be the output of the following code snippet?
class TrieNode {
  constructor() {
    this.children = {};
    this.isEnd = false;
  }
}

class Trie {
  constructor() {
    this.root = new TrieNode();
  }
  insert(word) {
    let node = this.root;
    for (const char of word) {
      if (!node.children[char]) node.children[char] = new TrieNode();
      node = node.children[char];
    }
    node.isEnd = true;
  }
  search(word) {
    let node = this.root;
    for (const char of word) {
      if (!node.children[char]) return false;
      node = node.children[char];
    }
    return node.isEnd;
  }
}

const trie = new Trie();
trie.insert('cat');
trie.insert('car');
console.log(trie.search('cat'));
console.log(trie.search('cap'));
medium
A. false false
B. false true
C. true false
D. true true

Solution

  1. Step 1: Insert words 'cat' and 'car'

    Both words are added character by character, marking end nodes for 't' and 'r'.
  2. Step 2: Search 'cat' and 'cap'

    'cat' exists with isEnd true, 'cap' path breaks at 'p' missing node, returns false.
  3. Final Answer:

    true false -> Option C
  4. Quick Check:

    Search 'cat' = true, 'cap' = false [OK]
Hint: Check inserted words and search path carefully [OK]
Common Mistakes:
  • Assuming 'cap' exists after inserting 'car'
  • Confusing isEnd flag
  • Ignoring missing child nodes
4. Identify the bug in the following Trie search method and select the fix:
search(word) {
  let node = this.root;
  for (const char of word) {
    if (!node.children[char]) return false;
    node = node.children[char];
  }
  return true;
}
medium
A. Return false at the end instead of true
B. Add a check for empty word before loop
C. Change for loop to while loop
D. Return node.isEnd instead of true to confirm full word match

Solution

  1. Step 1: Analyze current search return

    Returns true if path exists but does not check if word ends here.
  2. Step 2: Correct return value

    Must return node.isEnd to confirm the word is complete, not just prefix.
  3. Final Answer:

    Return node.isEnd instead of true to confirm full word match -> Option D
  4. Quick Check:

    Check isEnd flag to confirm word end [OK]
Hint: Always check isEnd flag to confirm full word [OK]
Common Mistakes:
  • Returning true without checking isEnd
  • Changing loop type unnecessarily
  • Ignoring empty word cases
5. Given a list of words, how can you use a Trie to find all words that start with a given prefix efficiently? Choose the best approach.
hard
A. Sort words and use binary search on prefix
B. Insert all words into Trie, traverse prefix nodes, then collect all descendant words
C. Use a hash map with prefix keys for direct lookup
D. Check each word in list if it starts with prefix using string methods

Solution

  1. Step 1: Insert all words into Trie

    Build Trie so each word is represented by a path of nodes.
  2. Step 2: Traverse prefix nodes and collect words

    Find node matching prefix end, then recursively collect all words below it.
  3. Step 3: Compare other options

    Sorting and binary search is possible but less efficient for many queries. Hash maps need many keys. Checking each word is slow.
  4. Final Answer:

    Insert all words into Trie, traverse prefix nodes, then collect all descendant words -> Option B
  5. Quick Check:

    Trie + prefix traversal = efficient prefix search [OK]
Hint: Use Trie prefix traversal then gather descendant words [OK]
Common Mistakes:
  • Ignoring Trie traversal for prefix
  • Using slow linear search for prefix
  • Confusing hash map with Trie
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard