ProgrammingHardAmazon OA · Infosys SP
What will be the output of the following pseudocode? (Arrays are indexed from 0)
What will be the output of the following pseudocode? (Arrays are indexed from 0)
Integer a[7] = {4, 2, 2, 8, 3, 3, 1}, cnt[0..8] = all 0, out[7]
for (each i from 0 to 6) cnt[a[i]] = cnt[a[i]] + 1
for (each v from 1 to 8) cnt[v] = cnt[v] + cnt[v - 1]
for (each i from 6 down to 0)
cnt[a[i]] = cnt[a[i]] - 1
out[cnt[a[i]]] = a[i]
end for
Print cnt[3], out[2], out[6]- A. 3 2 8
- B. 5 3 8
- C. 4 2 8
- D. 3 2 4
Correct answer: A. 3 2 8
After the prefix sums, cnt[v] is the number of values ≤ v; placing elements from the right decrements cnt, so in the end cnt[3] points at the first 3 (index 3). out = {1, 2, 2, 3, 3, 4, 8}: out[2] = 2, out[6] = 8. Output 3 2 8.
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? (Arrays are indexed from 0) Integer a[8] = {170, 45, 75, 90, 802, 24, 2, 66} List…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer lo = 1, hi = 1000…
- What will be the output of the following pseudocode? (^ is bitwise XOR) Integer x = 0 for (each i from 1 to 10) x = x ^ i end for Integer…
- What will be the output of the following pseudocode? (^ is XOR, & is AND, >> is right shift) Integer x = 21, z = 22 Integer y = x ^ (x >>…
- What will be the output of the following pseudocode? (code(c) is the ASCII code: 'A' = 65, 'a' = 97) Function cmp(String a, String b)…
- What will be the output of the following pseudocode? Integer a[8] = {3, 1, 4, 1, 5, 9, 2, 6} Integer best = a[0], c = 1 for (each i from 1…