Count Integers in Intervals - Problem

Imagine you're a booking system manager for a popular venue. You need to keep track of time slots that are reserved and quickly answer: "How many time units are currently booked?"

Your task is to implement a CountIntervals data structure that efficiently manages overlapping intervals and counts unique integers across all intervals.

The Challenge:

  • add(left, right) - Reserve a new time interval [left, right] (inclusive)
  • count() - Return total unique time units that are reserved

Key Insight: When intervals overlap, they should be merged automatically to avoid double-counting. For example, intervals [1,3] and [2,5] should be treated as [1,5] containing 5 unique integers.

Example: Adding [2,3] and [4,5] gives count=4, then adding [6,7] gives count=6. But if we add [5,6], it merges [4,5] and [6,7] into [4,7], keeping count=6.

Input & Output

example_1.py โ€” Basic Operations
$ Input: CountIntervals() obj.add(2, 3) obj.add(4, 5) obj.count() # Returns 4 obj.add(6, 7) obj.count() # Returns 6
โ€บ Output: 4 6
๐Ÿ’ก Note: After adding [2,3] and [4,5], we have 4 integers: {2,3,4,5}. Adding [6,7] gives us 2 more integers: {6,7}, for a total of 6.
example_2.py โ€” Interval Merging
$ Input: CountIntervals() obj.add(2, 3) obj.add(4, 5) obj.add(6, 7) obj.count() # Returns 6 obj.add(5, 6) # Merges [4,5], [5,6], [6,7] obj.count() # Returns 6 (not 8!)
โ€บ Output: 6 6
๐Ÿ’ก Note: The intervals [4,5], [5,6], and [6,7] all merge into [4,7] containing 4 integers. Combined with [2,3], total is still 6.
example_3.py โ€” Large Merge
$ Input: CountIntervals() obj.add(1, 3) obj.add(5, 8) obj.add(10, 12) obj.count() # Returns 10 obj.add(3, 10) # Big merge! obj.count() # Returns 12
โ€บ Output: 10 12
๐Ÿ’ก Note: Initially we have [1,3], [5,8], [10,12] = 3+4+3 = 10 integers. Adding [3,10] merges everything into [1,12] = 12 integers total.

Constraints

  • 1 โ‰ค left โ‰ค right โ‰ค 109
  • At most 105 calls to add and count
  • Important: Intervals are inclusive on both ends
  • The total count will never exceed 2 ร— 108

Visualization

Tap to expand
๐Ÿจ Hotel Room Booking SystemStep 1: Book rooms [2,4] โ†’ 3 room-nightsRooms:12345678Total: 3 room-nightsStep 2: Book rooms [6,8] โ†’ +3 room-nightsRooms:12345678Total: 6 room-nightsStep 3: Book rooms [4,6] โ†’ Connects the gaps!Rooms:12345678Adding [4,6] overlapswith existing bookingsStep 4: Smart System Merges โ†’ One block [2,8]Rooms:1Merged Block [2,8]Total: 7 room-nights๐ŸŽฏ Key InsightThe smart booking system automatically detects when new reservationsconnect existing ones, avoiding double-counting and providing instant totals!
Understanding the Visualization
1
Add Reservation [2,4]
Guest books rooms 2, 3, 4 for one night = 3 room-nights
2
Add Reservation [6,8]
Another guest books rooms 6, 7, 8 = 3 more room-nights, total = 6
3
Add Reservation [4,6]
New guest books rooms 4, 5, 6 - this connects the previous bookings!
4
Smart Merge
System automatically merges into one block [2,8] = 7 room-nights total
Key Takeaway
๐ŸŽฏ Key Insight: Maintain sorted, non-overlapping intervals with incremental count updates. This gives us O(1) count queries and efficient interval merging for optimal performance.
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 24
67.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen