ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (stop ends the program; arrays are indexed from 0)
What will be the output of the following pseudocode? (stop ends the program; arrays are indexed from 0)
Integer a[6] = {3, 4, 5, 6, 1, 8}, target = 9
Map seen = empty // value -> index
for (each j from 0 to 5)
if (seen contains (target - a[j]))
Print seen[target - a[j]], j
stop
end if
seen[a[j]] = j
end for- A. 0 3
- B. 4 5
- C. 3 0
- D. 1 2
Correct answer: D. 1 2
The map remembers values already passed. j = 2 (value 5) is the first index whose partner 4 has been seen (at index 1), so 1 2 is printed. A double loop over i first would find 3 + 6 at 0 3 instead.
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? (mod keeps the sign of the dividend (as in C); arrays are indexed from 0) Integer…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer calls = 0 Procedure…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[5] = {1, 2, 3, 4, 5}, comps = 0 Procedure…
- What will be the output of the following pseudocode? (mod keeps the sign of the dividend; code/char convert between letters and codes)…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer x = 1221, rev = 0…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer fact[10], count = 0…