Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a tree node with a value.
DSA C#
public class TreeNode { public int Value; public List<TreeNode> Children = new List<TreeNode>(); public TreeNode(int value) { Value = [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable like 'val' or 'node'.
Forgetting to assign the parameter to the field.
✗ Incorrect
The constructor parameter is named 'value', so we assign it to the Value field.
2fill in blank
mediumComplete the code to add a child node to the current tree node.
DSA C#
public void AddChild(TreeNode child) {
Children.[1](child);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'append' which is a Python method.
Using 'push' which is common in stacks or JavaScript arrays.
✗ Incorrect
In C#, List uses the Add method to add an item to the list.
3fill in blank
hardFix the error in the method that counts total nodes in the tree.
DSA C#
public int CountNodes() {
int count = 1;
foreach (var child in Children) {
count += child.[1]();
}
return count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase method names which cause errors.
Misspelling the method name.
✗ Incorrect
Method names in C# are case-sensitive and usually PascalCase, so 'CountNodes' is correct.
4fill in blank
hardFill both blanks to create a dictionary of node values and their counts for children with value > 5.
DSA C#
var result = new Dictionary<int, int>(); foreach (var child in Children) { if (child.Value [1] 5) { if (!result.ContainsKey(child.Value)) { result[child.Value] = [2]; } result[child.Value]++; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Initializing count to 1 which causes double counting.
✗ Incorrect
We check if child.Value is greater than 5 and initialize count to 0 before incrementing.
5fill in blank
hardFill all three blanks to create a dictionary mapping uppercase node values to their counts if count > 0.
DSA C#
var counts = new Dictionary<string, int>(); foreach (var node in nodes) { var key = node.Value.ToString().[1](); if (!counts.ContainsKey(key)) { counts[key] = [2]; } counts[key]++; } var filtered = counts.Where(kv => kv.Value [3] 0).ToDictionary(kv => kv.Key, kv => kv.Value);
Attempts:
3 left
💡 Hint
Common Mistakes
Using ToLower instead of ToUpper.
Initializing counts to 1 causing off-by-one errors.
Using '<' instead of '>' in the filter condition.
✗ Incorrect
We convert values to uppercase, initialize counts to 0, and filter counts greater than 0.
