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
Coding & Algorithms
Software Engineer
Duolingo
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

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
Topological sort
Kahn's algorithm
Priority queues
Cycle detection
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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,...


Submit Your Answer
Markdown supported

Related Questions