Bird
Raised Fist0

Given the following code snippet implementing the optimal BFS topological sort for minimum semesters, what is the output for n = 4 and relations = [[1,2],[1,3],[3,4]]?

easy🧾 Code Trace Q3 of Q15
Topological Sort - Parallel Courses (Minimum Semesters)
Given the following code snippet implementing the optimal BFS topological sort for minimum semesters, what is the output for n = 4 and relations = [[1,2],[1,3],[3,4]]?
A4
B3
C2
D-1
Step-by-Step Solution
Solution:
  1. Step 1: Trace initial indegree and queue

    Indegree: [0,0,1,1,1]; queue starts with course 1 (indegree 0).
  2. Step 2: Process semesters level-wise

    Semester 1: take course 1; Semester 2: courses 2 and 3 become available; Semester 3: course 4 becomes available.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    3 semesters needed to complete all courses [OK]
Quick Trick: Count BFS levels in topological order for semesters [OK]
Common Mistakes:
MISTAKES
  • Counting courses instead of semesters
  • Not processing all courses in a semester
Trap Explanation:
PITFALL
  • Candidates often forget parallel processing per semester, counting each course separately.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute BFS topological sort and count levels.
Master "Parallel Courses (Minimum Semesters)" in Topological Sort

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Topological Sort Quizzes