ProgrammingVery hardAmazon OA · Goldman Sachs OA
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[5] = {2, 3, 1, 1, 4}
Integer jumps = 0, curEnd = 0, far = 0
for (each i from 0 to 3)
far = max(far, i + a[i])
if (i == curEnd)
jumps = jumps + 1
curEnd = far
end if
end for
Print jumps- A. 3
- B. 4
- C. 1
- D. 2
Correct answer: D. 2
a[i] is the longest jump from index i; the greedy loop counts the fewest jumps to reach the end. From index 0 you can reach index 1 or 2; from index 1 (value 3) you reach the last index: 2 jumps.
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 h[9] = {1, 8, 6, 2, 5, 4, 8, 3, 7} Integer i = 0…
- What will be the output of the following pseudocode? (>> is right shift, & is AND) Integer a[3] = {4, 14, 2}, total = 0 for (each b from 0…
- What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts) Integer x = 1, r = 0 for (each i from 1 to…
- What will be the output of the following pseudocode? Integer m[3][3] = {{2, 0, 1}, {1, 3, 2}, {1, 1, 1}} Integer det = m[0][0] (m[1][1]…
- What will be the output of the following pseudocode? Integer m[4][4] = {{4, 3, 2, -1}, {3, 2, 1, -1}, {1, 1, -1, -2}, {-1, -1, -2, -3}}…
- What will be the output of the following pseudocode? // list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL Integer n = 5, k = 2 Node tail = last node…