Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Python Helper Function Implementation

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Python Helper Function Implementation. Start with the requirements on the right.

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.

Problem

General Dynamics Land Systems mission software may represent an Abrams vehicle procedure as a recipe of named steps. Each step lists the steps that must finish before it can begin. Implement a helper that groups the recipe into execution batches, where every step in a batch can run concurrently and batches execute in order.

Return the batches in deterministic order by sorting step names alphabetically within each batch. Return an empty list if the recipe contains a dependency cycle.

Formal Specification

Implement schedule_recipe(steps), where steps is a dictionary mapping a step name to a list of prerequisite step names. Every prerequisite appears as a key in steps. Return a list of lists of strings. Each inner list is one executable batch.

Constraints

  • 1 <= len(steps) <= 10^4
  • Step names are unique non-empty strings
  • Every prerequisite appears as a key in steps
  • The total number of dependency relationships is at most 5 * 10^4
  • Return [] when the dependency graph contains a cycle

Function Signature

def schedule_recipe(steps):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output