← Quiz: Interval Problems: all 10 questions
Quiz: Interval Problems · Question 3 of 10
What does this print? (code)
What does this print?
out = []
for s, e in sorted([[5, 7], [1, 4], [2, 3], [6, 8]]):
if out and s <= out[-1][1]:
out[-1][1] = max(out[-1][1], e)
else:
out.append([s, e])
print(out)
- A.[[1, 4], [5, 8]]
- B.[[1, 3], [5, 8]]
- C.[[1, 8]]
- D.[[1, 4], [5, 7], [6, 8]]
Answer & explanation
Correct answer: [[1, 4], [5, 8]]
[2, 3] lies inside [1, 4], so the max keeps the end at 4. [6, 8] extends [5, 7] to [5, 8].
Can you get the rest right without peeking?
Take the timed Quiz: Interval Problems quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →