ProgrammingVery hardTCS NQT · Accenture
Jobs each take one unit of time and carry a deadline: 1, 1, 2, 3, 4, 4 (the last slot the job may use; slots are 1, 2, 3, ...). The procedure below schedules as many jobs as possible, using each slot at most once, by taking jobs in deadline order and placing each in the latest free slot at or before its deadline. What is the number of jobs scheduled?
Jobs each take one unit of time and carry a deadline: 1, 1, 2, 3, 4, 4 (the last slot the job may use; slots are 1, 2, 3, ...). The procedure below schedules as many jobs as possible, using each slot at most once, by taking jobs in deadline order and placing each in the latest free slot at or before its deadline. What is the number of jobs scheduled?
sort deadlines ascending
for each deadline d
find the latest free slot t with 1 <= t <= d; if found, occupy it and count + 1
Print count- A. 4
- B. 6
- C. 1
- D. 5
Correct answer: A. 4
Only four slots are useful for these deadlines, so at most four jobs can run, and the greedy reaches that limit. Counting every job ignores the slot limit, stopping at the first conflict gives a smaller number, and adding the tight-deadline count by mistake gives 5.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Events (start, end) are attended one per day, and an event may be attended on any day from start to end inclusive. Events: (1,2), (1,2)…
- Seven tasks with durations 7, 5, 4, 3, 2, 2, 1 are assigned to two identical machines. Tasks are taken in decreasing duration order, and…
- Array a = [2, 5, 3, 5]. For every pair of positions i < j, count the pairs where a[i] is at most a[j] (a[i] <= a[j]). What is the printed…
- Array a = [4, -2, 5, -1, 3]. Exactly k = 2 negations are applied: each one chooses the currently smallest element and replaces it by its…
- Tasks (duration, deadline): (3,5), (2,4), (4,9), (1,3). Earliest deadline first is applied: tasks are sorted by deadline and run back to…
- Two positive integers are a = 252 and b = 105. The procedure below finds their gcd by repeated subtraction. How many times does the loop…