Practice/Apple/Job Scheduling Algorithms Implementation
Job Scheduling Algorithms Implementation
CodingMust
Problem
You are building a process scheduler for an operating system that needs to support multiple scheduling strategies. Given a list of processes and a scheduling strategy, determine the execution order and calculate timing metrics for each process.
Each process is represented as [arrival_time, burst_time, priority] where:
arrival_time: The time when the process arrives in the ready queue
burst_time: The total execution time needed for the process
priority: Priority level (higher numbers indicate higher priority)
Your scheduler must support two strategies:
- FCFS (First Come First Serve): Processes are executed in the order they arrive
- PRIORITY: Processes are executed based on priority (higher priority first). If priorities are equal, use arrival time as a tiebreaker
Return a list of [arrival_time, completion_time, waiting_time] for each process in the original input order.
Requirements
- Implement both FCFS and priority-based scheduling algorithms
- Calculate completion time for each process (when it finishes execution)
- Calculate waiting time for each process (time spent in queue before starting execution)
- For FCFS, maintain strict arrival order
- For PRIORITY, execute highest priority processes first
- If a process arrives after the CPU is idle, start it immediately
- Return results in the same order as input processes
Constraints
- 1 ≤ processes.length ≤ 1000
- 0 ≤ arrival_time ≤ 10^6
- 1 ≤ burst_time ≤ 1000
- 1 ≤ priority ≤ 10
- All time values are non-negative integers
- Strategy will be either "FCFS" or "PRIORITY"
Examples
Example 1:
`
Input: processes = [[1, 3, 2], [2, 5, 1], [4, 6, 3]], strategy = "FCFS"
Output: [[1, 4, 0], [2, 9, 2], [4, 15, 5]]
Explanation:
- Process 0 arrives at time 1, starts immediately, completes at time 4 (1+3), waiting time = 0
- Process 1 arrives at time 2, waits until time 4, completes at time 9 (4+5), waiting time = 2
- Process 2 arrives at time 4, waits until time 9, completes at time 15 (9+6), waiting time = 5
`
Example 2:
`
Input: processes = [[0, 4, 2], [1, 3, 1], [2, 2, 3]], strategy = "PRIORITY"
Output: [[0, 6, 2], [1, 9, 5], [2, 4, 0]]
Explanation:
- Process 2 has highest priority (3), arrives at time 2, executes first after process 0 starts
- Process 0 has priority 2, starts at time 0, completes at time 4
- Process 2 executes from time 4 to 6
- Process 1 has lowest priority (1), executes last from time 6 to 9
`
Example 3:
Input: processes = [[0, 5, 1]], strategy = "FCFS" Output: [[0, 5, 0]] Explanation: Single process starts immediately at time 0 and completes at time 5