0
0
DSA Cprogramming~10 mins

Shortest Path in Unweighted Graph Using BFS in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A/
B-
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes rear to move backward.
Using '*' or '/' is incorrect for pointer movement.
2fill in blank
medium

Complete 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'
A1
B0
C-1
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited as 0 means unvisited.
Using node value instead of 1 is incorrect.
3fill in blank
hard

Fix 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'
A!=
B==
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' causes loop to run only when front is -1.
Using '<' or '>' is incorrect for this condition.
4fill in blank
hard

Fill 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'
A+
B-
Cneighbor
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting 1 from distance is wrong.
Enqueueing current instead of neighbor causes infinite loop.
5fill in blank
hard

Fill 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'
A-1
Bend
C5
D0
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.