Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Lexicographically Smallest Topological Ordering
00:00
5 left

Lexicographically Smallest Topological Ordering

MediumPython

Problem

Speechify Reader may need to assemble a document from segments such as a title, headings, paragraphs, and footnotes. Some segments must be played before others. Given the segment identifiers and precedence constraints, return a valid playback order.

If multiple segments are currently available, choose the lexicographically smallest identifier. This makes the result deterministic. If the constraints contain a cycle, return an empty list because no valid playback order exists.

Formal Specification

Implement schedule_playback(items, prerequisites), where items is a list of unique strings and prerequisites is a list of pairs [before, after], meaning before must appear earlier than after. Return a list containing every item exactly once, or [] when no valid ordering exists.

Constraints

  • 1 <= len(items) <= 10^5
  • 0 <= len(prerequisites) <= 2 * 10^5
  • Every prerequisite contains exactly two identifiers from items
  • Item identifiers are unique, non-empty strings of at most 50 characters
  • Duplicate prerequisite pairs may appear
  • Return the lexicographically smallest valid topological ordering

Function Signature

def schedule_playback(items, prerequisites):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.