Count how many edges point at node 2.
Acceptance criteria
- 1.#out should show indegree 1.
DSA Tutorial · Graphs
These 10 tasks go with the Topological Sort 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: “Any question about ordering things with dependencies is a topological sort. Course prerequisites, build steps, module imports, task schedules — same problem, different nouns.” .
1.Count in-degreesexercise · easy · 10 min
Count how many edges point at node 2.
Passes when: #out should show indegree 1.
2.Order a simple chainexercise · easy · 10 min · practice plan
Produce a topological order for a chain and join it.
Passes when: #out should show 0-1-2.
3.Detect a cycleexercise · easy · 10 min · practice plan
Report that a two-node cycle has no valid ordering.
Passes when: #out should show cycle found.
4.Find the starting nodesexercise · easy · 10 min · practice plan
Count how many nodes have no incoming edge.
Passes when: #out should show one start.
5.Several valid ordersexercise · medium · 10 min · practice plan
Report whether more than one ordering can exist when two nodes are ready.
Passes when: #out should show many orders.
6.Fix in-degree counting the sourcebug fix · easy · 8 min
The first element of an edge is where it comes from, not where it points.
Passes when: #out should show indegree 1.
7.Fix the queue seeded with everythingbug fix · easy · 8 min · practice plan
Starting with every node ignores the dependencies entirely.
Passes when: #out should show 0-1-2.
8.Fix the cycle reported as an orderbug fix · medium · 8 min · practice plan
A shorter output than the node count means no ordering exists.
Passes when: #out should show cycle found.
9.Fix the decrement compared before subtractingbug fix · easy · 8 min · practice plan
Using a post-decrement checks the old value and enqueues too early.
Passes when: #out should show 0-1-2.
10.Fix the start count including targetsbug fix · easy · 8 min · practice plan
Nodes with incoming edges are not starting points.
Passes when: #out should show one start.
Between them these tasks cover In-degree, Kahn's algorithm and Cycle detection.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.