Skip to content

DAY 7 — Interview Simulation (Hard, Production-Grade)

This is it. ​Day 7 is a full interview-simulation day — these problems are hard on purpose and are extremely close to what strong companies ask for final machine-coding / depth rounds.
If you can do Day 7 well, you are solid SDE-2 and competitive for SDE-3.

⚫ DAY 7 — Interview Simulation (Hard, Production-Grade)

Today tests:
Rule engines
Rolling time windows
Idempotency
Failure handling
Backoff + DLQ
Clear abstractions under pressure

✅ Problem 1: Payment Rule Engine (Rolling Window + Rule Chaining)

📌 Problem Statement

Design a payment rule engine to validate transactions before execution.
This system enforces multiple rules:
KYC-based limits
Rolling time-window limits
Extensible rule chaining

Functional Requirements

validate(transaction) → allow / reject
Multiple rules apply
All rules must pass
Rules are independent and pluggable

Rules to Implement (Initial)

Rule 1️⃣: KYC Limit Rule

If user is NOT_KYC
Max ₹20,000 per transaction

Rule 2️⃣: Rolling 24-hour Limit

Total amount sent by user in last 24 hours ≤ ₹100,000

Example

User: U1 (NOT_KYC)
Transaction: ₹25,000 → ❌ rejected

User: U2 (KYC)
Last 24h total: ₹90,000
New txn: ₹15,000 → ❌ rejected

🧩 Java Interfaces (DO NOT MODIFY)

public enum KycStatus {
KYC,
NOT_KYC
}
public class Transaction {
public final String userId;
public final long amount;
public final long timestamp;
public final KycStatus kycStatus;

public Transaction(String userId, long amount, long timestamp, KycStatus kycStatus) {
this.userId = userId;
this.amount = amount;
this.timestamp = timestamp;
this.kycStatus = kycStatus;
}
}
public interface Rule {
boolean validate(Transaction txn);
}
import java.util.List;

public interface PaymentRuleEngine {
boolean validate(Transaction txn);
}

🧪 Driver Code (Auto Tests + Rolling Window)

public class PaymentRuleEngineTest {

public static void main(String[] args) throws Exception {
PaymentRuleEngine engine = new DefaultPaymentRuleEngine(
List.of(
new KycLimitRule(),
new RollingWindowLimitRule(100_000, 24 * 60 * 60 * 1000)
)
);

long now = System.currentTimeMillis();

Transaction t1 = new Transaction("U1", 25_000, now, KycStatus.NOT_KYC);
assert !engine.validate(t1);

Transaction t2 = new Transaction("U2", 60_000, now, KycStatus.KYC);
Transaction t3 = new Transaction("U2", 50_000, now + 1000, KycStatus.KYC);

assert engine.validate(t2);
assert !engine.validate(t3);

Thread.sleep(100);
Transaction t4 = new Transaction("U2", 40_000, now + 2000, KycStatus.KYC);
assert engine.validate(t4);

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

🎯 What Interviewer Evaluates

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