nail your next job interview trie

Summary

  • implement trie (prefix tree)

Implmentaton of trie

Array of TrieNode (lower case letter):

1
2
3
4
5
6
7
8
9
private class  { 
private boolean isWord;
private TrieNode[] children;

public () {
this.isWord = false;
this.children = new TrieNode[26];
}
}

HashMap w/ boolean

1
2
3
4
5
6
7
8
9
private class  { 
private boolean isWord;
private Map<Character, TrieNode> children;

public () {
isWord = false;
children = new HashMap<>();
}
}

HashMap w/ String

1
2
3
4
5
6
7
8
9
private class  { 
private String word;
private Map<Character, TrieNode> children;

public TrieNode() {
word = null;
children = new HashMap<>();
}
}

HashMap w/ List of Strings

  • TODO:
1
2
3
4
5
6
7
8
9
private class TrieNode { 
List<String> words;
private Map<Character, TrieNode> children;

public TrieNode() {
word = null;
children = new HashMap<>();
}
}

TD: how to represent trie in interview