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 :
      • icon picker
        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
      • 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 : RateLimiter + seat booking

🟢 Day 2 — Encapsulation & Controlled State Changes

🎯 Day 2 Objective

By the end of Day 2, the candidate should be able to:
Control how state is modified
Enforce rules and validations inside methods
Prevent invalid operations
Implement logic exactly as per requirements
Handle state transitions correctly
Today is about protecting state, not just storing it.

1️⃣ Teaching Script — What to Say

Start with This Reality

“In machine coding rounds, most bugs are not syntax bugs. They are logic bugs caused by invalid state changes.”
Examples:
Negative balance
Booking twice
Unlocking something already unlocked
Using invalid input
Your job is to stop invalid state transitions.

2️⃣ Core Idea of Day 2

Encapsulation = state can change only through valid methods.
No direct access
No uncontrolled mutation
All rules live inside methods

3️⃣ Small Code to Understand (Very Important)

Example — Door Lock

class Door:
def __init__(self):
self.is_locked = True

def unlock(self):
if not self.is_locked:
return False
self.is_locked = False
return True

def lock(self):
if self.is_locked:
return False
self.is_locked = True
return True

Key Observations

State is boolean
Invalid operations return False
State changes only when allowed
🧠 This pattern appears everywhere in machine coding.

4️⃣ Small Mandatory Practice (Warm-up)

Task

Implement this class:
Class: LightSwitch

Methods:
- turn_on() → bool
- turn_off() → bool
- is_on() → bool
Rules:
Turning ON when already ON → False
Turning OFF when already OFF → False
👉 This warms up state validation thinking.

5️⃣ Machine Coding Rules (Reinforced)

For all problems today:
Do NOT add new methods
Do NOT change signatures
Do NOT expose state directly
All validations must be inside methods
Return True / False — no prints

6️⃣ Main Problem 1 — Seat Reservation System

Problem Statement

You are given a class Seat.
A seat can be reserved or free.
Only valid transitions are allowed.

Class Definition (DO NOT MODIFY)

class Seat:
def __init__(self, seat_number: str):
pass

def reserve(self) -> bool:
pass

def cancel(self) -> bool:
pass

def is_reserved(self) -> bool:
pass

Requirements (Strict)

Initial State
Seat is not reserved
reserve()
If already reserved → return False
Otherwise:
Mark as reserved
Return True
cancel()
If not reserved → return False
Otherwise:
Mark as not reserved
Return True
is_reserved()
Return current reservation status

Example

seat = Seat("A1")

seat.reserve() # True
seat.reserve() # False
seat.cancel() # True
seat.cancel() # False

What This Tests

Boolean state control
Correct transitions
No illegal state changes

7️⃣ Main Problem 2 — Rate Limiter (Counter-Based)

Problem Statement

Implement a simple request limiter.
Each instance controls its own limit.

Class Definition (DO NOT MODIFY)

class RateLimiter:
def __init__(self, limit: int):
pass

def allow_request(self) -> bool:
pass

def get_remaining(self) -> int:
pass

Requirements

__init__(limit)
limit is maximum allowed requests
Initial used requests = 0
allow_request()
If used requests < limit:
Increment used requests
Return True
Otherwise:
Return False
get_remaining()
Return number of remaining allowed requests

Example

rl = RateLimiter(3)

rl.allow_request() # True
rl.allow_request() # True
rl.allow_request() # True
rl.allow_request() # False
rl.get_remaining() # 0

Hidden Edge Case

Each RateLimiter object must be independent

8️⃣ No-Guess Hints (Read Only If Stuck)

Use self.is_reserved
Use self.used_requests
Initialize state in __init__
Never reset state unless required
Think in state transitions

9️⃣ Evaluation Checklist

Solution is correct if:
✅ State initialized correctly
✅ Invalid operations blocked
✅ Methods return correct boolean
✅ State preserved across calls
✅ No shared/global state
✅ Multiple objects behave independently

🔟 Reflection (Candidate Must Answer)

Answer in words:
Why should state not be modified directly?
What breaks if reserve() always returns True?
How does RateLimiter prevent overuse?
What is an invalid state transition?

⭐ Day 2 Golden Rule

If state changes without rules, your program will fail in interviews.

🧪 Day 2 — Test File

📁 Folder Structure

day2/
├── solution.py
└── test_day2.py

📄 (Given to Candidate)

class Seat:
def __init__(self, seat_number: str):
pass
 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.