Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a dictionary that maps integers to lists of integers for vertical traversal.
DSA C#
Dictionary<int, List<int>> verticalMap = new Dictionary<int, List<int>>(); // Add a new list for a vertical line if it does not exist if (!verticalMap.ContainsKey([1])) { verticalMap[[1]] = new List<int>(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using node value as key instead of horizontal distance.
Using level instead of horizontal distance.
✗ Incorrect
The variable 'hd' (horizontal distance) is used as the key for vertical lines in the dictionary.
2fill in blank
mediumComplete the code to enqueue the left child of the current node with updated horizontal distance.
DSA C#
if (currentNode.left != null) { queue.Enqueue((currentNode.left, [1])); }
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing horizontal distance for left child.
Using level instead of horizontal distance.
✗ Incorrect
The left child is one step to the left, so horizontal distance decreases by 1.
3fill in blank
hardFix the error in the code that adds the current node's value to the vertical map list.
DSA C#
verticalMap[hd].[1](currentNode.val);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Append' which is not a List method in C#.
Using 'Push' which is for stacks.
✗ Incorrect
In C#, List uses 'Add' to append an element to the list.
4fill in blank
hardFill both blanks to complete the loop that processes nodes in the queue for vertical traversal.
DSA C#
while (queue.[1] > 0) { var (currentNode, hd) = queue.[2](); // process currentNode }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Length' which is not a Queue property.
Using 'Pop' which is for stacks.
✗ Incorrect
'Count' gives the number of items in the queue, and 'Dequeue' removes and returns the front item.
5fill in blank
hardFill all three blanks to create a sorted dictionary from the vertical map keys and print the vertical order traversal.
DSA C#
foreach (var key in verticalMap.Keys.[1](k => k)) { verticalMap[key].[2](); Console.Write(string.Join(", ", verticalMap[key]) + [3]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Sort' on keys which is not available for Keys collection.
Using wrong separator instead of newline.
✗ Incorrect
Use 'OrderBy' to sort keys, 'Sort' to sort each list, and '\n' to print each vertical line on a new line.
