Lexicographically Smallest Build Order for Brazil Package Dependencies

Last updated: November 29, 2026

Quick Overview

Given package names and pairs meaning one package must build before another, return the build order that respects all dependencies and is lexicographically smallest, or an empty list if a cycle prevents any order.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given package names and pairs meaning one package must build before another, return the build order that respects all dependencies and is lexicographically smallest, or an empty list if a cycle prevents any order.

Amazon's internal build system resolves package dependency graphs, and teams want deterministic, reproducible build logs. The interviewer wants to see you swap Kahn's FIFO queue for a min-heap and justify why that yields the smallest order.

What the Interviewer Expects
  • Build adjacency and in-degree maps keyed by package name
  • Use a min-heap as the ready set to force deterministic order
  • Argue the greedy choice: the smallest ready package is always safe to emit first
  • Return empty list on cycle
  • State O((n + m) log n) complexity
Key Topics to Cover
topological sort
min-heap
deterministic ordering
cycle detection
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
  • Why does a plain BFS queue not give the smallest order?
  • How would you report which packages are in the cycle?
  • What if builds of independent packages run in parallel on a fleet of hosts?
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: a list of package names and (a, b) pairs meaning a must build before b. Output: the lexicographically smallest valid order as a list, or [] if impossible. Names are unique strings; all ...

High-Level Design

Kahn's algorithm emits any ready node. To get the smallest sequence, emit the smallest ready node each time: keep ready packages in a min-heap.

Greedy proof sketch: at step i, any valid order must pl...


Submit Your Answer
Markdown supported