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.
      • icon picker
        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)
      • 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 5 : Key-value and payment processor.

Perfect. Here is Day 5 of the Python OOP – Machine Coding Focus course.
Today’s theme is “Interface-like problems” — extremely common in machine coding rounds.
You are not designing. You are filling logic inside a fixed contract.

🟢 Day 5 — Interface-Like Design (Strict Contracts, Zero Guessing)

🎯 Day 5 Objective

By the end of Day 5, the candidate should be able to:
Implement logic inside predefined method signatures
Respect a fixed contract
Avoid adding extra methods or fields
Write code that passes tests without redesigning structure
Handle behavior-driven requirements precisely
Today is about discipline, not creativity.

1️⃣ Teaching Script — What to Say

Start with Interview Reality

“In many machine coding rounds, the interviewer gives you:
Class names
Method signatures
Sometimes even the file structure And says: ‘Implement this’.”
If you try to:
Rename methods ❌
Add parameters ❌
Redesign classes ❌
You fail.
Your job is to respect the contract and fill logic.

2️⃣ Core Idea of Day 5

An interface is a promise: ‘If you call this method, it will behave like this.’
Even though Python doesn’t force interfaces, machine coding problems do.

3️⃣ Small Code to Understand (Very Important)

Example — Storage Contract

class Storage:
def save(self, key: str, value: str) -> bool:
pass

def load(self, key: str) -> str:
pass
Candidate must:
Implement logic
Not change signature
Not change behavior
🧠 This pattern appears directly in interviews.

4️⃣ Mandatory Warm-up Practice

Task

Implement this exactly:
Class: Logger

Methods:
- log(message: str) -> bool
- get_logs() -> list
Rules:
Empty message → False
Valid message → store it, return True
get_logs returns all messages in order
👉 This warms up contract-based thinking.

5️⃣ Machine Coding Rules (Day 5 Rules)

Do NOT add extra methods
Do NOT change method signatures
Do NOT return extra data
Logic must match requirement exactly
Tests will call methods directly

6️⃣ Main Problem 1 — Key-Value Storage System

Problem Statement

You are implementing a simple key-value storage system.
This mimics interview questions like cache, config store, metadata store.

Class Definition (DO NOT MODIFY)

class KeyValueStore:
def put(self, key: str, value: str) -> bool:
pass

def get(self, key: str) -> str:
pass

def delete(self, key: str) -> bool:
pass

Requirements (Strict)

put(key, value)
If key is empty string → return False
If value is empty string → return False
Otherwise:
Store key-value
Overwrite if key exists
Return True
get(key)
If key does not exist → return empty string ""
Otherwise → return stored value
delete(key)
If key does not exist → return False
Otherwise:
Delete key
Return True

Example

store = KeyValueStore()

store.put("a", "1") # True
store.get("a") # "1"
store.delete("a") # True
store.get("a") # ""

7️⃣ Main Problem 2 — Payment Processor (Behavior Contract)

Problem Statement

You are given a payment processor contract.
Different processors enforce different rules.

Class Definitions (DO NOT MODIFY)

class PaymentProcessor:
def pay(self, amount: int) -> bool:
pass


class CreditCardProcessor(PaymentProcessor):
def pay(self, amount: int) -> bool:
pass


class UPIPaymentProcessor(PaymentProcessor):
def pay(self, amount: int) -> bool:
pass

Requirements

PaymentProcessor
Base pay() always returns False
CreditCardProcessor
If amount <= 0 → False
If amount > 100000 → False
Else → True
UPIPaymentProcessor
If amount <= 0 → False
If amount > 100000 → False
If amount > 20000 → False if KYC is not done
Else → True

Important Constraint

UPIPaymentProcessor constructor takes:
def __init__(self, kyc_done: bool):
Do NOT add any other constructor parameters

8️⃣ No-Guess Hints

Use dictionary for KeyValueStore
Empty string is ""
Base class may intentionally do nothing
Store kyc_done inside object
Return only True / False

9️⃣ Evaluation Checklist

Solution is correct if:
✅ Contract respected
✅ No signature changes
✅ Exact return values
✅ Correct rule enforcement
✅ Independent object behavior
✅ Passes all tests

🔟 Reflection (Candidate Must Answer)

Why shouldn’t we change method signatures?
Why does base class sometimes do nothing?
What happens if behavior differs from contract?
Why is overwrite allowed in key-value store?

⭐ Day 5 Golden Rule

In machine coding, breaking the contract is worse than a bug.

🧪 Day 5 — Test File

📁 Folder Structure

day5/
├── solution.py
└── test_day5.py

📄 (Given to Candidate)

class KeyValueStore:
def put(self, key: str, value: str) -> bool:
pass

def get(self, key: str) -> str:
pass

def delete(self, key: str) -> bool:
pass


class PaymentProcessor:
def pay(self, amount: int) -> bool:
pass


class CreditCardProcessor(PaymentProcessor):
def pay(self, amount: int) -> bool:
pass


class UPIPaymentProcessor(PaymentProcessor):
def __init__(self, kyc_done: bool):
pass

def pay(self, amount: int) -> bool:
pass

📄 test_day5.py (Evaluator File)

from solution import (
KeyValueStore,
CreditCardProcessor,
UPIPaymentProcessor
)


def test_key_value_store():
print("Running KeyValueStore tests...")

store = KeyValueStore()

assert store.put("", "1") is False
assert store.put("a", "") is False
assert store.put("a", "1") is True
assert store.get("a") == "1"

assert store.put("a", "2") is True
assert store.get("a") == "2"

assert store.delete("a") is True
assert store.get("a") == ""
assert store.delete("a") is False

print("✅ KeyValueStore tests passed")


def test_payment_processors():
print("Running PaymentProcessor tests...")

cc = CreditCardProcessor()
upi_no_kyc = UPIPaymentProcessor(False)
upi_kyc = UPIPaymentProcessor(True)

assert cc.pay(0) is False
assert cc.pay(100001) is False
assert cc.pay(50000) is True

assert upi_no_kyc.pay(25000) is False
assert upi_no_kyc.pay(15000) is True

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