← Snowflake Interview Insights
I got the topological sort part pretty quickly, BFS with in-degree tracking, standard stuff.
Model the courses as a directed graph and use topological sorting (Kahn's algorithm) to process courses in batches. For each batch, compute the maximum completion time among courses with zero in-degree, add it to the total time, and update in-degrees of dependent courses. If not all courses are processed, return -1 for a cycle.
Pro tip: Mention that this is a variation of the classic Course Schedule problem, but with weighted batches. Emphasize that the key insight is to process level by level (BFS) rather than one by one, and that using a queue naturally handles parallel execution.
Confirm that courses in the same batch run in parallel and the batch time is the maximum course time. Represent the prerequisites as a directed graph with adjacency lists and compute in-degrees for each course.
Use a queue to store courses with zero in-degree (ready to take). Maintain an array for in-degrees and a variable to accumulate total time.
While the queue is not empty, process all courses currently in the queue as one batch. For each course, update the in-degrees of its neighbors; if a neighbor's in-degree becomes zero, add it to the next batch queue. Track the maximum course time in the current batch and add it to the total time.
After processing, if the number of courses taken is less than the total number of courses, a cycle exists, so return -1. Otherwise, return the accumulated total time.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.