Skip to content

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

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