FastPrepTopological Sort for Ads Tasks
Problems/Topological Sort for Ads Tasks

Topological Sort for Ads Tasks

Netflix logoNetflix● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given numTasks tasks, labeled from 0 to numTasks - 1, and a list of prerequisite edges called edges. Each edge [before, after] means task before must be done before task after.

Return one order that completes all the tasks. A task is ready when all of its prerequisites are done. If more than one task is ready at the same time, take the smaller task id first, so there is only one correct answer. If the dependency graph contains a cycle, it is impossible to complete all tasks, so return an empty array.

Function

findTaskOrder(numTasks: int, edges: int[][]) → int[]

Examples

Example 1

numTasks = 4edges = [[0, 1], [0, 2], [1, 3], [2, 3]]return = [0, 1, 2, 3]

Task 0 has no prerequisites, so it comes first. That makes tasks 1 and 2 ready, and the smaller id is taken first. The last task becomes ready once both are done, which gives the order shown.

Example 2

numTasks = 2edges = [[0, 1], [1, 0]]return = []

Each of the two tasks must come before the other. That is a cycle, so no order works and the answer is an empty array.

Constraints

  • 0 <= before, after < numTasks
  • Solve it with a topological sort that also detects cycles. A topological sort lists the tasks so that each task comes after all of its prerequisites.
  • When more than one task is ready (it has no prerequisites left undone), choose the smallest task id first.

More Netflix interview questions

  • Timed Cache with Sidecar CleanupPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Repeated Titles in Viewing HistoryPHONE SCREEN · Seen Sep 2026
  • Capacity-Limited Timed CachePHONE SCREEN · Seen Sep 2026
  • Custom Record and Free-Form Field ParserPHONE SCREEN · Seen Sep 2026
  • Dynamic Broadcast TimesPHONE SCREEN · Seen Aug 2026
  • Count String Pairs With Disjoint CharactersPHONE SCREEN · Seen Aug 2026
  • Deduplicate Homepage Content Across RowsPHONE SCREEN · Seen Aug 2026
  • Longest Consecutive Identical Character RunPHONE SCREEN · Seen Aug 2026
  • Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Aug 2026
  • Score-Rotating Movie PlaylistONSITE INTERVIEW · Seen Sep 2026

Similar questions at other companies

  • Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Course ScheduleByteDance · PHONE SCREENByteDance interview questions
  • Course ScheduleNykaa · PHONE SCREENNykaa interview questions
  • Course ScheduleBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Hubs With the Shortest Longest ChainAdobe · PHONE SCREENAdobe interview questions
  • Task Order With PrerequisitesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
  • Course Schedule FeasibilityTesla · PHONE SCREENTesla interview questions
  • Job Schedule with One Initial Ready JobDatadog · PHONE SCREENDatadog interview questions
  • Course SchedulePlaud · ONSITE INTERVIEW
  • Find a Valid Course Completion OrderGoogle · ONSITE INTERVIEWGoogle interview questions
All Netflix interview questionsSee Netflix hiring insights
Saved
class Solution {
    public int[] findTaskOrder(int numTasks, int[][] edges) {
      // write your code here
    }
}
Checking account…