ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (merging two sorted arrays)? (Arrays are indexed from 0.)
What will be the output of the following pseudocode (merging two sorted arrays)? (Arrays are indexed from 0.)
Integer a[4] = {1, 3, 5, 7}
Integer b[5] = {2, 4, 6, 8, 10}
Integer i = 0, j = 0
List merged = empty list
while (i < length(a) and j < length(b))
if (a[i] < b[j])
append(merged, a[i])
i = i + 1
else
append(merged, b[j])
j = j + 1
end if
end while
append remaining elements of a (from i) and b (from j) to merged
Print merged- A. [1, 2, 3, 4, 5, 6, 7, 8, 10]
- B. [1, 2, 3, 4, 5, 6, 7, 10, 8]
- C. [2, 1, 4, 3, 6, 5, 8, 7, 10]
- D. [1, 3, 5, 7, 2, 4, 6, 8, 10]
Correct answer: A. [1, 2, 3, 4, 5, 6, 7, 8, 10]
This is the standard merge step of merge sort: comparing the fronts of both sorted arrays and taking the smaller each time correctly interleaves them into one fully sorted array, then appends the leftover tail of b once a is exhausted.
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, n = 5 for (each i from 1 to n) for (each j from 1 to 2 i - 1) c = c +…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[6] = {4, 8, 15, 16, 23, 42} Integer target =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function countDigits(Integer…
- What will be the output of the following pseudocode (Pascal's identity for combinations)? Function C(Integer n, Integer r) if (r == 0 or r…
- What will be the output of the following pseudocode (Armstrong number check)? Function isArmstrong(Integer n) String s = toString(n)…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 3) for (each j from 1 to 3) if (j == 2) break end…