Skip to main content
All Questions

Order tasks with mandatory and costed optional dependencies, breaking cycles cheaply.

Unlock detailed company stats for this questionUpgrade

Given tasks numbered 0 to n-1 and two types of dependencies: mandatory dependencies (strictly required sequence) and optional dependencies, each with an associated cost.

Determine a valid execution order. If a valid sequence exists using all dependencies, return it. If a cycle is detected, remove the optional dependency with the minimum cost that breaks the cycle and return the resulting order. If the cycle consists purely of mandatory dependencies, it cannot be resolved, so return an empty list.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.