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 :
      • 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)
      • icon picker
        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 9 — Ticket Booking with Expiry (Simulated Time)

🟢 Day 9 — Ticket Booking with Expiry (Time-Aware State)

This problem tests whether the candidate can handle ​state + time + rules together — calmly and correctly.

🎯 Day 9 Objective

By the end of Day 9, the candidate should be able to:
Implement time-bound logic without real clocks
Handle expiry-based state transitions
Enforce booking rules strictly
Clean up expired state correctly
Pass interview problems involving locks, TTL, and expiry

⏱ Interview Simulation Rules

Give these rules before coding:
⏰ Time limit: 90 minutes
❌ Do NOT change class names
❌ Do NOT change method signatures
❌ Do NOT modify test file
❌ No print statements
❌ No real time (time.sleep, datetime, etc.)
✅ Use only provided timestamps

🧠 Problem Theme — Ticket Booking with Seat Lock Expiry

You are implementing a ticket booking system.
Each seat:
Can be locked temporarily
Lock expires after a fixed duration
Must be booked before expiry
Automatically becomes free after expiry

🧱 Seat States

Each seat can be in one of the following states:
FREE
LOCKED
BOOKED

🕒 Time Model (IMPORTANT)

Time is represented by integer timestamps
Current time is always passed explicitly
Expiry must be calculated using timestamps
⚠️ No real clocks allowed

🧱 Class Definitions (DO NOT MODIFY)

class Seat:
def __init__(self, seat_id: str, lock_duration: int):
pass

def lock(self, current_time: int) -> bool:
pass

def book(self, current_time: int) -> bool:
pass

def unlock_if_expired(self, current_time: int) -> bool:
pass

def get_state(self) -> str:
pass

📌 Detailed Requirements (NO GUESSING)

🔹 Seat

__init__(seat_id, lock_duration)

Store:
seat_id
lock_duration
Initial state = FREE
No lock time initially

lock(current_time)

Return True if:
Seat is FREE
Then:
Change state to LOCKED
Store lock_time = current_time
Return True
Else return False.

book(current_time)

Return True if:
Seat is LOCKED
Lock has NOT expired
Then:
Change state to BOOKED
Return True
Else return False.

unlock_if_expired(current_time)

If seat is LOCKED and
current_time - lock_time >= lock_duration Then:
Change state to FREE
Clear lock_time
Return True
Else return False.
⚠️ This method must be explicitly called (No automatic expiry)

get_state()

Return current seat state (FREE, LOCKED, BOOKED)

🧠 Expected Thinking Process

Candidate should realize:
Expiry is checked explicitly
Booking must validate current time vs lock time
Expired locks must be cleared manually
Time logic must be precise
No guessing or shortcuts

🧪 (Given to Candidate)

class Seat:
def __init__(self, seat_id: str, lock_duration: int):
pass

def lock(self, current_time: int) -> bool:
pass

def book(self, current_time: int) -> bool:
pass

def unlock_if_expired(self, current_time: int) -> bool:
pass

def get_state(self) -> str:
pass

🧪 test_day9.py (Evaluation File)

from solution import Seat


def test_basic_lock_and_book():
print("Running basic lock & book tests...")

seat = Seat("A1", lock_duration=5)

assert seat.get_state() == "FREE"

assert seat.lock(10) is True
assert seat.get_state() == "LOCKED"

assert seat.book(12) is True
assert seat.get_state() == "BOOKED"

print("✅ Basic lock & book tests passed")


def test_lock_expiry():
print("Running lock expiry tests...")

seat = Seat("A2", lock_duration=5)

assert seat.lock(10) is True
assert seat.get_state() == "LOCKED"

# Time passes beyond expiry
assert seat.unlock_if_expired(16) is True
assert seat.get_state() == "FREE"

print("✅ Lock expiry tests passed")


def test_booking_after_expiry():
print("Running booking after expiry tests...")

seat = Seat("A3", lock_duration=5)

assert seat.lock(10) is True
assert seat.unlock_if_expired(16) is True

assert seat.book(17) is False
assert seat.get_state() == "FREE"

print("✅ Booking after expiry tests passed")


def test_invalid_transitions():
print("Running invalid transition tests...")

seat = Seat("A4", lock_duration=5)

assert seat.book(10) is False
assert seat.unlock_if_expired(10) is False

seat.lock(10)
assert seat.lock(11) is False

seat.book(12)
assert seat.lock(13) is False
assert seat.unlock_if_expired(20) is False

print("✅ Invalid transition tests passed")


if __name__ == "__main__":
test_basic_lock_and_book()
test_lock_expiry()
test_booking_after_expiry()
test_invalid_transitions()

print("\n🎉 ALL DAY 9 TESTS PASSED SUCCESSFULLY")

✅ Pass Criteria

Candidate passes Day 9 if:
All tests pass
No real time used
No signature changes
Correct expiry logic
Correct state transitions
No hidden assumptions

🧠 Reflection (Mandatory)

Candidate must answer:
Why is expiry not automatic?
Why must booking check expiry explicitly?
What breaks if time logic is incorrect?
How is this different from Day 6 seat locking?

⭐ Day 9 Golden Rule

Time is just another input. If you don’t control it, it will control your system.

🎯 What Day 9 Completes

After Day 9, the candidate:
Can handle time-aware systems
Understands TTL & expiry patterns
Is comfortable with lock-based workflows
Is ready for real SDE-1 machine coding rounds

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