Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the Node class constructor.
DSA C#
public class Node { public int data; public Node left, right; public Node(int [1]) { data = [1]; left = right = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' as parameter name causes confusion with the field.
Using 'node' or 'value' which are not the parameter name here.
✗ Incorrect
The constructor parameter name is 'val' and it is assigned to the data field.
2fill in blank
mediumComplete the code to add a node to the queue in the top view traversal.
DSA C#
Queue<Node> queue = new Queue<Node>();
queue.[1](root);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'enqueue' (lowercase) causes error.
Using 'push' which is for stacks, not queues.
✗ Incorrect
In C#, Queue uses 'Enqueue' method to add elements.
3fill in blank
hardFix the error in the dictionary check for horizontal distance key.
DSA C#
if (!map.[1](hd)) { map[hd] = node.data; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'HasKey' which is not a C# Dictionary method.
Using 'Contains' which checks values, not keys.
✗ Incorrect
In C#, Dictionary uses ContainsKey to check if a key exists.
4fill in blank
hardFill both blanks to correctly dequeue and get the horizontal distance from the queue.
DSA C#
Node node = queue.[1](); int hd = hdQueue.[2]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop which is for stacks, not queues.
Using Peek which only returns without removing.
✗ Incorrect
Use Dequeue() to remove and return the front element from both queues.
5fill in blank
hardFill all three blanks to add left and right children to the queue and their horizontal distances.
DSA C#
if (node.left != null) { queue.[1](node.left); hdQueue.[2](hd [3] 1); } if (node.right != null) { queue.Enqueue(node.right); hdQueue.Enqueue(hd - 1); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' for left child hd.
Using Add which is not a Queue method.
✗ Incorrect
Use Enqueue to add nodes and hd + 1 for left child horizontal distance.
