Skip to content
Zeni Study
  • Pages
    • Learn Python In 7 Days
      • Day 1 : Variable + if/else
      • Day 2: Loops
      • Day 3 : Array + String
      • Day 4 — Functions
      • Day 5 — Dictionaries - Hashmap
      • Day 6 — Files & Errors
      • Day 7 — Mini Project & Programmer Thinking
    • Machine Coding:
      • Day 1 — Bank account + Wallet :
      • Day 2 : RateLimiter + seat booking
      • Day 3 : Library Management + Classroom Attendance System
      • Day 4 : Bank account and notification system.
      • Day 5 : Key-value and payment processor.
      • Day 6 : Discount engine + Seat booking( in depth)
      • Day 7: Parking lot System :
    • Machine coding extended:
      • Day 7.5 — Parking Lot (Constraint-Heavy Variant)
      • Day 8 : Order + Payment System
      • Day 8 — Extension Round (Mid-Interview Change)
      • Day 9 — Ticket Booking with Expiry (Simulated Time)
      • Day 9 — Extension Round (Seat Lock Ownership + Forced Unlock)
    • SQL + DB Thinking course
      • Day 1 — Tables, Rows & Thinking in Data
      • Day 2 — Relationships & Foreign Keys (Thinking in Connections)
      • Day 3 — Filtering, Aggregation & Answering Data Questions
      • Day 4 — JOINs (Combining Tables the Right Way)
      • Day 5 — Subqueries, EXISTS & NOT EXISTS (Thinking in Layers)
      • Day 6 — Indexes, Constraints & How Databases Think
      • Day 7 — Full SQL Interview Simulation (SDE-1 Ready)
      • Extended Day 1 — Core Business Systems (Deep Practice)
      • Extended Day 2 — Booking & Platform Systems (High-Depth Practice)
      • Extended Day 3 — Content & Learning Platforms (Capstone Practice)
    • LLD: Java
      • DAY 1 — Core Foundations (SDE-2 Level)
      • DAY 2 — State, Time & Domain Correctness
      • icon picker
        DAY 3 — Extensibility & Failure Handling (SDE-2 Core Signal)
      • DAY 4 — Concurrency, Scheduling & Consistency (SDE-2 Critical)
      • DAY 5 — Workflow Design & Config-Driven Systems (SDE-2+)
      • DAY 6 — Eventing & Usage-Based Systems (Advanced SDE-2)
      • DAY 7 — Interview Simulation (Hard, Production-Grade)
      • Hidden Edge Cases Interviewers Expect (Day-wise)
      • Detailed Testing file:
    • Assignment:
      • If else Practice Set

DAY 3 — Extensibility & Failure Handling (SDE-2 Core Signal)

Perfect. ​Day 3 is where you start looking like a strong SDE-2 / early SDE-3 — not just making things work, but making them extendable without rewriting code.
Today’s problems explicitly test:
Open–Closed Principle
Strategy-based design
Failure handling
Clean abstractions
You’ll still code fully, but design quality now matters as much as correctness.

🟠 DAY 3 — Extensibility & Failure Handling (SDE-2 Core Signal)

Concepts Introduced Today (Short + Practical)

1️⃣ Open–Closed Principle (OCP)

Classes should be open for extension, closed for modification.
Bad:
if (type == "FLAT") { ... }
else if (type == "PERCENT") { ... }
Good:
interface DiscountRule {
boolean applicable(Context ctx);
double apply(double price);
}
Interviewers actively watch for this.

2️⃣ Failures Are First-Class Citizens

Production systems:
Fail
Retry
Fallback
Eventually give up
Ignoring failures = ❌ SDE-1 thinking.

✅ Problem 1: Pluggable Discount Engine

📌 Problem Statement

Design a discount engine that applies multiple discount rules to a cart.
Rules:
Each discount decides if it applies
Discounts have priority
Applied in priority order
Easy to add new discounts without changing engine code

Functional Requirements

Input:
Cart total
Cart metadata (userType, coupon, etc.)
Output:
Final discounted price

Discount Rules (Initial)

Flat ₹100 OFF
Applies if cart total ≥ 1000
10% OFF for PREMIUM users
Coupon Discount
₹200 OFF if coupon = "SAVE200"

Example

Cart total = 2000
User = PREMIUM
Coupon = SAVE200

Apply in order:
- Flat 100 → 1900
- 10% → 1710
- Coupon 200 → 1510

🧩 Java Interfaces (DO NOT MODIFY)

public interface DiscountRule {
int getPriority(); // lower number = higher priority
boolean isApplicable(DiscountContext context);
double apply(double currentPrice);
}
public class DiscountContext {
public final double cartTotal;
public final String userType;
public final String coupon;

public DiscountContext(double cartTotal, String userType, String coupon) {
this.cartTotal = cartTotal;
this.userType = userType;
this.coupon = coupon;
}
}
import java.util.List;

public interface DiscountEngine {
double applyDiscounts(double cartTotal, DiscountContext context);
}

🧪 Driver Code (Auto Tests + Edge Cases)

import java.util.*;

public class DiscountEngineTest {

public static void main(String[] args) {
List<DiscountRule> rules = List.of(
new FlatDiscountRule(),
new PremiumUserDiscountRule(),
new CouponDiscountRule()
);

 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.