ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import bisect
def span(a, x):
    lo = bisect.bisect_left(a, x)
    if lo == len(a) or a[lo] != x:
        return (-1, -1)
    return (lo, bisect.bisect_right(a, x) - 1)
print(span([1, 2, 2, 2, 5], 2), span([1, 2, 2, 2, 5], 3))
  1. A. (1, 4) (-1, -1)
  2. B. (1, 3) (-1, -1)
  3. C. (1, 3) (2, 2)
  4. D. (0, 3) (-1, -1)

Correct answer: B. (1, 3) (-1, -1)

bisect_left finds the first position of x, and bisect_right minus one finds the last. The value 3 is absent, so the function returns (-1, -1).

More Python Programming questions

Exams that ask this topic