Order SmartThings Routine Dependencies and Report a Cycle

Last updated: November 29, 2026

Quick Overview

Given home automation routines and pairs (a, b) meaning a must finish before b starts, return a valid execution order that is alphabetically smallest at each choice, or report one dependency cycle if no order exists.

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

0

0

0 solved


Given home automation routines and pairs (a, b) meaning a must finish before b starts, return a valid execution order that is alphabetically smallest at each choice, or report one dependency cycle if no order exists.

SmartThings lets users chain scenes and routines, and a misconfigured chain can deadlock. This screen checks topological sorting, deterministic output and, importantly, producing an actionable error (the actual cycle) rather than just a boolean.

What the Interviewer Expects
  • Uses Kahn's algorithm with a min-heap for deterministic order
  • Detects failure when the order is shorter than the routine count
  • Extracts a concrete cycle with a colored DFS
  • Uses an iterative DFS to avoid recursion limits
  • Gives O((V+E) log V) complexity
Key Topics to Cover
topological sort
cycle detection
heaps
graph DFS
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
  • Routines can also have a delay. How would you compute the earliest finish time of every routine?
  • How would you validate a new dependency at edit time incrementally instead of re-sorting everything?
  • Some routines can run in parallel. How do you output batches?
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

Nodes are routine names; edges a->b. Output (order, cycle). If acyclic, cycle is empty. If cyclic, return the partial order Kahn produced plus one cycle like [a, b, c, a] for the app to show the user....

High-Level Design

Kahn: compute indegrees, push zero-indegree nodes into a min-heap, pop smallest, decrement neighbors. If every node is output the graph is a DAG. Otherwise run DFS with white/gray/black colors; hittin...


Submit Your Answer
Markdown supported

Related Questions