Order Agent Journey Steps With Dependencies and Report Cycles

Last updated: November 29, 2026

Quick Overview

An agent journey is a set of named steps plus pairs (before, after) meaning one step must run before another. Return the lexicographically smallest valid order, or if none exists, return one cycle of steps that causes the conflict.

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

0

0

0 solved


An agent journey is a set of named steps plus pairs (before, after) meaning one step must run before another. Return the lexicographically smallest valid order, or if none exists, return one cycle of steps that causes the conflict.

Sierra lets customers describe agent behavior as steps such as greet, verify identity, look up order and issue refund, and a misconfigured dependency must be explained, not just rejected. The interviewer checks Kahn's algorithm, deterministic tie-breaking with a heap, and producing a useful error.

What the Interviewer Expects
  • Build adjacency lists and in-degrees
  • Use a min-heap to get the lexicographically smallest order
  • Detect a cycle when fewer steps are emitted than exist
  • Extract an actual cycle rather than just saying one exists
  • Explain the complexity including the heap factor
Key Topics to Cover
topological sort
Kahn's algorithm
cycle detection
heaps
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 return all steps that are blocked by the cycle, not just the cycle?
  • How would you allow steps that can run in parallel and compute the minimum number of stages?
  • How would you validate this incrementally as a user edits the journey in a UI?
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

Steps are unique strings, dependencies reference known steps. Output ("ok", order) where among all valid orders we pick the lexicographically smallest, or ("cycle", [s1, s2, ...]) where s1 must pr...

High-Level Design

Kahn's algorithm repeatedly removes a step with no remaining prerequisites. Using a min-heap for the ready set always picks the smallest available name, which yields the smallest order. If the heap em...


Submit Your Answer
Markdown supported

Related Questions