Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the queue for BFS.
DSA C
int queue[MAX]; int front = -1, rear = -1; // Enqueue function void enqueue(int x) { rear = rear [1] 1; queue[rear] = x; if (front == -1) front = 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes rear to move backward.
Using '*' or '/' is incorrect for pointer movement.
✗ Incorrect
We increase rear by 1 to add a new element at the end of the queue.
2fill in blank
mediumComplete the code to mark a node as visited in BFS.
DSA C
int visited[MAX] = {0};
void bfs(int start) {
visited[start] = [1];
enqueue(start);
// rest of BFS
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited as 0 means unvisited.
Using node value instead of 1 is incorrect.
✗ Incorrect
Marking visited[start] as 1 means the node is visited.
3fill in blank
hardFix the error in the BFS loop condition to process all nodes in the queue.
DSA C
while (front [1] -1 && front <= rear) { int current = queue[front]; front = front + 1; // process current }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' causes loop to run only when front is -1.
Using '<' or '>' is incorrect for this condition.
✗ Incorrect
The loop should run while front is not -1 and front <= rear.
4fill in blank
hardFill both blanks to update distance and enqueue neighbors in BFS.
DSA C
if (!visited[neighbor]) { visited[neighbor] = 1; distance[neighbor] = distance[current] [1] 1; enqueue([2]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting 1 from distance is wrong.
Enqueueing current instead of neighbor causes infinite loop.
✗ Incorrect
Distance to neighbor is current distance plus 1; enqueue the neighbor node.
5fill in blank
hardFill all three blanks to print the shortest path distance after BFS.
DSA C
int shortest_path(int start, int end) {
bfs(start);
if (distance[end] == [1]) {
return -1; // no path
} else {
return distance[[2]];
}
}
// Usage
int dist = shortest_path(0, [3]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking distance == 0 instead of -1.
Returning distance of start node instead of end.
Using wrong end node in function call.
✗ Incorrect
If distance[end] is -1, no path exists; return distance[end]; example end node is 5.