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)
      • icon picker
        DAY 2 — State, Time & Domain Correctness
      • 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 2 — State, Time & Domain Correctness

Today you’ll deal with state transitions, invariants, idempotency, and correctness under time — exactly what SDE-2 interviewers push on.
You will again only implement concrete classes. 👉 Interfaces + driver code are fixed.

🟡 DAY 2 — State, Time & Domain Correctness (SDE-2)

Concepts Introduced Today (Short & Practical)

1️⃣ Explicit State Machines

Real systems are not CRUD. They move through valid states only.
Example:
HELD → CONFIRMED
HELD → EXPIRED
CONFIRMED → (terminal)
Invalid transitions must be rejected.

2️⃣ Domain Invariants

Rules that must never break, e.g.:
Wallet balance ≥ 0
Same seat cannot be booked twice
Transaction history is immutable
Interviewers love invariant thinking.

✅ Problem 1: Seat Hold & Reservation System

(BookMyShow-style, simplified but interview-real)

📌 Problem Statement

Design a seat reservation system where:
Seats can be held temporarily
Held seats must be confirmed within a timeout
Expired holds automatically release seats

Functional Requirements

holdSeat(seatId, userId) → returns holdId
confirmSeat(holdId) → books seat permanently
A seat:
cannot be double-held
cannot be confirmed after expiry

Rules

Hold timeout = 3 seconds
Only the same user can confirm their hold
Expired holds behave as if they never existed

Example Flow

holdSeat("A1", "user1") → hold123
confirmSeat(hold123) → SUCCESS

holdSeat("A2", "user2") → hold456
(wait 4 sec)
confirmSeat(hold456) → FAIL (expired)

🧩 Java Interfaces (DO NOT MODIFY)

public interface SeatReservationSystem {
String holdSeat(String seatId, String userId);
boolean confirmSeat(String holdId);
}

🧪 Driver Code (Auto Tests + Edge Cases)

public class SeatReservationTest {

public static void main(String[] args) throws Exception {
SeatReservationSystem system = new InMemorySeatReservationSystem(3000);

String holdId = system.holdSeat("A1", "user1");
assert holdId != null;
assert system.confirmSeat(holdId);

// Cannot double book
assert system.holdSeat("A1", "user2") == null;

// Expiry test
String hold2 = system.holdSeat("A2", "user2");
Thread.sleep(3100);
assert !system.confirmSeat(hold2);

// Seat should be available again
String hold3 = system.holdSeat("A2", "user3");
assert hold3 != null;

System.out.println("✅ SeatReservation tests passed");
}
}

🎯 What Interviewer Evaluates

State transitions
Expiry handling
Defensive programming
Clear modeling (Seat vs Hold vs Booking)

✅ Problem 2: Wallet & Transaction Ledger

(Extremely common in fintech interviews)

📌 Problem Statement

Design a wallet system that supports:
Credit
Debit
Immutable transaction history

Functional Requirements

Wallet starts with 0 balance
credit(amount)
debit(amount) → fails if insufficient balance
getBalance()
getTransactions() (read-only)

Transaction Rules

Every successful operation creates a transaction
Failed debits do not create transactions
Transaction history cannot be modified

Example

credit(100)
debit(40)
debit(100) → FAIL

balance → 60
transactions → [CREDIT 100, DEBIT 40]

🧩 Java Interfaces (DO NOT MODIFY)

import java.util.List;

public interface Wallet {
void credit(long amount);
boolean debit(long amount);
long getBalance();
List<Transaction> getTransactions();
}

public class Transaction {
public final String type;
public final long amount;
public final long timestamp;

public Transaction(String type, long amount, long timestamp) {
this.type = type;
this.amount = amount;
this.timestamp = timestamp;
}
}

🧪 Driver Code (Auto Tests + Edge Cases)

import java.util.List;

public class WalletTest {

public static void main(String[] args) {
Wallet wallet = new SimpleWallet();

wallet.credit(100);
assert wallet.getBalance() == 100;

assert wallet.debit(40);
assert wallet.getBalance() == 60;

assert !wallet.debit(100);
assert wallet.getBalance() == 60;

List<Transaction> txns = wallet.getTransactions();
assert txns.size() == 2;

assert "CREDIT".equals(txns.get(0).type);
assert "DEBIT".equals(txns.get(1).type);

// Ensure immutability
try {
txns.add(new Transaction("CREDIT", 100, System.currentTimeMillis()));
assert false;
} catch (Exception e) {
// expected
}

System.out.println("✅ Wallet tests passed");
}
}

🎯 What Interviewer Evaluates

Domain correctness
Invariant protection
Immutability
API cleanliness

🧠 Day 2 Takeaways

By now, you should:
Think in states, not just methods
Protect invariants aggressively
Design for correctness before optimization
This is exactly where average SDE-2 candidates start failing.

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