Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Topological Sort DSA

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

Your question is Topological Sort DSA. 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

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output