PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Meta

Detect cycles and order with DFS

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding and implementation of graph traversal concepts such as depth-first search, explicit cycle detection, and topological ordering, and falls within the coding and algorithms domain, requiring practical application alongside conceptual understanding.

  • Medium
  • Meta
  • Coding & Algorithms
  • Machine Learning Engineer

Detect cycles and order with DFS

Company: Meta

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

You are given a directed graph of package dependencies represented as an adjacency list: Map<String, List<String>> deps where deps[p] lists packages that p depends on. Implement installOrder(List<String> packages, Map<String, List<String>> deps) that returns a valid installation order containing all packages, or indicates that no order exists if a cycle is present. Requirements: Use depth-first search with explicit cycle detection (recursion stack or color marking). The graph may be disconnected. If acyclic, output any valid topological order. Discuss iterative vs. recursive DFS, stack overflow risks, and complexity analysis.

Quick Answer: This question evaluates understanding and implementation of graph traversal concepts such as depth-first search, explicit cycle detection, and topological ordering, and falls within the coding and algorithms domain, requiring practical application alongside conceptual understanding.

Related Interview Questions

  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve a Key-Door Corridor Maze - Meta (medium)
  • Solve Array Merge and Parentheses Cleanup - Meta (medium)
  • Solve Two Backtracking Array Problems - Meta (hard)
  • Solve Maze and Suffix Problems - Meta (medium)
Meta logo
Meta
Sep 6, 2025, 12:00 AM
Machine Learning Engineer
Onsite
Coding & Algorithms
2
0

You are given a directed graph of package dependencies represented as an adjacency list: Map<String, List<String>> deps where deps[p] lists packages that p depends on. Implement installOrder(List<String> packages, Map<String, List<String>> deps) that returns a valid installation order containing all packages, or indicates that no order exists if a cycle is present. Requirements: Use depth-first search with explicit cycle detection (recursion stack or color marking). The graph may be disconnected. If acyclic, output any valid topological order. Discuss iterative vs. recursive DFS, stack overflow risks, and complexity analysis.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Meta•More Machine Learning Engineer•Meta Machine Learning Engineer•Meta Coding & Algorithms•Machine Learning 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.