Skip to content

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
 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.