Skip to content

Day 6 : Discount engine + Seat booking( in depth)

Today is the most important and hardest day.
This is where most SDE-1 candidates fail machine coding rounds — not because of syntax, but because of complex rules + state transitions.

🟢 Day 6 — Constraint-Heavy Problems & State Transitions

🎯 Day 6 Objective

By the end of Day 6, the candidate should be able to:
Implement multiple rules simultaneously
Handle state transitions correctly
Enforce constraints without missing edge cases
Write logic that survives many method calls
Stay calm under complex requirements
Today is about precision under pressure.

1️⃣ Teaching Script — What to Say

Start with This Interview Reality

“Most machine coding problems are not hard because of logic. They are hard because many small rules interact.”
Common failure reasons:
Forgetting one constraint
Allowing invalid transitions
Resetting state incorrectly
Mixing responsibilities
Your goal is not clever code. Your goal is correct behavior.

2️⃣ Core Idea of Day 6

State + Rules + Time = Complexity
State changes over time
Rules restrict how state can change
Every method call must respect current state
🧠 You must ask: “Is this transition allowed right now?”

3️⃣ Small Code to Understand (Very Important)

Example — Simple Ticket Lock

class Ticket:
def __init__(self):
self.locked = False

def lock(self) -> bool:
if self.locked:
return False
self.locked = True
return True

def unlock(self) -> bool:
if not self.locked:
return False
self.locked = False
return True
Key idea:
Same method behaves differently depending on state
You must always check current state first

4️⃣ Mandatory Warm-up Practice

Task

Implement:
Class: PowerSwitch

Methods:
- turn_on() → bool
- turn_off() → bool
- is_on() → bool
Rules:
Cannot turn ON if already ON
Cannot turn OFF if already OFF
👉 This reinforces state-dependent behavior.

5️⃣ Machine Coding Rules (Day 6 Rules)

Read all constraints before coding
Track state explicitly
Validate before mutating state
Never assume default behavior
Return values strictly as required

6️⃣ Main Problem 1 — Ticket Booking with Seat Locking

Problem Statement

You are implementing a ticket booking system.
Each seat can be:
FREE
LOCKED
BOOKED
Only certain transitions are allowed.

Class Definition (DO NOT MODIFY)

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

def lock(self) -> bool:
pass

def book(self) -> bool:
pass

def unlock(self) -> bool:
pass

def get_state(self) -> str:
pass

Allowed State Transitions

Current State
Action
Result
Return
FREE
lock
LOCKED
True
FREE
book
BOOKED
True
LOCKED
book
BOOKED
True
LOCKED
unlock
FREE
True
BOOKED
any
False
There are no rows in this table
 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.