ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[7] = {10, 5, 3, 4, 3, 5, 6}
Set seen = empty
Integer first = -1
for (each i from 6 down to 0)
if (seen contains a[i])
first = a[i]
else
add a[i] to seen
end if
end for
Print first- A. 3
- B. 10
- C. 5
- D. -1
Correct answer: C. 5
Scanning from the right, first is overwritten every time a value already seen to the right turns up, so it ends with the repeating element whose FIRST occurrence is leftmost: 5 (index 1). 3 repeats sooner (at index 4) but first occurs later, at index 2.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What is the time complexity of the following pseudocode, as a function of n? for (each i from 1 to n) for (each j from 1 to n) if (i == j)…
- Strings are immutable here, so s = s + "a" copies the whole current string. What is the time complexity of the following pseudocode, as a…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Boolean prime[2..50] //…
- What will be the output of the following pseudocode? Integer count = 0, n = 2, s = 0 while (count < 10) Boolean pr = true for (each d from…
- What will be the output of the following pseudocode? (XOR is true when exactly one side is true; NOT binds tighter than AND, which binds…
- What will be the output of the following pseudocode? Integer a[6] = {3, 8, 1, 9, 4, 7} Integer best = -1, i = 0, j = 5 while (i < j) best…