This question evaluates competency in graph algorithms, dependency resolution, stateful scheduling, and robust error handling within the Coding & Algorithms domain.
You are building a course-taking engine.
Each course is represented as an object:
id
(unique identifier)
prevCoursesList
: a list of prerequisite course IDs that must be
passed
before this course can be attempted.
You are given:
roots
: a list of
target
course IDs the student ultimately wants to complete.
didPass(courseId) -> bool
that can be called
each time you attempt
a course. If it returns
false
, the student failed that attempt and must retake the course later.
didPass(id)
returns
true
, mark it as passed.
false
, the course must be retaken later (i.e., re-queued for another attempt).
course -> prerequisites
). You must first discover all involved courses reachable from
roots
and build any additional relationships you need (e.g., prerequisite → dependent adjacency).
Implement a function that returns the sequence of course IDs in the order they were attempted until:
roots
have been passed, or
maxRetries
failed attempts.
Return:
attemptOrder
: list of attempted course IDs (including retries), if successful.
roots
.
maxRetries
is a small integer (e.g., 3–10).
roots
.
maxRetries
.