Order Course Units When Some Require Earlier Units
Last updated: November 29, 2026
Quick Overview
A course path lists units with a display position, plus prerequisite pairs saying one unit must be finished before another unlocks. Return an order that respects every prerequisite and, among valid choices, always takes the unit with the lowest display position next; raise an error naming the units involved if the rules form a cycle.
Duolingo
November 29, 20260
0
0 solved
A course path lists units with a display position, plus prerequisite pairs saying one unit must be finished before another unlocks. Return an order that respects every prerequisite and, among valid choices, always takes the unit with the lowest display position next; raise an error naming the units involved if the rules form a cycle.
Duolingo's home screen is a guided path, and content authors for languages, math and music define which units depend on others. This onsite question checks topological sorting, using a priority queue to keep the order close to the authored layout, and producing an error message an author can actually act on.
What the Interviewer Expects
- Recognizes the problem as topological sort
- Uses a min-heap keyed on display position for a stable, author-friendly order
- Detects cycles by comparing output length to unit count
- Reports which units are stuck rather than a generic failure
- Handles units with no prerequisites and prerequisites naming unknown units
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you validate this at content publish time across 100 courses?
- How would you explain to an author which edge to remove to break a cycle?
- What changes if a unit unlocks when any one of several prerequisites is done?
- How would you compute the earliest point each unit can unlock?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: units mapping id to display position, prereqs list of (before, after). Output: list of ids. Among units whose prerequisites are all satisfied, pick the lowest position, so the order match...
High-Level Design
Kahn's algorithm: compute in-degrees, seed a heap with zero in-degree units keyed by position, pop the minimum, decrement neighbors, and push any that reach zero. If fewer units come out than went in,...