ProgrammingHardAmazon OA · Flipkart coding round

What is printed?

What is printed?
def merge(iv):
    iv.sort()
    out = [iv[0]]
    for s, e in iv[1:]:
        if s <= out[-1][1]:
            out[-1][1] = max(out[-1][1], e)
        else:
            out.append([s, e])
    return out
print(merge([[1, 3], [8, 10], [2, 6], [15, 18], [6, 7]]))
  1. A. [[1, 6], [8, 10], [15, 18]]
  2. B. [[1, 7], [8, 10], [15, 18]]
  3. C. [[1, 3], [2, 6], [8, 10], [15, 18]]
  4. D. [[1, 6], [6, 7], [8, 10], [15, 18]]

Correct answer: B. [[1, 7], [8, 10], [15, 18]]

After sorting, [6, 7] starts exactly where the merged interval ends, and the rule s <= end merges it, so the interval extends to 7. Touching intervals merge under this rule.

More Python Programming questions

Exams that ask this topic