Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Implement and Debug a Class
00:00
5 left

Implement and Debug a Class

HardPython

Problem

Databricks Workflows executes tasks whose dependencies form a directed acyclic graph. Implement a scheduler that produces a deterministic execution order and computes the earliest completion time for every task when unlimited parallel execution is available.

A task can start only after all of its prerequisites finish. When multiple tasks are ready, choose the lexicographically smallest task ID. If the dependency graph contains a cycle, no valid schedule exists.

Formal Specification

Implement schedule_workflow(tasks, dependencies).

  • tasks is a dictionary mapping a unique string task ID to a nonnegative integer duration.
  • dependencies is a list of two-element lists [prerequisite, dependent].
  • Each dependency means prerequisite must finish before dependent starts.
  • Return None if the graph contains a cycle.
  • Otherwise, return a dictionary with:
    • order: the deterministic topological order of task IDs.
    • finish_times: a dictionary mapping each task ID to its earliest finish time.
    • makespan: the earliest time at which all tasks finish.

Tasks with no prerequisites start at time zero. The scheduler must not mutate its inputs.

Constraints

  • 1 <= len(tasks) <= 10^5
  • 0 <= len(dependencies) <= 2 * 10^5
  • Task IDs are unique strings with length from 1 to 50
  • Dependency endpoints always exist in tasks
  • Duplicate dependency pairs do not appear
  • Task durations are nonnegative integers

Function Signature

def schedule_workflow(tasks, dependencies):
Interviewer

Your question is Implement and Debug a Class. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.