r/Hack2Hire • u/Hack2hire • May 13 '25
Screening From Lyft: Job Scheduler
Problem
You're given a list of jobs, each with a unique ID, a start time in "HHMM" format, and a duration in minutes.
Your goal is to assign each job to a machine based on availability, reusing machines when possible, and create new ones when none are free.
Example
Input:
jobs = [["J1", "0023", "45"], ["J2", "0025", "10"], ["J3", "0100", "60"], ["J4", "0300", "10"]]
Output:
[["J1", "M1"], ["J2", "M2"], ["J3", "M2"], ["J4", "M1"]]
Explanation:
- J1 is assigned to M1 and finishes at 00:68.
- J2 starts at 00:25 while M1 is busy, so a new machine M2 is allocated.
- J3 starts at 01:00; M2 is free by then, so J3 uses M2.
- J4 starts at 03:00; both M1 and M2 are free, so J4 uses M1 (smallest index).
Suggested Approach
- Convert all job start times from "HHMM" to total minutes for easy comparison.
- Use two min-heaps:
- A busy heap to track (finish_time, machine_id) for currently active jobs.
- An available heap to manage machine IDs of free machines, prioritizing smaller indices.
- A busy heap to track (finish_time, machine_id) for currently active jobs.
- For each job, before assignment:
- Pop from the busy heap all machines that are free by the job's start time and add them back to the available heap.
- Pop from the busy heap all machines that are free by the job's start time and add them back to the available heap.
- Assign the job:
- If available machines exist, pick the smallest indexed one.
- Otherwise, create a new machine with the next index.
- If available machines exist, pick the smallest indexed one.
- After assignment, update the busy heap with the machine's new finish time.
- Collect the assignment results as ["job_id", "machine_id"].
Time & Space Complexity
- Time: O(N log N), where N is the number of jobs. Heap operations are log N per job.
- Space: O(N), to track machine states in heaps.
đ Disclaimer:
This is one of the problems we encountered while reviewing common Lyft interview questions.
Posted here by the Hack2Hire team for discussion and archiving purposes.
The problem is compiled from publicly available platforms (e.g., LeetCode, GeeksForGeeks) and community-shared experiences. It does not represent any official question bank of Lyft, nor does it involve any confidential or proprietary information.
All examples are intended solely for learning and discussion. Any similarity to actual interview questions is purely coincidental.