Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a word to the Trie.
DSA C#
public void Insert(string word) {
TrieNode node = root;
foreach (char c in word) {
if (!node.Children.ContainsKey(c)) {
node.Children.Add(c, new TrieNode());
}
node = node.Children[1];
}
node.IsWord = true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets to access dictionary elements.
Trying to access a property or method that does not exist on the dictionary.
✗ Incorrect
In C#, to access a value in a dictionary by key, use square brackets [key]. Here, node.Children is a dictionary, so node.Children[c] accesses the child node for character c.
2fill in blank
mediumComplete the code to check if a prefix exists in the Trie.
DSA C#
public bool StartsWith(string prefix) {
TrieNode node = root;
foreach (char c in prefix) {
if (!node.Children.ContainsKey(c)) {
return false;
}
node = node.Children[1];
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or braces instead of square brackets.
Trying to access a property directly on the dictionary.
✗ Incorrect
To get the child node for character c from the dictionary, use square brackets [c]. This lets us move down the Trie nodes following the prefix characters.
3fill in blank
hardFix the error in the HashMap prefix check method.
DSA C#
public bool HasPrefix(string prefix) {
foreach (string word in words) {
if (word.[1]) {
return true;
}
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Contains which matches anywhere in the word, not just prefix.
Using Equals which requires exact match.
Using EndsWith which checks suffix, not prefix.
✗ Incorrect
To check if a word starts with a prefix, use the StartsWith method. Contains checks for substring anywhere, which is not the same as prefix matching.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps words to their lengths for words starting with 'a'.
DSA C#
var result = words.Where(word => word.[1]("a")).ToDictionary(word => word, word => word.[2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Contains instead of StartsWith for filtering.
Using Count which is a method for collections, not strings.
Using wrong property for length.
✗ Incorrect
We filter words starting with 'a' using StartsWith, then map each word to its Length property in the dictionary.
5fill in blank
hardFill all three blanks to build a dictionary of uppercase words with their lengths for words containing 'e'.
DSA C#
var dict = words.Where(word => word.[1]("e")).ToDictionary(word => word.[2], word => word.[3]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using StartsWith instead of Contains for filtering.
Not converting the word to uppercase for the key.
Using wrong property for length.
✗ Incorrect
We filter words containing 'e' using Contains, then use ToUpper() for keys and Length for values in the dictionary.
