Strategy in LLD — Swap algorithms at runtime
Encapsulate algorithms behind one interface. · Swap strategy at runtime (user tier, promo, payment rail).
Watch
Watch, then scroll down for code and practice.
In code
interface FareStrategy {
compute(km: number): number;
}
class EconomyFare implements FareStrategy {
compute(km: number) {
return 8 * km;
}
}
class PremiumFare implements FareStrategy {
compute(km: number) {
return 15 * km;
}
}📘 Key ideas
Problem
Payment gateway needs to support UPI, Card, NetBanking. Ride fare calculation needs Economy vs Premium pricing. Hard-coding these creates brittle switch-cases.
Solution
Define a Strategy interface. Each algorithm is a class. The context accepts a strategy and delegates to it.
Composable
Strategies can be combined. A fare calculator might apply a BaseStrategy + SurgeStrategy + PromoStrategy in sequence.
Relation to OCP
Strategy is the mechanism that makes OCP achievable — new algorithms are new classes, no existing code changes.
🧠 Practice — Apply What You Learned
Parking Lot System
Design a parking lot system that can handle multiple floors, different vehicle types, and …
Library Management System
Design a library system where members can search, borrow, and return books. Librarians can…
ATM Machine
Design an ATM system that handles card authentication, balance inquiry, cash withdrawal, a…
Tic Tac Toe Game
Design a Tic Tac Toe game supporting 2 players (human or AI) on an N×N board.…
Logger / Logging Framework
Design a flexible logging framework that supports multiple log levels, formatters, and out…
Food Delivery System (Swiggy/Zomato)
Design a food delivery platform where customers can browse restaurants, place orders, and …
Chat Application (WhatsApp-like)
Design a messaging system supporting 1-on-1 chats, group chats, message status, and media …
Notification System
Design a notification service that can send alerts via multiple channels based on user pre…
Ride Sharing System (Uber/Ola)
Design a ride sharing platform where riders request rides and drivers accept them.…
LRU Cache System
Design an in-memory cache system with LRU eviction policy, TTL support, and thread safety.…
Elevator System
Design an elevator control system for a building with multiple elevators and floors.…
Payment Gateway
Design a payment processing system that handles transactions across multiple payment metho…
Distributed Job Scheduler
Design a job scheduling system that can queue, execute, and monitor background jobs with r…
Movie Ticket Booking (BookMyShow)
Design a movie ticket booking platform with seat selection, concurrent booking safety, and…
Inventory Management System
Design an inventory system for an e-commerce warehouse with stock tracking, reordering, an…
Hotel Booking System
Design a hotel room booking platform with availability search, pricing, and reservation ma…
Expense Splitter (Splitwise-like)
Design an app that tracks shared expenses and calculates who owes whom in a group.…
Social Media Feed (Twitter/Instagram)
Design a social media platform with posts, follows, and a personalized news feed.…
URL Shortener (bit.ly)
Design a URL shortening service that generates short aliases, handles redirects, and track…
Rate Limiter
Design a rate limiting service that restricts request rates per user/IP using multiple alg…
Event Ticketing System
Design a large-scale event ticketing platform (like Ticketmaster) with high concurrency su…
🚀 Now apply what you learned
Pick a problem above, write your solution, and get AI feedback on your design.
Start Practice →