~/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.

Streams & durability

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.Counter isn'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

esc