You are given a catalog of training courses. Each course has:
-
a unique
course_id
-
a list of prerequisite course IDs
prereqs
(possibly empty)
Write a function isValidCatalog(courses) -> bool that validates the catalog.
A catalog is considered valid if:
-
Every prerequisite referenced by a course
exists
in the catalog.
-
The prerequisite relationships contain
no cycles
(a course cannot depend on itself directly or indirectly).
Follow-ups (discuss, no need to implement unless asked):
-
How would you also return a valid course-taking order when it’s valid?
-
Time and space complexity.
-
Compare DFS vs BFS approaches for detecting cycles / producing a topological order.
Example:
-
A: [B, C]
,
B: [C]
,
C: []
-> valid
-
A: [B]
,
B: [A]
-> invalid (cycle)
-
A: [X]
where
X
not in catalog -> invalid