A method for LLD rounds
40 min · Advanced
Need help? Ask a tutor on WhatsApp (opens in a new tab)Someone says "design a parking lot" and gives you 45 minutes. Most people freeze or start coding at once. A fixed method fixes that, and ShopLite's orders and payments are the practice ground here.
Clarify requirements and scope
Clarifying requirements means asking short questions until you and the interviewer agree what the system must do, and what it does not need to do.
The question is vague on purpose. Here the interviewer says "design the order flow for ShopLite", and the agreed list ends up like this.
Problem: "Design the order flow for ShopLite."Questions Agreed answerWho places orders? Logged-in customers onlyHow do they pay? UPI or card for now; more laterWho is told about the order? Customer by SMS and emailStock and delivery? Out of scope for this roundConcurrency? One order at a time is fineIn scope: place order, pay, mark paid or failed, notifyOut: login, stock, delivery, refunds- Ask about users and their main actions first: who does what.
- Ask about variations: payment types, vehicle types, seat types. These become interfaces later.
- Ask about scale and concurrency only after the core is clear.
- Repeat the final list back in one breath before you design anything.
The list has functional requirements (what the system does, such as "place an order") and non-functional requirements (how well it does it, such as "two users must not buy the last item twice"). Mark what is out of scope, so you do not spend ten minutes on a login screen nobody asked for.
In the ShopLite story, this is how you design the order flow cleanly before the code grows messy.
Order of execution
Put the method in order
Drag the steps into the order you would use in the round.
Entities, fields and responsibilities
An entity is a thing the system must remember, such as Order, Product or Customer. Each entity has fields and one job.
The example turns the nouns of an order system into small classes, each with one job.
Example 1 · Entities with one job each
import java.util.ArrayList;import java.util.List;
record Customer(String id, String name, String phone) { }
record Product(String sku, String name, int priceRupees) { }
record OrderLine(Product product, int quantity) { int lineTotal() { return product.priceRupees() * quantity; }}
class Order { private final String id; private final Customer customer; private final List<OrderLine> lines = new ArrayList<>();
Order(String id, Customer customer) { this.id = id; this.customer = customer; }
void addLine(Product product, int quantity) { lines.add(new OrderLine(product, quantity)); }
int total() { int sum = 0; for (OrderLine line : lines) { sum += line.lineTotal(); } return sum; }
String id() { return id; } Customer customer() { return customer; } List<OrderLine> lines() { return List.copyOf(lines); }}
public class EntitiesDemo { public static void main(String[] args) { Customer asha = new Customer("C1", "Asha", "98450 11111"); Order order = new Order("ORD-101", asha); order.addLine(new Product("BK-1", "Java notebook", 120), 3); order.addLine(new Product("PN-2", "Gel pen pack", 50), 2);
for (OrderLine line : order.lines()) { System.out.println(line.product().name() + " x" + line.quantity() + " = Rs " + line.lineTotal()); } System.out.println(order.id() + " for " + order.customer().name() + ": Rs " + order.total()); }}Java notebook x3 = Rs 360 Gel pen pack x2 = Rs 100 ORD-101 for Asha: Rs 460
What this code does
- Each noun from the requirements became a type:
Customer,Product,OrderLine,Order. Simple value holders are records. Orderowns its lines and its total, and hands out a copy of the list, so no caller can change it from outside.- Prices are whole rupees to keep the demo short; real ShopLite code keeps money in
BigDecimal.
To find entities, underline the nouns in the agreed requirements, then drop the nouns that are only a field of another noun: a price is a field of Product, not a class.
For each entity, write its fields and one line of responsibility: the job it owns. An Order owns its lines and its total; it does not send SMS or talk to a bank. When a class needs the word "and" to describe its job, it is probably two classes.
Relationships and a quick class diagram
A relationship says how two classes are connected: has-a (an Order has many OrderLines), is-a (UpiPayment is a PaymentMethod) or uses (OrderService uses a PaymentMethod but does not own it).
Draw a small text diagram before coding: boxes for classes, arrows for relationships, and a count on each side.
Customer 1 ---- * Order 1 ---- * OrderLine * ---- 1 Product |OrderService ---uses---> PaymentMethod <|-- UpiPayment, CardPaymentOrderService ---uses---> NotificationService <|-- SmsNotifier, EmailNotifierhas-a: Order -> OrderLine (an order owns its lines)is-a: UpiPayment -> PaymentMethod (implements)uses: OrderService -> PaymentMethod (passed in, not created)Two minutes on this diagram saves you from moving fields between classes halfway through the code.
Patterns only where they solve a stated need
A design pattern is a known shape of classes for a common problem, such as Strategy or Observer. In an interview, add one only when a requirement asks for it.
The example adds exactly two patterns, each one named after the requirement that asked for it.
Example 2 · Strategy and Observer where the requirements ask
import java.util.List;
interface PaymentMethod { boolean pay(String orderId, int amountRupees); String name();}
class UpiPayment implements PaymentMethod { private final String upiId; UpiPayment(String upiId) { this.upiId = upiId; } public boolean pay(String orderId, int amountRupees) { System.out.println("UPI collect request to " + upiId + " for Rs " + amountRupees); return true; } public String name() { return "UPI"; }}
class CardPayment implements PaymentMethod { private final String last4; CardPayment(String last4) { this.last4 = last4; } public boolean pay(String orderId, int amountRupees) { System.out.println("Card ending " + last4 + " charged Rs " + amountRupees); return true; } public String name() { return "Card"; }}
interface NotificationService { void orderPaid(String orderId, int amountRupees);}
class SmsNotifier implements NotificationService { public void orderPaid(String orderId, int amountRupees) { System.out.println("SMS: " + orderId + " paid, Rs " + amountRupees); }}
class EmailNotifier implements NotificationService { public void orderPaid(String orderId, int amountRupees) { System.out.println("Email: receipt for " + orderId); }}
public class PatternsByNeedDemo { public static void main(String[] args) { List<NotificationService> listeners = List.of(new SmsNotifier(), new EmailNotifier()); List<PaymentMethod> methods = List.of(new UpiPayment("asha@okbank"), new CardPayment("4242"));
int i = 1; for (PaymentMethod method : methods) { String orderId = "ORD-10" + i++; if (method.pay(orderId, 460)) { listeners.forEach(l -> l.orderPaid(orderId, 460)); } } }}UPI collect request to asha@okbank for Rs 460 SMS: ORD-101 paid, Rs 460 Email: receipt for ORD-101 Card ending 4242 charged Rs 460 SMS: ORD-102 paid, Rs 460 Email: receipt for ORD-102
What this code does
- "Payment types will grow" became the
PaymentMethodinterface (Strategy): the loop callspaywithout knowing which type it has. - "Tell the customer by SMS and email" became a list of
NotificationServicelisteners (Observer): adding WhatsApp later is one more class in the list. - Nothing else got a pattern, because no requirement asked for more flexibility.
Say the requirement out loud when you add a pattern: "Payment types will grow, so PaymentMethod is an interface: that is the Strategy pattern." If no requirement asks for flexibility, a plain class is the better answer.
The usual matches:
- Many interchangeable rules (payment, pricing, spot choice): Strategy
- Many parties to tell about an event: Observer
- Behaviour that changes with the current status: State
- An object with many optional fields: Builder
Find the bug
Spot the design smell
Click the line that makes the design hard to extend.
A new payment type must be added next month.
Round 1 of 2Core flow first, then extensions, with a time plan
The core flow is the one happy path that proves your design works, such as place an order, pay, mark it paid and notify. Code it end to end before adding anything else.
The first example codes the order core flow; the second adds cash on delivery as one new class.
Example 3 · Core flow first: place, pay, mark, notify
import java.util.List;
enum OrderStatus { CREATED, PAID, PAYMENT_FAILED }
class Order { final String id; final int totalRupees; OrderStatus status = OrderStatus.CREATED; Order(String id, int totalRupees) { this.id = id; this.totalRupees = totalRupees; }}
interface PaymentMethod { boolean pay(Order order); }
interface NotificationService { void orderPaid(Order order); }
class UpiPayment implements PaymentMethod { private final int balance; UpiPayment(int balance) { this.balance = balance; } public boolean pay(Order order) { return balance >= order.totalRupees; }}
class SmsNotifier implements NotificationService { public void orderPaid(Order order) { System.out.println(" SMS: " + order.id + " confirmed"); }}
class OrderService { private final List<NotificationService> notifiers; OrderService(List<NotificationService> notifiers) { this.notifiers = notifiers; }
void placeOrder(Order order, PaymentMethod method) { if (method.pay(order)) { order.status = OrderStatus.PAID; notifiers.forEach(n -> n.orderPaid(order)); } else { order.status = OrderStatus.PAYMENT_FAILED; } System.out.println(order.id + " -> " + order.status); }}
public class CoreFlowDemo { public static void main(String[] args) { OrderService service = new OrderService(List.of(new SmsNotifier())); service.placeOrder(new Order("ORD-201", 899), new UpiPayment(1500)); service.placeOrder(new Order("ORD-202", 2499), new UpiPayment(1500)); }}SMS: ORD-201 confirmed ORD-201 -> PAID ORD-202 -> PAYMENT_FAILED
What this code does
- This is the whole happy path plus its one failure case, and it runs. Everything else in the round builds on it.
OrderServicereceives its notifiers in the constructor instead of creating them, so tests and extensions can pass in different ones.- The second order fails payment, so its status is
PAYMENT_FAILEDand no SMS is sent.
Example 4 · Extension: cash on delivery as a new class
import java.util.List;
enum OrderStatus { CREATED, PAID, PAYMENT_FAILED, PAY_ON_DELIVERY }
class Order { final String id; final int totalRupees; OrderStatus status = OrderStatus.CREATED; Order(String id, int totalRupees) { this.id = id; this.totalRupees = totalRupees; }}
interface PaymentMethod { boolean pay(Order order); default OrderStatus statusAfterSuccess() { return OrderStatus.PAID; }}
interface NotificationService { void orderPaid(Order order); }
class UpiPayment implements PaymentMethod { public boolean pay(Order order) { return true; }}
// New requirement: cash on delivery up to Rs 2000. Only this class is new.class CashOnDelivery implements PaymentMethod { public boolean pay(Order order) { return order.totalRupees <= 2000; } public OrderStatus statusAfterSuccess() { return OrderStatus.PAY_ON_DELIVERY; }}
class SmsNotifier implements NotificationService { public void orderPaid(Order order) { System.out.println(" SMS: " + order.id + " is " + order.status); }}
class OrderService { private final List<NotificationService> notifiers; OrderService(List<NotificationService> notifiers) { this.notifiers = notifiers; }
void placeOrder(Order order, PaymentMethod method) { if (method.pay(order)) { order.status = method.statusAfterSuccess(); notifiers.forEach(n -> n.orderPaid(order)); } else { order.status = OrderStatus.PAYMENT_FAILED; } System.out.println(order.id + " -> " + order.status); }}
public class ExtensionDemo { public static void main(String[] args) { OrderService service = new OrderService(List.of(new SmsNotifier())); service.placeOrder(new Order("ORD-301", 650), new UpiPayment()); service.placeOrder(new Order("ORD-302", 650), new CashOnDelivery()); service.placeOrder(new Order("ORD-303", 3200), new CashOnDelivery()); }}SMS: ORD-301 is PAID ORD-301 -> PAID SMS: ORD-302 is PAY_ON_DELIVERY ORD-302 -> PAY_ON_DELIVERY ORD-303 -> PAYMENT_FAILED
What this code does
- Cash on delivery is a new
PaymentMethodclass.OrderServicechanged by one line (it asks the method which status to set), and noifon payment type was added. - The
defaultmethod keepsUpiPaymentunchanged: old payment classes still meanPAID. - Saying "only one new class" while you code is how you show the design is open for extension.
A working core with two payment types beats a half-written design with six. Add each extension as a new class, not as an edit to old code: that is the moment the interviewer sees your interfaces pay off.
A time plan for 45 minutes:
- About 5 minutes: questions and the agreed list.
- About 5 minutes: entities, fields, responsibilities.
- About 5 minutes: relationships and the text diagram.
- About 20 to 25 minutes: core flow in code, then one or two extensions; keep the last 5 for follow-up questions.
Next step
Check your understanding
11 quick questions