~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
Rolling windows and rate counters
QPS over the last N seconds, hit counters, rate limiters.
Notes
Recognise it when: you need "requests in the last N seconds", QPS per key, hit counters, or rate limiting.
- Exact, low volume: a deque of timestamps; pop from the left while
t - q[0] >= window. - High volume: time buckets: an array of N one-second buckets
(second, count)in a ring. On each hit, reset the bucket if its second is stale. The rolling sum is the sum of the fresh buckets (keep a running total to make it O(1)). - Per key:
dict[key] -> bucket ring. Evict idle keys. - Rate limiter: a token bucket (refill
rate * elapsed, cap at burst) or a sliding-window log or counter. - Thread safety: a lock per key or striped locks;
collections.Counterisn't atomic.
Gotchas: clock monotonicity (time.monotonic), window boundaries (≤ vs <), and memory growth from many keys.
7 problems
Practical systems
Streams & durability Rate counters, serialization, retrying work queues.
Rolling windows and rate counters
- Basics: count hits in the last N seconds basics py · c++ · java easy
- Café drink counts over the last N seconds py · c++ · java easy
- Key-value store with rolling QPS 3 levels Databricks medium
- OA: Chat message events aggregation 3 levels OpenAI py · c++ · java easy
- OA: Throttle requests per client 4 levels py · c++ · java hard
- Page views in the last five minutes py · c++ · java easy
- Logger Rate Limiter py · c++ · java easy