Skip to content

ORIGINAL CODING QUESTION

Total booked calendar time

A shared calendar holds n bookings as half-open time ranges [start, end). Bookings may overlap, touch or be empty (start equals end). Return the total amount of time covered by at least one booking, counting overlapping time only once.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n, then n pairs (start, end). Print the covered length.

Constraints

1 ≤ n ≤ 200; -1000 ≤ start ≤ end ≤ 1000.

Example 1

Input

3
1 4
2 6
8 10

Output

7

[1,4) and [2,6) merge into [1,6), length 5, plus [8,10), length 2.

Example 2

Input

2
0 5
5 9

Output

9

Touching bookings cover 9 units with no double counting.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Overlaps are easiest to handle when bookings are processed in time order.

Hint 2

Sort by start and keep one current merged block.

Hint 3

If the next booking starts at or before the current block end, extend the end; otherwise add the block length to the answer and start a new block. Add the final block at the end.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →