Design a rate limiter (e.g., token bucket, sliding window log) to cap API requests per user per second.
# Token Bucket
class TokenBucket:
def __init__(self, capacity, refill_rate):
self.capacity = capacity
self.tokens = capacity
self.refill_rate = refill_rate
self.last_refill = time.time()
def allow_request(self):
now = time.time()
elapsed = now - self.last_refill
self.tokens = min(self.capacity,
self.tokens + elapsed * self.refill_rate)
self.last_refill = now
if self.tokens >= 1:
self.tokens -= 1
return True
return False
Design a multi-level parking lot: multiple floors, spot types (compact/large/motorcycle), ticketing, payment, availability tracking.
# Key classes: ParkingLot, Floor, Spot, Ticket, Vehicle
# Patterns: Singleton for ParkingLot, Factory for Spot types
# Data structures: Map<Floor, List<Spot>>, PriorityQueue for nearest
| Problem | Key Components | Patterns |
|---|---|---|
| Library Management | Books, members, check-in/out, fines | Repository, MVC |
| Amazon Shopping | Products, cart, orders, payments, inventory | Microservices, event-driven |
| Notification System | Email, SMS, push; templates, queue | Observer, pub-sub |
| Uber Backend | Rider/driver matching, pricing, ETA, trip history | QuadTree, pub-sub, gRPC |
| Google Drive | File upload/download, sync, sharing, versioning | Storage hierarchy, CRDT |
| Tweets, timeline, follows, trends, search | Fan-out, cache (Redis) | |
| YouTube | Video upload/stream, transcoding, CDN, recommendations | Async processing, CDN |