ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def merge(a, b):
    out, i, j = [], 0, 0
    while i < len(a) and j < len(b):
        if a[i] <= b[j]:
            out.append(a[i]); i += 1
        else:
            out.append(b[j]); j += 1
    return out + a[i:] + b[j:]
print(merge([1, 4, 9], [2, 3, 10, 11]))
  1. A. [1, 4, 9, 2, 3, 10, 11]
  2. B. [1, 2, 3, 4, 9, 11]
  3. C. [1, 2, 3, 4, 9, 10, 11]
  4. D. [1, 2, 3, 4, 9]

Correct answer: C. [1, 2, 3, 4, 9, 10, 11]

This is the merge step of merge sort. The left list is drained and then the rest of the right list is appended. The leftover tail of b is 10 and 11.

More Python Programming questions

Exams that ask this topic