Find a Valid Task Execution Order with Dependencies
Last updated: June 30, 2026
Quick Overview
The candidate is asked to find a valid order of task execution given a set of tasks and their dependencies.
Salesforce
June 30, 20261
0
12 solved
The candidate is asked to find a valid order of task execution given a set of tasks and their dependencies.
This question is commonly asked during onsite interviews for software engineering roles to assess a candidate's problem-solving skills and understanding of graph theory. The interviewer evaluates the candidate's ability to implement algorithms that can handle directed acyclic graphs (DAGs) and their approach to managing dependencies effectively. This question also tests the candidate's coding skills and their ability to write efficient and clean code.
What the Interviewer Expects
- Clearly explain the problem and the approach to solving it.
- Identify the data structure needed to represent tasks and dependencies.
- Implement a topological sort algorithm to determine the valid execution order.
- Discuss the time and space complexity of the solution.
- Provide a working code example that handles edge cases.
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you handle cyclic dependencies?
- Can you optimize your solution further?
- What would you change if tasks could have multiple dependencies?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Approach
To solve the problem of finding a valid task execution order given dependencies, we can represent the tasks and their dependencies as a directed graph. Each task is a node, and a directed edge from ta...
Algorithm
We can use Kahn's algorithm for topological sorting, which involves the following steps:
- Initialize an in-degree array to count incoming edges for each node.
- Populate the in-degree array based o...