PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/TikTok

Find a valid course order from prerequisites

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of graph algorithms, topological ordering, dependency resolution, and cycle detection within the Coding & Algorithms domain, and represents a practical application of algorithmic concepts.

  • medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Find a valid course order from prerequisites

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given `numCourses` labeled from `0` to `numCourses-1` and a list of prerequisite pairs `prerequisites`, where each pair `[a, b]` means you must complete course `b` before you can take course `a`. ## Task Return **one valid order** in which you can take all courses. - If there are multiple valid answers, return any one of them. - If it is **impossible** to finish all courses (because prerequisites contain a cycle), return an **empty list**. ## Input - `numCourses`: integer - `prerequisites`: list of pairs `[a, b]` ## Output - A list of length `numCourses` representing a valid topological ordering, or `[]` if none exists. ## Constraints (reasonable interview defaults) - `1 <= numCourses <= 2 * 10^5` - `0 <= len(prerequisites) <= 2 * 10^5` - `0 <= a, b < numCourses` ## Example Input: `numCourses = 4`, `prerequisites = [[1,0],[2,0],[3,1],[3,2]]` Output: `[0,1,2,3]` (or `[0,2,1,3]`)

Quick Answer: This question evaluates understanding of graph algorithms, topological ordering, dependency resolution, and cycle detection within the Coding & Algorithms domain, and represents a practical application of algorithmic concepts.

Related Interview Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)
TikTok logo
TikTok
Jan 20, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
1
0
Loading...

You are given numCourses labeled from 0 to numCourses-1 and a list of prerequisite pairs prerequisites, where each pair [a, b] means you must complete course b before you can take course a.

Task

Return one valid order in which you can take all courses.

  • If there are multiple valid answers, return any one of them.
  • If it is impossible to finish all courses (because prerequisites contain a cycle), return an empty list .

Input

  • numCourses : integer
  • prerequisites : list of pairs [a, b]

Output

  • A list of length numCourses representing a valid topological ordering, or [] if none exists.

Constraints (reasonable interview defaults)

  • 1 <= numCourses <= 2 * 10^5
  • 0 <= len(prerequisites) <= 2 * 10^5
  • 0 <= a, b < numCourses

Example

Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]

Output: [0,1,2,3] (or [0,2,1,3])

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More TikTok•More Software Engineer•TikTok Software Engineer•TikTok Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.