Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a class for a binary tree node with left and right children.
DSA Typescript
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = [1]; this.right = null; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined instead of null.
Assigning 0 or this which are incorrect types.
✗ Incorrect
The left child should be initialized to null to indicate no child initially.
2fill in blank
mediumComplete the code to add a node to the queue for level order traversal.
DSA Typescript
queue.push({ node: currentNode, hd: [1] }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting 1 instead of adding for right child.
Using node value instead of distance.
✗ Incorrect
For the right child, horizontal distance increases by 1.
3fill in blank
hardFix the error in the code that updates the map with the bottom view node value.
DSA Typescript
map.set(hd, [1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.left or hd as value instead of node.value.
Using queue which is unrelated here.
✗ Incorrect
We store the node's value for the horizontal distance in the map.
4fill in blank
hardFill both blanks to complete the loop that processes nodes in the queue.
DSA Typescript
while (queue.length > 0) { const { node, hd } = queue.[1](); map.set(hd, node.value); if (node.left) queue.push({ node: node.left, hd: hd [2] 1 }); if (node.right) queue.push({ node: node.right, hd: hd + 1 }); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes from end, breaking BFS order.
Adding 1 for left child hd instead of subtracting.
✗ Incorrect
Use shift() to dequeue from front; left child hd is hd - 1.
5fill in blank
hardFill all three blanks to create the bottom view array from the map.
DSA Typescript
const sortedKeys = Array.from(map.keys()).[1]((a, b) => a [2] b); const bottomView = sortedKeys.[3](key => map.get(key));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - in sort comparator causing wrong order.
Using forEach instead of map for transformation.
✗ Incorrect
Sort keys ascending with sort and subtract; map keys to values with map().