Bird
Raised Fist0
DSA Cprogramming~20 mins

Shortest Path in Unweighted Graph Using BFS in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Shortest Path BFS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of BFS shortest path distance array
What is the output distance array after running BFS from node 0 in this unweighted graph?
DSA C
int graph[5][5] = {
  {0, 1, 0, 0, 0},
  {1, 0, 1, 1, 0},
  {0, 1, 0, 0, 1},
  {0, 1, 0, 0, 1},
  {0, 0, 1, 1, 0}
};

int dist[5];
for (int i = 0; i < 5; i++) dist[i] = -1;
dist[0] = 0;

int queue[5], front = 0, rear = 0;
queue[rear++] = 0;

while (front < rear) {
  int u = queue[front++];
  for (int v = 0; v < 5; v++) {
    if (graph[u][v] == 1 && dist[v] == -1) {
      dist[v] = dist[u] + 1;
      queue[rear++] = v;
    }
  }
}

for (int i = 0; i < 5; i++) printf("%d ", dist[i]);
A[0, 1, 2, 1, 2]
B[0, 1, 1, 2, 2]
C[0, 1, 2, 2, 3]
D[0, 1, 2, 3, 4]
Attempts:
2 left
💡 Hint
Trace BFS layer by layer starting from node 0.
🧠 Conceptual
intermediate
1:30remaining
Why BFS finds shortest path in unweighted graphs?
Why does BFS guarantee the shortest path in an unweighted graph?
ABecause BFS uses a priority queue to select the next node.
BBecause BFS uses a stack to explore nodes deeply first.
CBecause BFS marks nodes visited only after exploring all neighbors.
DBecause BFS explores nodes in increasing order of their distance from the start node.
Attempts:
2 left
💡 Hint
Think about the order in which BFS visits nodes.
🔧 Debug
advanced
2:00remaining
Identify the bug in BFS shortest path code
What error will this BFS code cause when run on an unweighted graph?
DSA C
int dist[5];
for (int i = 0; i < 5; i++) dist[i] = 0;

int queue[5], front = 0, rear = 0;
queue[rear++] = 0;

dist[0] = 0;

while (front < rear) {
  int u = queue[front++];
  for (int v = 0; v < 5; v++) {
    if (graph[u][v] == 1 && dist[v] == 0) {
      dist[v] = dist[u] + 1;
      queue[rear++] = v;
    }
  }
}
AInfinite loop because nodes are revisited endlessly
BNo error, code runs correctly
CArray index out of bounds error
DCompilation error due to missing semicolon
Attempts:
2 left
💡 Hint
Check how visited nodes are tracked.
❓ Predict Output
advanced
2:30remaining
Output of BFS path reconstruction
What is the reconstructed shortest path from node 0 to node 4 after BFS?
DSA C
int parent[5];
for (int i = 0; i < 5; i++) parent[i] = -1;

int dist[5];
for (int i = 0; i < 5; i++) dist[i] = -1;
dist[0] = 0;

int queue[5], front = 0, rear = 0;
queue[rear++] = 0;

while (front < rear) {
  int u = queue[front++];
  for (int v = 0; v < 5; v++) {
    if (graph[u][v] == 1 && dist[v] == -1) {
      dist[v] = dist[u] + 1;
      parent[v] = u;
      queue[rear++] = v;
    }
  }
}

int path[5];
int count = 0;
for (int v = 4; v != -1; v = parent[v]) {
  path[count++] = v;
}

for (int i = count - 1; i >= 0; i--) printf("%d ", path[i]);
A0 2 4
B0 1 2 4
C0 1 3 4
D0 3 4
Attempts:
2 left
💡 Hint
Trace parent links from node 4 back to 0.
🧠 Conceptual
expert
1:30remaining
Time complexity of BFS shortest path in adjacency matrix
What is the time complexity of BFS to find shortest path in an unweighted graph represented by an adjacency matrix of size N x N?
AO(N^2) because BFS checks all possible edges in the matrix
BO(N) because BFS visits each node once
CO(E) where E is number of edges, because BFS visits edges only
DO(log N) because BFS uses a queue
Attempts:
2 left
💡 Hint
Consider how adjacency matrix stores edges and how BFS scans neighbors.

Practice

(1/5)
1. What does BFS (Breadth-First Search) help find in an unweighted graph?
easy
A. The total number of nodes in the graph
B. The shortest path from a start node to all other nodes
C. The longest path between two nodes
D. The heaviest edge in the graph

Solution

  1. Step 1: Understand BFS traversal

    BFS explores nodes level by level starting from the source node, visiting all neighbors before moving deeper.
  2. Step 2: BFS finds shortest paths in unweighted graphs

    Because BFS visits nodes in order of increasing distance, it finds the shortest path in terms of edges from the start node.
  3. Final Answer:

    The shortest path from a start node to all other nodes -> Option B
  4. Quick Check:

    BFS = shortest path in unweighted graph [OK]
Hint: BFS explores neighbors level-wise to find shortest paths [OK]
Common Mistakes:
  • Confusing BFS with DFS for shortest path
  • Thinking BFS finds longest path
  • Assuming BFS works only for weighted graphs
2. Which of the following is the correct way to initialize a queue for BFS in C?
easy
A. int queue[MAX]; int front = 0, rear = 0;
B. queue<int> q; // C++ syntax
C. Queue q = new Queue(); // Java syntax
D. var q = []; // JavaScript syntax

Solution

  1. Step 1: Identify C syntax for queue

    C does not have built-in queue; we use arrays with front and rear indices to simulate queue.
  2. Step 2: Check options for C correctness

    int queue[MAX]; int front = 0, rear = 0; uses array and indices, which is valid in C. Others use non-C syntax.
  3. Final Answer:

    int queue[MAX]; int front = 0, rear = 0; -> Option A
  4. Quick Check:

    C queue uses array + front/rear indices [OK]
Hint: Use array with front/rear for queue in C [OK]
Common Mistakes:
  • Using C++ or Java syntax in C code
  • Not initializing front and rear indices
  • Assuming queue is built-in in C
3. What is the output distance array after running BFS from node 0 on this graph?
Graph edges:
0 - 1
0 - 2
1 - 3
2 - 3
3 - 4
Assume nodes are 0 to 4 and distance to unreachable nodes is -1.
medium
A. [0, 1, 2, 3, 4]
B. [0, 1, 1, 1, 2]
C. [0, 1, 1, 2, 3]
D. [0, -1, -1, -1, -1]

Solution

  1. Step 1: Start BFS from node 0 and set distance[0] = 0

    Initialize distances: distance[0]=0, others = -1.
  2. Step 2: Visit neighbors level-wise

    Neighbors of 0: 1 and 2 get distance 1. Then neighbors of 1 and 2: node 3 gets distance 2. Finally, neighbor of 3: node 4 gets distance 3.
  3. Final Answer:

    [0, 1, 1, 2, 3] -> Option C
  4. Quick Check:

    Distances increase by 1 per BFS level [OK]
Hint: Distance increments by 1 each BFS level from start [OK]
Common Mistakes:
  • Assigning wrong distances to nodes
  • Confusing BFS with DFS order
  • Not initializing unreachable nodes to -1
4. Identify the bug in this BFS code snippet for shortest path:
void bfs(int start) {
  queue[rear++] = start;
  visited[start] = 1;
  distance[start] = 0;
  while(front < rear) {
    int u = queue[front++];
    for(int v : adj[u]) {
      if(!visited[v]) {
        visited[v] = 1;
        distance[v] = distance[u] + 1;
        queue[rear++] = v;
      }
    }
  }
}
medium
A. Not marking start node as visited
B. Not initializing distance array before BFS
C. Queue overflow not handled
D. Using 'int v : adj[u]' is invalid in C

Solution

  1. Step 1: Check syntax of for loop in C

    Range-based for loops like 'for(int v : adj[u])' are not valid in C; this is C++ syntax.
  2. Step 2: Identify correct iteration method in C

    In C, adjacency lists require explicit indexing or pointer iteration, not range-based loops.
  3. Final Answer:

    Using 'int v : adj[u]' is invalid in C -> Option D
  4. Quick Check:

    Range-based for loops = invalid in C [OK]
Hint: C uses indexed loops, not range-based for [OK]
Common Mistakes:
  • Using C++ syntax in C code
  • Ignoring queue overflow possibility
  • Forgetting to initialize arrays
5. Given an unweighted graph with 6 nodes (0 to 5) and edges:
0-1, 0-2, 1-3, 3-4, 4-5
Write the shortest path from node 0 to node 5 using BFS. What is the path?
hard
A. [0, 1, 3, 4, 5]
B. [0, 2, 3, 4, 5]
C. [0, 1, 2, 3, 4, 5]
D. [0, 5]

Solution

  1. Step 1: Run BFS from node 0 to find shortest distances

    Distances: 0(0), 1(1), 2(1), 3(2), 4(3), 5(4). BFS visits neighbors level-wise.
  2. Step 2: Trace shortest path from 5 back to 0

    From 5 to 4, 4 to 3, 3 to 1 (via 1-3 edge; no 2-3 edge), 1 to 0.
  3. Final Answer:

    [0, 1, 3, 4, 5] -> Option A
  4. Quick Check:

    Shortest path = BFS parent chain [OK]
Hint: Trace BFS parents backward for shortest path [OK]
Common Mistakes:
  • Choosing longer paths ignoring BFS order
  • Assuming direct edge from 0 to 5
  • Confusing BFS with DFS path
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard