Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the queue for BFS traversal.
DSA Java
Queue<Integer> queue = new LinkedList<>();
queue.[1](start);Attempts:
3 left
💡 Hint
Common Mistakes
Using remove() instead of add() to insert elements.
Using poll() which removes elements instead of adding.
✗ Incorrect
We use queue.add(start) to add the starting node to the BFS queue.
2fill in blank
mediumComplete the code to mark the starting node as visited.
DSA Java
boolean[] visited = new boolean[n];
visited[[1]] = true;Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed index like 0 or 1 instead of the variable 'start'.
Using 'n' which is the size, not an index.
✗ Incorrect
We mark the starting node as visited by setting visited[start] = true.
3fill in blank
hardFix the error in the BFS loop condition to continue while the queue is not empty.
DSA Java
while (!queue.[1]()) { int current = queue.poll(); // process current }
Attempts:
3 left
💡 Hint
Common Mistakes
Using isFull() which does not exist for queues.
Using empty() which is not a Java Queue method.
✗ Incorrect
The method isEmpty() checks if the queue is empty. We continue while it is NOT empty.
4fill in blank
hardFill both blanks to check neighbors and update their distance if unvisited.
DSA Java
for (int neighbor : graph.get(current)) { if (!visited[neighbor]) { visited[neighbor] = [1]; distance[neighbor] = distance[current] [2] 1; queue.add(neighbor); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting visited to false instead of true.
Subtracting 1 from distance instead of adding.
✗ Incorrect
We mark neighbors as visited by setting visited[neighbor] = true and update distance by adding 1.
5fill in blank
hardFill all three blanks to return the shortest path distance or -1 if unreachable.
DSA Java
return distance[[1]] != [2] ? distance[[3]] : -1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' instead of 'end' for distance checks.
Comparing with 0 instead of Integer.MAX_VALUE.
✗ Incorrect
We check if distance[end] is not Integer.MAX_VALUE (meaning reachable), then return distance[end], else -1.
