ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Pair iv[11] = {(1,4), (3,5), (0,6), (5,7), (3,9), (5,9), (6,10), (8,11), (8,12), (2,14), (12,16)}
sort iv by end time (ties keep the given order)
Integer lastEnd = -1, chosen = 0
for (each (s, f) in iv)
if (s >= lastEnd)
chosen = chosen + 1
lastEnd = f
end if
end for
Print chosen, lastEnd- A. 3 16
- B. 5 16
- C. 4 14
- D. 4 16
Correct answer: D. 4 16
Greedy activity selection by earliest finish: (1, 4), then (5, 7), then (8, 11), then (12, 16). chosen = 4 and the last end time is 16.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer arr[6] = {900, 940, 950, 1100, 1500, 1800} Integer dep[6] = {910, 1200, 1120…
- What will be the output of the following pseudocode? (& is AND, >> is right shift) Integer x = 40, pos = 1 while ((x & 1) == 0) x = x >> 1…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 10, steps = 0…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; and / are evaluated left to…
- What will be the output of the following pseudocode? Integer h[6] = {2, 1, 5, 6, 2, 3}, best = 0 for (each i from 0 to 5) Integer mn =…
- What will be the output of the following pseudocode? String stream = "aabcbc" Integer cnt[26] = all 0 Queue q = empty for (each ch in…