ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (All variables are integers, so / is integer division; arrays are indexed from 0)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division; arrays are indexed from 0)
Integer a[6] = {1, 3, 3, 3, 5, 7}, x = 3
Integer lo = 0, hi = 6, mid
while (lo < hi)
mid = (lo + hi) / 2
if (a[mid] < x) lo = mid + 1 else hi = mid
end while
Integer first = lo
lo = 0
hi = 6
while (lo < hi)
mid = (lo + hi) / 2
if (a[mid] <= x) lo = mid + 1 else hi = mid
end while
Print first, lo, lo - first- A. 1 3 2
- B. 1 4 3
- C. 2 4 2
- D. 0 4 4
Correct answer: B. 1 4 3
The first search finds the first index with a value ≥ 3 (lower bound): 1. The second finds the first index with a value > 3 (upper bound): 4. Their difference is the number of 3's: 1 4 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? Integer c = 0 for (each i from 1 to 10) for (each j from 1 to i) if (j j > i) break…
- What will be the output of the following pseudocode? Integer m[3][3] = {{5, 1, 7}, {2, 9, 3}, {8, 4, 6}} Integer c = 0 Boolean peak for…
- What will be the output of the following pseudocode? String s = "CODE IS FUN", r = "", w = "" for (each i from 0 to length(s) - 1) if…
- What will be the output of the following pseudocode? (substring(t, i, k) is the k characters starting at index i; indices start at 0)…
- What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift) Integer a = 13, b = 11, carry, rounds = 0…
- What will be the output of the following pseudocode? ((b & 1) is evaluated before == ; & is AND, << and >> are shifts) Integer a = 13, b =…