Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a value into a Binary Search Tree (BST).
DSA C#
public class Node { public int Value; public Node Left, Right; public Node(int value) { Value = value; Left = Right = null; } } public Node Insert(Node root, int value) { if (root == null) { return new Node(value); } if (value [1] root.Value) { root.Left = Insert(root.Left, value); } else { root.Right = Insert(root.Right, value); } return root; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes wrong placement of nodes.
Using '==' in comparison leads to incorrect logic.
✗ Incorrect
In a BST, values less than the current node go to the left subtree, so we use '<' to decide where to insert.
2fill in blank
mediumComplete the code to search for a value in a BST.
DSA C#
public bool Search(Node root, int value) {
if (root == null) return false;
if (root.Value == value) return true;
if (value [1] root.Value) {
return Search(root.Left, value);
} else {
return Search(root.Right, value);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Searching right subtree when value is smaller.
Using equality operator in wrong place.
✗ Incorrect
To search in BST, if the value is less than current node, search left subtree.
3fill in blank
hardFix the error in the code that finds the minimum value node in a BST.
DSA C#
public Node FindMin(Node root) {
while (root.[1] != null) {
root = root.Left;
}
return root;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Right' instead of 'Left' causes wrong minimum.
Using incorrect property names like 'LeftChild'.
✗ Incorrect
The minimum value in BST is found by going left until no more left child exists.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps node values to their depths, but only for nodes with values {{BLANK_1}} 10 and depths {{BLANK_2}} 2.
DSA C#
var depthMap = new Dictionary<int, int>(); foreach (var node in nodes) { if (node.Value [1] 10 && node.Depth [2] 2) { depthMap[node.Value] = node.Depth; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for values.
Using '>' instead of '<=' for depths.
✗ Incorrect
We want values greater than 10 and depths less than or equal to 2.
5fill in blank
hardFill all three blanks to create a dictionary that stores uppercase node names as keys, their values as values, but only if value {{BLANK_1}} 50, name {{BLANK_2}} 'A', and depth {{BLANK_3}} 3.
DSA C#
var filteredNodes = new Dictionary<string, int>(); foreach (var node in nodes) { if (node.Value [1] 50 && node.Name [2] "A" && node.Depth [3] 3) { filteredNodes[node.Name.ToUpper()] = node.Value; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' for value.
Using '!=' instead of '==' for name.
Using '>=' instead of '<' for depth.
✗ Incorrect
We want values greater than 50, names equal to 'A', and depths less than 3.
