Greedy Scheduling And Resource Reuse
Asked of: Software Engineer
Last updated

What's being tested
Candidates must show proficiency with greedy scheduling and resource reuse: converting temporal/resource constraints into an algorithmic strategy that minimizes concurrent resource count. Interviewers probe ordering (sorting/sweep), reuse tracking (min-heap or multiset), and correctness under ties and edge cases like simultaneous end/start. For dependency-flavored variants, they expect topological ordering and cycle detection to layer execution before applying parallelism limits.
Patterns & templates
-
Sort + sweep-line by start time, maintain active resource end-times in a
min-heap(heapq); reuse when smallest end ≤ new start, O(n log n). -
Min-heap of end times: push trip/service end, pop while top ≤ current start; heap size = concurrent resources needed.
-
Greedy assignment: always reuse earliest-finishing resource; proven optimal for interval partitioning (activity/interval scheduling duals).
-
Kahn's algorithm for DAGs: compute in-degree, extract zero-degree nodes per layer to form parallelizable batches, O(V+E).
-
Detect cycles with DFS (coloring) or Kahn; if cycle exists, no valid layered startup ordering.
-
Concurrency bounding: after layering, treat each layer as set of independent tasks and schedule with a thread pool / semaphore, O(total tasks log k).
-
Edge-case tie rules: define whether end==start allows reuse; implement consistent comparator to avoid off-by-one bugs.
Common pitfalls
Pitfall: Sorting only by start time and ignoring equal-time end/start semantics loses reuse opportunities or double-counts resources.
Pitfall: Using an unsized array or naive nested loops yields O(n^2) for large n; prefer heap-based O(n log n).
Pitfall: For dependency problems, returning any topological order without checking cycles will miss unsatisfiable inputs.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Find the Minimum Fleet for a Bus TimetableSnowflake · Software Engineer · Technical Screen · medium
- Find the Minimum Bus Fleet with Station ConstraintsSnowflake · Software Engineer · Technical Screen · medium
- Compute task order and layered executionSnowflake · Software Engineer · Onsite · medium
- Design multi-core service startup schedulerSnowflake · Software Engineer · Onsite · hard
- Schedule dependent services with layered startupSnowflake · Software Engineer · Onsite · medium
Related concepts
- Greedy, Heaps, And Scheduling OptimizationCoding & Algorithms
- Heaps, Top-K, And Streaming SelectionCoding & Algorithms
- Intervals, Sliding Windows, And Time-Ordered StateCoding & Algorithms
- Interval Scheduling And Calendar SystemsCoding & Algorithms
- Time Interval Overlap And Sweep-Line AlgorithmsCoding & Algorithms
- Dynamic Programming, Scheduling, And Set CoverCoding & Algorithms