Implement ordering with dependency constraints
Company: Nextdoor
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of dependency resolution and ordering within graph-based problems, including cycle detection, lexicographic tie-breaking, and extensions requiring certain items to appear as contiguous groups.
Constraints
- 0 <= u,v < n
- If multiple nodes are available, choose the smallest id first
Examples
Input: (4, [[0, 2], [1, 2], [2, 3]])
Expected Output: [0, 1, 2, 3]
Input: (3, [[0, 1], [1, 2], [2, 0]])
Expected Output: 'IMPOSSIBLE'
Input: (3, [])
Expected Output: [0, 1, 2]
Hints
- Kahn topological sort with a min-heap gives the lexicographically smallest order.