OA. free
Free
Qualcomm Core Computer Science Core Computer Science Medium

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:

  1. Set i = 0, j = 0
  2. Repeat Step 3 to 8 till __________
  3. If A[i] < B[j] then
  4. Print A[i++]
  5. Else if B[j] < A[i] then
  6. Print B[j++]
  7. Else
  8. Print B[j++] and i++
  9. Repeat step 10 till __________
  10. Print A[i++]
  11. Repeat step 12 till __________
  12. Print B[j++]
  13. Stop

**MCQ

Choose one option.
Show answer & explanation
Answer: C. Blank 1: i < m || j < n; Blank 2: i < m; Blank 3: j < n

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.