0
0
DSA Javascriptprogramming~10 mins

Longest Word in Dictionary Using Trie in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Longest Word in Dictionary Using Trie
Start with empty Trie
Insert each word letter by letter
Mark end of word nodes
Traverse Trie from root
At each node, check if it ends a word
Build candidate words by adding letters
Keep track of longest valid word
Return longest word found
Build a Trie from all words, then traverse it to find the longest word where every prefix is also a word.
Execution Sample
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isWord = false;
  }
}
// Insert words and find longest word
This code builds a Trie node class and inserts words to find the longest valid word.
Execution Table
StepOperationNodes in TriePointer ChangesVisual State
1Insert 'a'root -> a(isWord: true)root.children['a'] createdroot └─ a*
2Insert 'app'root -> a(isWord: true) -> p -> p(isWord: true)a.children['p'] created, p.children['p'] created, mark last p isWord trueroot └─ a* └─ p └─ p*
3Insert 'ap'root -> a(isWord: true) -> p(isWord: true) -> p(isWord: true)a.children['p'] isWord set trueroot └─ a* └─ p* └─ p*
4Insert 'apple'root -> a* -> p* -> p* -> l -> e(isWord: true)p.children['l'] created, l.children['e'] created, mark e isWord trueroot └─ a* └─ p* └─ p* └─ l └─ e*
5Insert 'apply'root -> a* -> p* -> p* -> l -> e* and y(isWord: true)l.children['y'] created, mark y isWord trueroot └─ a* └─ p* └─ p* └─ l ├─ e* └─ y*
6Traverse Trie to find longest wordStarting at rootTraverse nodes where isWord trueCurrent longest: ''
7Visit 'a' node (isWord: true)At node 'a'Add 'a' to current wordCurrent longest: 'a'
8Visit 'p' node (isWord: true)At node 'p' after 'a'Add 'p' to current wordCurrent longest: 'ap'
9Visit next 'p' node (isWord: true)At node 'p' after 'ap'Add 'p' to current wordCurrent longest: 'app'
10Visit 'l' node (isWord: false)At node 'l' after 'app'Cannot continue, 'l' is not end of wordCurrent longest: 'app'
11Visit 'e' node (isWord: true) under 'l'At node 'e' after 'appl'Add 'e' to current wordCurrent longest: 'apple'
12Visit 'y' node (isWord: true) under 'l'At node 'y' after 'appl'Add 'y' to current wordCurrent longest: 'apple' (tie with 'apply', apple chosen lex order)
13Traversal completeAll nodes visitedLongest word found: 'apple'Final longest word: 'apple'
💡 Traversal ends after visiting all nodes with isWord true; longest word is 'apple'
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10After Step 11After Step 12Final
Trie Size01346777777777
Current Word'''''''''''''''a''ap''app''app''apple''apply''apple'
Longest Word'''a''ap''app''app''app''app''a''ap''app''app''apple''apple''apple'
Key Moments - 3 Insights
Why do we only continue traversal at nodes where isWord is true?
Because the problem requires every prefix of the word to be a valid word. If a node is not marked isWord, it means the prefix up to that node is not a word, so we stop. See execution_table rows 10 and 11.
Why do we mark nodes as isWord true during insertion?
Marking nodes as isWord true shows that the path from root to that node forms a valid word. This helps during traversal to check if prefixes are valid. See execution_table rows 1 to 5.
How do we decide between words of the same length?
We choose the lexicographically smallest word when lengths tie. For example, between 'apple' and 'apply', 'apple' is chosen. See execution_table row 12.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 9, what is the current longest word?
A'apple'
B'ap'
C'app'
D'a'
💡 Hint
Check the 'Current longest' column at step 9 in execution_table.
At which step does the traversal stop continuing down a path because the node is not a word end?
AStep 10
BStep 7
CStep 12
DStep 5
💡 Hint
Look for the step where 'Cannot continue' is noted in execution_table.
If the word 'app' was not marked as isWord true during insertion, how would the longest word change?
A'apple' would still be found
B'apple' would not be found
CNo longest word would be found
D'app' would be the longest word
💡 Hint
Refer to key_moments about why traversal only continues at nodes marked isWord true.
Concept Snapshot
Longest Word in Dictionary Using Trie:
- Build Trie by inserting words letter by letter
- Mark nodes where words end (isWord = true)
- Traverse Trie only through nodes marked isWord
- Track longest word where all prefixes are valid words
- If tie, choose lexicographically smallest
- Return longest valid word found
Full Transcript
We build a Trie by inserting each word letter by letter, marking nodes where words end. Then, we traverse the Trie from the root, only moving through nodes that mark the end of a word. This ensures every prefix of the word is valid. We keep track of the longest such word found during traversal. If multiple words tie in length, we pick the lexicographically smallest. The traversal stops when no further nodes are valid word ends. Finally, we return the longest word found.