KatInterview

Interactive Time Calendar with Pagination and Range Queries

Data Structure DesignAuto-graded · 18 test cases
HardAuto-gradedData Structure Designtrees-graphsdesign-dsgreedy-intervals

Last updated

Process dynamic interval updates, range queries, and ordered pagination operations efficiently.

Design an in-memory calendar tracking system that manages time intervals defined as half-open ranges [start, end).

Implement the function process_calendar_operations(operations, arguments) which processes a sequential list of command strings and their corresponding argument lists, returning a list containing the result of each operation.

The supported operations and their behaviors are:

  1. add_event(event_id: str, start: int, end: int)

    • Adds or updates an event identified by event_id with time bounds [start, end).
    • If event_id is already stored, its interval is modified to [start, end).
    • Returns None.
    • Note: Events can be added with start >= end (e.g., zero-length or invalid intervals). These events are included in pagination but never match any overlap query.
  2. delete_event(event_id: str)

    • Deletes the event associated with event_id. If event_id does not exist, does nothing.
    • Returns None.
  3. get_overlaps(start: int, end: int)

    • Retrieves all active events whose interval [e_start, e_end) overlaps with the query interval [start, end).
    • Two half-open intervals overlap if max(start, e_start) < min(end, e_end) and e_start < e_end.
    • Returns a list of matching event_id strings sorted primarily by e_start ascending, and secondarily by event_id in standard lexicographical order.
    • If start >= end, returns an empty list [].
  4. get_page(page_number: int, page_size: int)

    • Returns a list of event_id strings corresponding to the requested page.
    • All currently active events are sorted primarily by start (ascending) and secondarily by event_id (lexicographically).
    • Pages are 0-indexed. Page $p$ of size $s$ includes events at 0-based indices from $p \times s$ up to $\min((p + 1) \times s, N)$, where $N$ is the total count of active events.
    • Returns [] if page_number < 0, page_size <= 0, or page_number * page_size >= N.

Constraints

  • $0 \le start, end \le 10^9$
  • Total number of operations across all calls $\le 10^5$
  • event_id is a non-empty string.

Format

  • The function receives operations as List[str] and arguments as List[List[Any]].
  • The returned list has length equal to len(operations), where each entry is None (for add_event / delete_event) or a list of str (for get_overlaps / get_page).

Examples

Example 1

Input: process_calendar_operations(['add_event', 'add_event', 'get_overlaps', 'delete_event', 'get_overlaps'], [['A', 10, 20], ['B', 15, 25], [12, 18], ['A'], [12, 18]])

Output: [None, None, ['A', 'B'], None, ['B']]

Example 2

Input: process_calendar_operations(['add_event', 'add_event', 'add_event', 'get_page', 'get_page'], [['B', 20, 30], ['A', 10, 20], ['C', 10, 15], [0, 2], [1, 2]])

Output: [None, None, None, ['A', 'C'], ['B']]

Example 3

Input: process_calendar_operations(['add_event', 'add_event', 'get_overlaps', 'get_overlaps'], [['A', 0, 10], ['B', 10, 20], [0, 10], [10, 20]])

Output: [None, None, ['A'], ['B']]

Hints

Hint 1

Use a dictionary mapping event_id to (start, end) for O(1) attribute lookups during updates and deletions.

Hint 2

Store events in an augmented self-balancing binary search tree (like a Treap) keyed by (start, event_id).

Hint 3

Augment tree nodes with subtree size (for logarithmic k-th element pagination) and max_end (to prune non-overlapping subtrees).