DSA interview question
An operating-systems course simulates a CPU scheduler. n processes all arrive at time 0 with known burst times, and the CPU runs them one at a time, without preemption. A process's waiting time is how long it sits in the queue before starting. Which order minimises the average waiting time, and what is it (rounded down)? Shortest Job First: run the shortest jobs first. Each job's burst delays every job after it, so short jobs should go first.
Example: bursts [4, 3, 7, 1, 2] → 4 (order 1, 2, 3, 4, 7 gives waits 0, 1, 3, 6, 10).
long long averageWaitingTime(vector<int> bt); // floor of the average waiting time
| Input | Output | Explanation |
|---|---|---|
[4, 3, 7, 1, 2] | 4 | Total wait 20, divided by 5 |
[1, 2, 3, 4] | 2 | 10 / 4 = 2.5, rounded down |
[2, 2, 2] | 2 | Waits 0, 2, 4 |
[10, 1] | 0 | Waits 0, 1: average 0.5 |
[5] | 0 |
(Confirm: all processes arrive at time 0; non-preemptive; the result is the floor of the average.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in