ProgrammingVery hardCAT · MBA placement

What is printed?

What is printed?
def pick_by_start(iv):
    iv = sorted(iv)
    cnt, end = 0, -10**9
    for s, e in iv:
        if s >= end:
            cnt += 1
            end = e
    return cnt
print(pick_by_start([(1, 10), (2, 3), (4, 5)]))
  1. A. 0
  2. B. 2
  3. C. 3
  4. D. 1

Correct answer: D. 1

Sorting by start takes (1, 10) first, which ends at 10 and blocks both short intervals. Sorting by finish time would take (2, 3) and (4, 5) and give 2.

More Python Programming questions

Exams that ask this topic