← Back to Tutorials

11. System Design (Days 146—150)

Rate Limiter

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

Parking Lot System

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

Design Problems Overview

ProblemKey ComponentsPatterns
Library ManagementBooks, members, check-in/out, finesRepository, MVC
Amazon ShoppingProducts, cart, orders, payments, inventoryMicroservices, event-driven
Notification SystemEmail, SMS, push; templates, queueObserver, pub-sub
Uber BackendRider/driver matching, pricing, ETA, trip historyQuadTree, pub-sub, gRPC
Google DriveFile upload/download, sync, sharing, versioningStorage hierarchy, CRDT
TwitterTweets, timeline, follows, trends, searchFan-out, cache (Redis)
YouTubeVideo upload/stream, transcoding, CDN, recommendationsAsync processing, CDN
✏️ Exercise: Design a rate limiter using the sliding window log approach. Draw the architecture diagram. Write pseudocode for the allow_request() method.