PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of graph algorithms and scheduling, specifically reasoning about task dependencies, parallel execution, and cycle detection in directed graphs.

  • medium
  • Uber
  • Coding & Algorithms
  • Machine Learning Engineer

Compute minimal time to finish dependent tasks

Company: Uber

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Coding: Task Scheduling With Prerequisites (Parallel Allowed) You have `n` tasks labeled `1..n`. Each task takes **exactly 1 unit of time** to complete. Some tasks have prerequisites. You are given a list of prerequisite pairs `deps`, where each pair `[a, b]` means: - Task `a` must be completed **before** task `b` can start. You can run **any number of tasks in parallel** as long as their prerequisites are satisfied. ### Input - Integer `n`. - 2D array/list `deps` of pairs `[a, b]`. ### Output Return the **minimum total time** (number of time units) required to finish all tasks. ### Notes - If the dependency graph contains a cycle (tasks cannot be completed), specify what you would return (e.g., `-1`) and implement accordingly. ### Example If `n = 3` and `deps = [[1,3],[2,3]]`, tasks 1 and 2 can run in parallel in time 1, then task 3 runs in time 2, so the answer is 2.

Quick Answer: This question evaluates understanding of graph algorithms and scheduling, specifically reasoning about task dependencies, parallel execution, and cycle detection in directed graphs.

Tasks 1..n each take one unit. deps [a,b] means a before b. Unlimited parallelism is allowed. Return the minimum total time, or -1 if there is a cycle.

Constraints

  • Tasks are labeled 1..n

Examples

Input: (3, [[1, 3], [2, 3]])

Expected Output: 2

Input: (4, [[1, 2], [2, 3], [1, 3]])

Expected Output: 3

Input: (2, [[1, 2], [2, 1]])

Expected Output: -1

Input: (3, [])

Expected Output: 1

Hints

  1. The answer is the longest path length in the DAG.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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
  • AI Coding 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.

Related Coding Questions

  • Deep Equality of Two Records - Uber (medium)
  • Shortest Path in a Grid with Blocked Cells - Uber (medium)
  • Design and Implement an LRU Cache - Uber (medium)
  • Reconstruct the Alphabet Order of an Alien Language - Uber (medium)
  • Maximize Throughput and Count Trigger Components - Uber (medium)