Order Schema Migrations by Declared Dependencies and Report Cycles
Last updated: November 29, 2026
Quick Overview
Given a map from migration name to the migrations it depends on, return a deterministic order in which to apply them, or raise an error naming one concrete dependency cycle if no valid order exists.
Stripe
November 29, 20260
0
0 solved
Given a map from migration name to the migrations it depends on, return a deterministic order in which to apply them, or raise an error naming one concrete dependency cycle if no valid order exists.
Stripe runs large online schema and data migrations across many services, and tooling that orders them safely is the kind of internal infrastructure engineers build. The interviewer checks a correct topological sort, a deterministic tie-break so every deploy applies migrations in the same order, and an error message a human can act on.
What the Interviewer Expects
- Validate that every referenced dependency exists before sorting
- Use Kahn's algorithm (or DFS) and explain why it detects cycles
- Break ties lexicographically with a heap so output is reproducible
- On failure, report an actual cycle path, not just "cycle detected"
- State time and space complexity in terms of nodes and edges
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 apply independent migrations in parallel while respecting dependencies?
- What if some migrations are already applied in production and must be skipped?
- How would you detect that a new migration was inserted before an already-applied one?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: deps such as {"add_index": ["create_table"], "create_table": []}. Output: a list with every migration after all of its dependencies, ties broken alphabetically. Unknown dependency names and...
High-Level Design
Build in-degrees (number of unmet dependencies) and a reverse adjacency list from each migration to its dependents. Push all zero in-degree migrations onto a min-heap; repeatedly pop the smallest, app...