Find the cheapest cost from a to c.
Acceptance criteria
- 1.#out should show cost 3.
DSA Tutorial · Graphs
These 10 tasks go with the Dijkstra's Algorithm chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “BFS finds the fewest hops. Dijkstra finds the lowest cost. The moment your edges carry a weight — distance, price, latency — BFS starts giving confidently wrong answers.” .
1.Shortest weighted pathexercise · easy · 10 min
Find the cheapest cost from a to c.
Passes when: #out should show cost 3.
2.Relax an edgeexercise · easy · 10 min · practice plan
Improve a tentative distance and show the new value.
Passes when: #out should show improved to 3.
3.Unweighted uses BFSexercise · easy · 10 min · practice plan
Report which algorithm suits edges that all weigh one.
Passes when: #out should show use bfs.
4.Negative weightsexercise · easy · 10 min · practice plan
Report which algorithm handles negative edge weights.
Passes when: #out should show bellman ford.
5.Unreachable stays infiniteexercise · medium · 10 min · practice plan
Report the distance to a node with no route in.
Passes when: #out should show unreachable.
6.Fix the greedy pick by insertion orderbug fix · medium · 8 min
Taking the first entry instead of the cheapest breaks the guarantee.
Passes when: #out should show cost 3.
7.Fix the relaxation without comparingbug fix · easy · 8 min · practice plan
Overwriting unconditionally can replace a shorter route with a longer one.
Passes when: #out should show improved to 3.
8.Fix Dijkstra used with negative weightsbug fix · easy · 8 min · practice plan
Negative edges break the assumption that finalised nodes stay finalised.
Passes when: #out should show bellman ford.
9.Fix the missing distance treated as zerobug fix · easy · 8 min · practice plan
An absent entry means unreachable, not a cost of nothing.
Passes when: #out should show unreachable.
10.Fix Dijkstra used on unweighted edgesbug fix · easy · 8 min · practice plan
When every weight is one, a plain breadth-first walk is simpler and faster.
Passes when: #out should show use bfs.
Between them these tasks cover Weighted edges, Relaxation and Choosing an algorithm.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.