KatInterview

Concurrent Data Structures and Calendar Scheduling

Data Structure DesignModel answer
MediumModel answerData Structure Designconcurrencydata-structuresalgorithms

Last updated

Build thread-safe request throttling, search from scratch, frequency-ranked retrieval, and multi-calendar availability finder.

You will implement four independent components that test your ability to design correct, efficient data structures and reason about concurrency, ranking, and calendar logic.

Part 1: Request Throttler

Create a throttling mechanism that accepts or rejects requests based on a sliding time window. Your implementation must:

  • Track requests per client identifier
  • Enforce a maximum number of requests within a rolling time window
  • Support concurrent access from multiple threads safely
  • Work without requiring the caller to supply the current timestamp

Interface:

throttler = RequestThrottle(max_requests=5, window_seconds=60.0)
allowed = throttler.check(client_id)  # returns bool

Part 2: Search Implementation

Write a binary search function without using any standard library search methods. Your function should locate a target value in a sorted array and return its index, or -1 if not found. Handle edge cases including empty arrays, missing values, and duplicates correctly.

Part 3: Frequency-Based Ranking

Design a data structure that tracks item frequencies and efficiently retrieves the most frequent items. Requirements:

  • record(item) – increment the frequency of an item
  • top(k) – return the k most frequent items

Optimize for the case where record is called much more often than top. Use a doubly-linked list internally to achieve constant-time frequency updates.

Part 4: Calendar Availability Finder

Build a system that finds open time slots across multiple people's calendars. Given:

  • A set of calendars, each containing busy intervals as (start, end) pairs
  • A desired meeting duration
  • Working hours boundaries

Find all available slots where everyone is free for at least the requested duration.

Extension: How would you rank meeting rooms when multiple options exist? Consider factors like room capacity, previous usage frequency, and total time already booked.

Hints

Hint 1

For the throttler, think about what state must be updated atomically when checking a request.

Hint 2

Binary search maintains an invariant about where the answer can be. What should happen when the midpoint equals the target?

Hint 3

For top-k retrieval, organize items into buckets by frequency. How can you move an item between adjacent buckets in O(1)?

Hint 4

Calendar merging: sort all busy times, then identify gaps. A single pass can merge overlapping intervals while finding free windows.