Challenge - 5 Problems
Dijkstra Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Dijkstra's Algorithm on a Simple Graph
What is the output distances array after running Dijkstra's algorithm from source node 0 on the following graph?
Graph edges:
0 -> 1 (weight 4), 0 -> 2 (weight 1), 2 -> 1 (weight 2), 1 -> 3 (weight 1), 2 -> 3 (weight 5)
Assume nodes are 0-indexed and graph has 4 nodes.
Graph edges:
0 -> 1 (weight 4), 0 -> 2 (weight 1), 2 -> 1 (weight 2), 1 -> 3 (weight 1), 2 -> 3 (weight 5)
Assume nodes are 0-indexed and graph has 4 nodes.
DSA C++
int V = 4; vector<pair<int,int>> adj[V]; adj[0].push_back({1,4}); adj[0].push_back({2,1}); adj[2].push_back({1,2}); adj[1].push_back({3,1}); adj[2].push_back({3,5}); vector<int> dist(V, INT_MAX); dist[0] = 0; priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; pq.push({0,0}); while(!pq.empty()) { int u = pq.top().second; int d = pq.top().first; pq.pop(); if(d > dist[u]) continue; for(auto &edge : adj[u]) { int v = edge.first; int w = edge.second; if(dist[u] + w < dist[v]) { dist[v] = dist[u] + w; pq.push({dist[v], v}); } } } for(int i=0; i<V; i++) { cout << dist[i] << " "; }
Attempts:
2 left
💡 Hint
Trace the shortest path updates from source 0 step-by-step.
✗ Incorrect
Starting from node 0, the shortest distances are updated as follows: dist[0]=0, dist[2]=1 (via 0->2), dist[1]=3 (via 0->2->1), dist[3]=4 (via 0->2->1->3).
🧠 Conceptual
intermediateUnderstanding Dijkstra's Algorithm Behavior
Which of the following statements about Dijkstra's algorithm is always true when used on a graph with non-negative edge weights?
Attempts:
2 left
💡 Hint
Recall the conditions for Dijkstra's algorithm correctness.
✗ Incorrect
Dijkstra's algorithm finds the shortest path from the source to all nodes if all edge weights are non-negative. It does not work correctly with negative weights. It uses a priority queue, not a stack.
🔧 Debug
advancedIdentify the Bug in Dijkstra's Algorithm Implementation
Consider this snippet from a Dijkstra's algorithm implementation. What is the bug causing incorrect shortest path results?
Code snippet:
for (auto &edge : adj[u]) { int v = edge.first; int w = edge.second; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; pq.push({dist[u], v}); } }
Code snippet:
for (auto &edge : adj[u]) { int v = edge.first; int w = edge.second; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; pq.push({dist[u], v}); } }
Attempts:
2 left
💡 Hint
Check what value is pushed into the priority queue after updating dist[v].
✗ Incorrect
After updating dist[v], the new distance dist[v] must be pushed into the priority queue, not dist[u]. Using dist[u] causes wrong priority and incorrect results.
❓ Predict Output
advancedOutput Distances with a Larger Graph
What is the output distances array after running Dijkstra's algorithm from source node 0 on this graph with 5 nodes?
Edges:
0->1 (10), 0->4 (5), 1->2 (1), 1->4 (2), 2->3 (4), 3->2 (6), 4->1 (3), 4->2 (9), 4->3 (2)
Print distances array after algorithm completes.
Edges:
0->1 (10), 0->4 (5), 1->2 (1), 1->4 (2), 2->3 (4), 3->2 (6), 4->1 (3), 4->2 (9), 4->3 (2)
Print distances array after algorithm completes.
DSA C++
int V = 5; vector<pair<int,int>> adj[V]; adj[0].push_back({1,10}); adj[0].push_back({4,5}); adj[1].push_back({2,1}); adj[1].push_back({4,2}); adj[2].push_back({3,4}); adj[3].push_back({2,6}); adj[4].push_back({1,3}); adj[4].push_back({2,9}); adj[4].push_back({3,2}); vector<int> dist(V, INT_MAX); dist[0] = 0; priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; pq.push({0,0}); while(!pq.empty()) { int u = pq.top().second; int d = pq.top().first; pq.pop(); if(d > dist[u]) continue; for(auto &edge : adj[u]) { int v = edge.first; int w = edge.second; if(dist[u] + w < dist[v]) { dist[v] = dist[u] + w; pq.push({dist[v], v}); } } } for(int i=0; i<V; i++) { cout << dist[i] << " "; }
Attempts:
2 left
💡 Hint
Calculate shortest paths stepwise from node 0.
✗ Incorrect
The shortest distances are: 0->0=0, 0->4=5, 0->1=8 (via 4), 0->3=7 (via 4), 0->2=9 (via 1).
🧠 Conceptual
expertDijkstra's Algorithm Limitations and Alternatives
Which of the following statements correctly describes a limitation of Dijkstra's algorithm and a suitable alternative?
Attempts:
2 left
💡 Hint
Think about edge weights and algorithm applicability.
✗ Incorrect
Dijkstra's algorithm fails with negative edge weights because it assumes once a node is visited with shortest distance, it won't be updated. Bellman-Ford handles negative weights correctly.
