CPU Scheduling Simulator
Updated
Problem statement
Simulate one CPU running n processes under a scheduling policy. Process i arrives at time arrival[i] and needs burst[i] units of CPU time. Time starts at 0; whenever no process is ready, the CPU waits for the next arrival. Return finish, where finish[i] is the time process i completes.
"FCFS"(first come, first served): when the CPU is free, it runs the waiting process that arrived first, to completion. Same arrival time: lower index first."SJF"(shortest job first, non-preemptive): when the CPU is free, it runs the waiting process with the smallest burst, to completion. Ties go to the earlier arrival, then the lower index. A process that arrives at the moment the CPU becomes free is already waiting."RR"(round robin): waiting processes form a queue in arrival order (same time: lower index first). The CPU runs the process at the front for up toquantumunits. If it is not finished, it goes to the back of the queue, behind every process that arrived up to and including the moment its slice ended.
quantum is used only by "RR".
Function
scheduleProcesses(policy: String, arrival: int[], burst: int[], quantum: int) → int[]Examples
Example 1
policy = "FCFS"arrival = [0,1,2,3]burst = [5,3,1,2]quantum = 2return = [5,8,9,11]Processes run in arrival order: P0 from 0 to 5, P1 from 5 to 8, P2 from 8 to 9, P3 from 9 to 11. The quantum is ignored.
Example 2
policy = "SJF"arrival = [0,1,2,3]burst = [5,3,1,2]quantum = 2return = [5,11,6,8]P0 is the only process at time 0 and runs to 5 (no preemption). At 5, P1, P2 and P3 are waiting; the shortest is P2 (burst 1), which runs to 6, then P3 (burst 2) to 8, then P1 (burst 3) to 11.
Example 3
policy = "RR"arrival = [0,1,2,3]burst = [5,3,1,2]quantum = 2return = [11,10,5,9]With quantum 2: P0 runs 0-2 (P1 and P2 arrived meanwhile, and they are queued before P0 returns). P1 runs 2-4; P3 arrives at 3, so the queue is now P2, P0, P3, P1. P2 runs 4-5 and finishes. P0 runs 5-7, P3 runs 7-9 and finishes, P1 runs 9-10 and finishes, and P0 runs 10-11 and finishes.
Constraints
policyis"FCFS","SJF"or"RR".1 <= n == arrival.length == burst.length <= 5000 <= arrival[i] <= 10^5,1 <= burst[i] <= 1000, and1 <= quantum <= 1000
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Ad Score Scheduler With DelayGoogle · ONSITE INTERVIEWGoogle interview questions
- Durable Work Queue OperationsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Dynamic Batch Decode TracexAI · ONSITE INTERVIEWxAI interview questions
- Batch API Requests with Retry DelaysDecagon · PHONE SCREENDecagon interview questions
- Combine the Two Smallest Until All Reach kAdobe · PHONE SCREENAdobe interview questions
- Limit Order MatchingHeadlands Technologies · PHONE SCREENHeadlands Technologies interview questions
- Named Async Task SchedulerAirwallex · OAAirwallex interview questions
- Price-Time Order MatchingTwo Sigma · PHONE SCREENTwo Sigma interview questions
- Design Authentication ManagerOracle · ONSITE INTERVIEWOracle interview questions
- Delayed Job SchedulerWalmart Labs · ONSITE INTERVIEW
Interview reports that mention this question
Microsoft interview experiences
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
- Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)