Order Ansible Automation Roles and Report Dependency Cycles

Last updated: November 29, 2026

Quick Overview

Given automation roles and their prerequisite roles, return a valid execution order, or if impossible, return the set of roles that are stuck because they sit on or behind a dependency cycle.

IBM
Coding & Algorithms
Software Engineer
IBM
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given automation roles and their prerequisite roles, return a valid execution order, or if impossible, return the set of roles that are stuck because they sit on or behind a dependency cycle.

Red Hat Ansible Automation Platform resolves role dependencies before running a playbook, so topological sorting with useful error reporting fits IBM and Red Hat onsite coding rounds. The interviewer looks for Kahn's algorithm and, importantly, a helpful failure output instead of just returning false.

What the Interviewer Expects
  • Model prerequisites as directed edges from prerequisite to dependent
  • Use Kahn's algorithm with in-degree counts
  • Detect cycles when the output is shorter than the role count
  • Report which roles could not be scheduled
  • Keep the order deterministic for reproducible runs
Key Topics to Cover
topological sort
Kahn's algorithm
cycle detection
directed graphs
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you print one actual cycle rather than all stuck roles?
  • How would you run independent roles in parallel waves?
  • What if a prerequisite name is misspelled and not defined as a role?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: deps, a dictionary mapping each role to a list of prerequisite roles. Prerequisites that are not keys still count as roles with no prerequisites. Output: (order, stuck). If all roles can ru...

High-Level Design

Collect every role name, compute in-degrees (number of prerequisites) and a reverse map from prerequisite to dependents. Seed a min-heap with zero in-degree roles so ties resolve alphabetically, which...


Submit Your Answer
Markdown supported