🟢 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:
Unlocking something already unlocked Your job is to stop invalid state transitions.
2️⃣ Core Idea of Day 2
Encapsulation = state can change only through valid methods.
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
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 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
reserve()
If already reserved → return False cancel()
If not reserved → return False 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
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: 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)
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 ✅ 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