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.:
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 cannot be confirmed after expiry Rules
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
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:
Immutable transaction history Functional Requirements
Wallet starts with 0 balance debit(amount) → fails if insufficient balance 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
🧠 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.