PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Snowflake

Compute total time to finish all courses

Last updated: Mar 29, 2026

Quick Overview

This question evaluates competency in graph algorithms and scheduling, including dependency resolution, cycle detection, and aggregation of task durations; it belongs to the Coding & Algorithms domain and primarily tests practical application of algorithmic reasoning with conceptual understanding of precedence constraints.

  • medium
  • Snowflake
  • Coding & Algorithms
  • Software Engineer

Compute total time to finish all courses

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem You are given `n` courses labeled `1..n`. Each course `i` takes `time[i]` units of time to complete. You are also given prerequisite relations `prereqs`, where each pair `[a, b]` means **course `a` must be completed before course `b` can be taken**. Courses are taken in **batches (rounds/semesters)** under the following rule: - At the **start** of a batch, you may start **any number of courses** whose prerequisites are already satisfied. - The batch **finishes only when all courses started in that batch finish**. - The duration of a batch is therefore `max(time[c])` over all courses `c` taken in that batch. - You can only start the next batch after the current batch finishes. ### Task Return the **minimum total time** required to finish all courses. ### Cycle condition If the prerequisite graph contains a cycle (so it is impossible to finish all courses), return `-1`. ## Example Suppose prerequisites imply two batches: first take courses `{1, 3}`, then take `{2, 4}` (e.g., edges `1 -> 2` and `3 -> 4`). - `time = [1, 10, 10, 1]` for courses `1..4` - Batch 1 duration: `max(1, 10) = 10` - Batch 2 duration: `max(10, 1) = 10` - Total = `20` ## Follow-up (variant scheduling rule) How would your approach change if courses can start **immediately when their prerequisites are satisfied**, without waiting for the rest of the current batch to finish (i.e., no batch barrier; courses can overlap asynchronously)? In that variant, return the minimum total completion time or `-1` if a cycle exists. ## Constraints (typical) - `1 <= n <= 2 * 10^5` - `0 <= prereqs.length <= 2 * 10^5` - `1 <= time[i] <= 10^9`

Quick Answer: This question evaluates competency in graph algorithms and scheduling, including dependency resolution, cycle detection, and aggregation of task durations; it belongs to the Coding & Algorithms domain and primarily tests practical application of algorithmic reasoning with conceptual understanding of precedence constraints.

Related Interview Questions

  • Solve Array Distance and Wiki Navigation - Snowflake (medium)
  • Implement Document Predicate APIs - Snowflake (medium)
  • Find Shortest Wiki Click Path - Snowflake (medium)
  • Schedule prerequisite classes with retakes - Snowflake (easy)
  • Solve three coding rounds - Snowflake (medium)
Snowflake logo
Snowflake
Jan 6, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
3
0
Loading...

Problem

You are given n courses labeled 1..n. Each course i takes time[i] units of time to complete.

You are also given prerequisite relations prereqs, where each pair [a, b] means course a must be completed before course b can be taken.

Courses are taken in batches (rounds/semesters) under the following rule:

  • At the start of a batch, you may start any number of courses whose prerequisites are already satisfied.
  • The batch finishes only when all courses started in that batch finish .
  • The duration of a batch is therefore max(time[c]) over all courses c taken in that batch.
  • You can only start the next batch after the current batch finishes.

Task

Return the minimum total time required to finish all courses.

Cycle condition

If the prerequisite graph contains a cycle (so it is impossible to finish all courses), return -1.

Example

Suppose prerequisites imply two batches: first take courses {1, 3}, then take {2, 4} (e.g., edges 1 -> 2 and 3 -> 4).

  • time = [1, 10, 10, 1] for courses 1..4
  • Batch 1 duration: max(1, 10) = 10
  • Batch 2 duration: max(10, 1) = 10
  • Total = 20

Follow-up (variant scheduling rule)

How would your approach change if courses can start immediately when their prerequisites are satisfied, without waiting for the rest of the current batch to finish (i.e., no batch barrier; courses can overlap asynchronously)? In that variant, return the minimum total completion time or -1 if a cycle exists.

Constraints (typical)

  • 1 <= n <= 2 * 10^5
  • 0 <= prereqs.length <= 2 * 10^5
  • 1 <= time[i] <= 10^9

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Snowflake•More Software Engineer•Snowflake Software Engineer•Snowflake Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.