ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (Arrays are indexed from 0; abs gives the absolute value)
What will be the output of the following pseudocode? (Arrays are indexed from 0; abs gives the absolute value)
Integer a[8] = {4, 3, 2, 7, 8, 2, 3, 1}
for (each i from 0 to 7)
Integer v = abs(a[i])
if (a[v - 1] < 0)
Print v
else
a[v - 1] = -a[v - 1]
end if
end for- A. 2 3
- B. 3 2
- C. 2 3 1
- D. 4 7
Correct answer: A. 2 3
Visiting value v flips the sign at index v − 1; finding it already negative means v was seen before. The second 2 is at index 5 and the second 3 at index 6, so the duplicates come out as 2 3.
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[4] = {3, 4, -1, 1}, n = 4 for (each i from 0 to…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[3] = {1, 3, 8}, b[4] = {2, 7, 9, 10}, c[7]…
- What will be the output of the following pseudocode? Integer m[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}} for (each i from 0 to…
- What will be the output of the following pseudocode? (/ is integer division) String tok[9] = {"-", "", "+", "2", "3", "4", "/", "8", "2"}…
- What will be the output of the following pseudocode? Stack s = empty, mins = empty Procedure push2(Integer x) push(s, x) if (mins is empty…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[8] = {1, 3, -1, -3, 5, 3, 6, 7}, k = 3 Deque d…