Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the queue with the root node.
DSA Go
queue := []*TreeNode[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing queue as empty instead of with root node
Using nil instead of root
Using an empty struct instead of the root pointer
✗ Incorrect
We start the BFS by adding the root node to the queue.
2fill in blank
mediumComplete the code to dequeue the first node from the queue.
DSA Go
node := queue[[1]] queue = queue[1:]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 which skips the first element
Using negative index which is invalid
Using length of queue which is out of range
✗ Incorrect
In Go slices, the first element is at index 0, so we dequeue from index 0.
3fill in blank
hardFix the error in the code to add the left child to the queue only if it exists.
DSA Go
if node.Left [1] nil { queue = append(queue, node.Left) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if left child is equal to nil instead of not equal
Using comparison operators like < or > which are invalid for pointers
✗ Incorrect
We add the left child only if it is not nil (exists).
4fill in blank
hardFill both blanks to add the right child to the queue only if it exists and continue the loop.
DSA Go
if node.Right [1] nil { queue = append(queue, node.Right) } [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' to check for nil
Using 'break' instead of 'continue' which stops the loop
✗ Incorrect
We add the right child if it is not nil, then continue to the next iteration.
5fill in blank
hardFill all three blanks to complete the BFS loop that processes nodes until the queue is empty.
DSA Go
for len(queue) [1] 0 { node := queue[[2]] queue = queue[[3]:] // process node }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in loop condition
Using wrong indices for dequeue and slicing
✗ Incorrect
The loop runs while queue length is greater than 0, dequeues from index 0, and slices from index 1 onward.