Skip to content

ORIGINAL CODING QUESTION

Count merged booking blocks

Room bookings are closed intervals [start, end] given in any order. Bookings that overlap, or that merely share an endpoint, join into one block, and blocks keep chaining as long as bookings connect. Bookings like [1,2] and [3,4] do not touch, so they stay separate. Return the number of blocks.

Open practice workspace

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

Input and output

n, then n pairs (start,end).

Constraints

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

Example 1

Input

4 1 3 8 10 2 6 15 18

Output

3

Blocks are [1,6], [8,10] and [15,18].

Example 2

Input

3 1 2 2 3 4 5

Output

2

[1,2] and [2,3] share day 2; [4,5] stands alone.

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

Unsorted input hides which bookings connect.

Hint 2

Sort by start and keep the furthest end of the current block.

Hint 3

A booking starting after that end opens a new block.

Practice this question

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

Open practice workspaceBrowse the question library →