QUESTION 51 What will be come in place of blank in the algorithm given below to get the...
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
QUESTION 51
What will be come in place of blank in the algorithm given below to get the desired output?
Input: A = {3, 6, 9, 10, 36} and B = {1, 8, 29}, m = 5, n = 3
Output: 1 3 6 8 9 10 29 36
Algorithm:
- Set i = 0, j = 0
- Repeat Step 3 to 8 till __________
- If A[i] < B[j] then
- Print A[i++]
- Else if B[j] < A[i] then
- Print B[j++]
- Else
- Print B[j++] and i++
- Repeat step 10 till __________
- Print A[i++]
- Repeat step 12 till __________
- Print B[j++]
- Stop
**MCQ
Show answer & explanation
This is a merge algorithm for two sorted arrays. The first blank needs OR (||) because we continue the main loop as long as either array has remaining elements—the inner comparisons (lines 3-8) only work when both i and j are valid. After exhausting one array, we print remaining elements from the other: Blank 2 prints remaining A elements (while i < m), and Blank 3 prints remaining B elements (while j < n).
Step-by-step Derivation:
Trace through with A = {3, 6, 9, 10, 36}, B = {1, 8, 29}, m = 5, n = 3:
- i=0, j=0: B[0]=1 < A[0]=3 → print 1, j=1
- i=0, j=1: A[0]=3 < B[1]=8 → print 3, i=1
- i=1, j=1: A[1]=6 < B[1]=8 → print 6, i=2
- i=2, j=1: A[2]=9 > B[1]=8 → print 8, j=2
- i=2, j=2: A[2]=9 < B[2]=29 → print 9, i=3
- i=3, j=2: A[3]=10 < B[2]=29 → print 10, i=4
- i=4, j=2: A[4]=36 > B[2]=29 → print 29, j=3
- Now j=3 (equals n), so main loop exits (i < m && j < n fails)
- Print remaining A: 36
- B is exhausted, nothing remains
Output: 1 3 6 8 9 10 29 36 ✓
Option D fails because Blank 1 uses AND (&&), causing the loop to exit as soon as either array is exhausted, leaving unmerged elements.