ProgrammingVery hardTCS NQT · Wipro pattern

What is printed?

What is printed?
import bisect
a = [1, 3, 3, 5, 7]
bisect.insort(a, 3)
bisect.insort_right(a, 4, lo=0, hi=3)
print(a, bisect.bisect(a, 3, 0, 2))
  1. A. [1, 3, 3, 3, 4, 5, 7] 2
  2. B. [1, 3, 3, 3, 5, 7, 4] 2
  3. C. [1, 3, 3, 4, 3, 5, 7] 2
  4. D. [1, 3, 3, 4, 3, 5, 7] 1

Correct answer: C. [1, 3, 3, 4, 3, 5, 7] 2

insort places 3 after the existing 3s, and insort_right with hi=3 searches only the first three items, so 4 lands at index 3 and the list is no longer sorted. The final bisect searches a[0:2] and returns 2.

More Python Programming questions

Exams that ask this topic