Design a Rule-Driven Process Graph Validator
HadrianSoftware Engineer · OnsiteLast updated
This problem tests your ability to model dependency constraints in a directed graph and implement an efficient algorithm to validate that a given execution sequence satisfies all structural and ordering requirements.
Problem Statement
You are designing a validator for a process automation engine. A process is defined as a directed graph where nodes represent tasks and edges represent dependencies. The validator must ensure that a given execution sequence of tasks satisfies two types of rules:
- Presence Rules: A list of tasks that must appear in the execution.
- Ordering Rules: A set of constraints defined as
(TaskA, TaskB), meaning TaskA must be completed before TaskB starts.
The validator receives a process_graph (an adjacency list representation), a set of required_tasks, and a list of ordering_rules.
Implement the following interface:
def validate_process(process_graph, execution_sequence, required_tasks, ordering_rules):
# Returns True if the execution_sequence is valid, False otherwise
Constraints
process_graph: A dictionary mapping task IDs to lists of downstream task IDs.execution_sequence: A list of task IDs representing the actual order of execution.required_tasks: A set of strings.ordering_rules: A list of tuples(u, v)whereumust precedev.
Example 1
required_tasks: {'A', 'B', 'C'}ordering_rules: [('A', 'B'), ('B', 'C')]execution_sequence: ['A', 'B', 'C']- Result:
True
Example 2
required_tasks: {'A', 'B', 'C'}ordering_rules: [('A', 'B')]execution_sequence: ['B', 'A', 'C']- Result:
False(because A must precede B).
Edge Cases
- The
execution_sequencecontains tasks not in theprocess_graph. - The
ordering_rulesform a cycle (which should fail validation). - The
execution_sequenceomits a required task.
Hints
Hint 1
Use a hash map to store the index of each task in the execution_sequence for O(1) lookups of relative positions.
Hint 2
First, verify that every task in required_tasks exists exactly once in the execution_sequence.
Hint 3
For ordering rules (A, B), verify that the index of A is strictly less than the index of B in the sequence.
Follow-up questions
- How would you modify your validator if the ordering rules were dynamic and could be updated while the process is currently running?
- What changes are needed to support parallel execution where tasks can overlap in time rather than being a strictly ordered list?
- If the process graph is massive, how would you optimize the validation process to fail early as soon as an invalid sequence is detected?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.