Skip to content

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;

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