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)
      • icon picker
        Day 8 : Order + Payment System
      • Day 8 — Extension Round (Mid-Interview Change)
      • 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 8 : Order + Payment System

Below is Day 8 — Order + Payment System, built fully and strictly in the same machine-coding, interview-ready format.
This is harder than Day 7.5, but still SDE-1 safe. No new theory. No design patterns. Just more coordination + rules.

🟢 Day 8 — Order & Payment System (Multi-Class, Rule-Heavy)

This problem appears (in variants) in Amazon, Flipkart, Uber, Meesho interviews.

🎯 Day 8 Objective

By the end of Day 8, the candidate should be able to:
Implement a multi-class business workflow
Coordinate Order + Items + Payment
Enforce state-based restrictions
Prevent invalid transitions
Pass a real backend-style machine coding round
Day 8 tests whether the candidate can build a small backend system, not just classes.

⏱ Interview Simulation Rules

Give these rules before coding:
⏰ Time limit: 90 minutes
❌ Do NOT change class names
❌ Do NOT change method signatures
❌ Do NOT add new public methods
❌ Do NOT modify test file
❌ No print statements
✅ Logic must match requirements exactly

🧠 Problem Theme — Online Order System

You are implementing a basic order system with payment support.
The system supports:
Creating an order
Adding items
Calculating total price
Making payment
Enforcing order state rules

🧱 System Overview (Mental Model)

Order
├── has many OrderItems
├── has total price
├── has payment state
└── controls what actions are allowed

📦 Order States

An order can be in one of these states:
CREATED
PAID
CANCELLED

🚫 Rules Summary (Read Carefully)

Items can be added only when order is CREATED
Payment can happen only once
Order cannot be modified after payment
Cancelled order cannot be paid
Total amount must match payment amount

🧱 Class Definitions (DO NOT MODIFY)

class OrderItem:
def __init__(self, item_id: str, price: int, quantity: int):
pass

def get_total_price(self) -> int:
pass


class Payment:
def __init__(self, amount: int):
pass

def is_successful(self) -> bool:
pass


class Order:
def __init__(self, order_id: str):
pass

def add_item(self, item: OrderItem) -> bool:
pass

def get_total_amount(self) -> int:
pass

def make_payment(self, payment: Payment) -> bool:
pass

def cancel(self) -> bool:
pass

def get_status(self) -> str:
pass

📌 Detailed Requirements (NO ASSUMPTIONS)

🔹 OrderItem

__init__(item_id, price, quantity)

Store:
item_id
price
quantity

get_total_price()

Return price * quantity

🔹 Payment

__init__(amount)

Store payment amount

is_successful()

Always return True
(Actual payment gateway is NOT part of this problem)

🔹 Order

__init__(order_id)

Initialize:
order_id
empty list of items
status = CREATED
no payment attached

add_item(item)

Return True if:
Order status is CREATED
Item quantity > 0
Item price > 0
Else return False.

get_total_amount()

Return sum of get_total_price() for all items
If no items → return 0

make_payment(payment)

Return True if:
Order status is CREATED
No payment has been made before
payment.is_successful() is True
payment.amount == get_total_amount()
Total amount > 0
Then:
Save payment
Change status to PAID
Return True
Else return False.

cancel()

Return True if:
Order status is CREATED
Then:
Change status to CANCELLED
Return True
Else return False.

get_status()

Return current order status

🧠 Expected Candidate Thinking

Candidate should realize:
Order is the central authority
OrderItem is passive data
Payment is validation input
Status controls allowed actions
Total price is derived, not stored manually

🧪 (Given to Candidate)

class OrderItem:
def __init__(self, item_id: str, price: int, quantity: int):
pass

def get_total_price(self) -> int:
pass


class Payment:
def __init__(self, amount: int):
pass

def is_successful(self) -> bool:
pass


class Order:
def __init__(self, order_id: str):
pass

def add_item(self, item: OrderItem) -> bool:
pass

def get_total_amount(self) -> int:
pass

def make_payment(self, payment: Payment) -> bool:
pass

def cancel(self) -> bool:
pass

def get_status(self) -> str:
pass

🧪 test_day8.py (Evaluation File)

from solution import Order, OrderItem, Payment


def test_order_flow():
print("Running basic order flow tests...")

order = Order("O1")

item1 = OrderItem("I1", 100, 2)
item2 = OrderItem("I2", 50, 1)

assert order.get_status() == "CREATED"
assert order.add_item(item1) is True
assert order.add_item(item2) is True

assert order.get_total_amount() == 250

payment = Payment(250)
assert order.make_payment(payment) is True
assert order.get_status() == "PAID"

print("✅ Basic order flow tests passed")


def test_invalid_item_addition():
print("Running invalid item tests...")

order = Order("O2")
bad_item = OrderItem("I3", -10, 2)

assert order.add_item(bad_item) is False
assert order.get_total_amount() == 0

print("✅ Invalid item tests passed")


def test_payment_rules():
print("Running payment rules tests...")

order = Order("O3")
item = OrderItem("I4", 100, 1)
order.add_item(item)

wrong_payment = Payment(50)
assert order.make_payment(wrong_payment) is False

correct_payment = Payment(100)
assert order.make_payment(correct_payment) is True

second_payment = Payment(100)
assert order.make_payment(second_payment) is False

print("✅ Payment rule tests passed")


def test_cancel_rules():
print("Running cancel rules tests...")

order = Order("O4")
item = OrderItem("I5", 200, 1)
order.add_item(item)

assert order.cancel() is True
assert order.get_status() == "CANCELLED"

payment = Payment(200)
assert order.make_payment(payment) is False

print("✅ Cancel rule tests passed")


if __name__ == "__main__":
test_order_flow()
test_invalid_item_addition()
test_payment_rules()
test_cancel_rules()

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

✅ Pass Criteria

Candidate passes Day 8 if:
All tests pass
No signature changes
No prints
Correct state transitions
Correct total calculations
Correct validation logic

🧠 Reflection (Mandatory)

Candidate must answer:
Why is Order the central authority?
Why should items not track order state?
Why is total price derived, not stored?
What breaks if payment happens before item addition?

⭐ Day 8 Golden Rule

Real systems fail not because of logic, but because rules are applied in the wrong order.

🎯 What Day 8 Unlocks

After Day 8, the candidate:
Can build multi-class backend workflows
 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.