Skip to content

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
 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.