Day 1 — Classes, Objects & State (Machine Coding Foundations)
🎯 Day 1 Objective (Say This Clearly)
By the end of Day 1, the candidate should be able to:
Read a problem statement with given class & method signatures Correctly place data inside objects Implement logic inside methods without guessing Maintain and update object state Pass basic machine-coding style test cases Today is about where data lives and how methods change it.
1️⃣ Teaching Script — What to Say (Structured)
Start with This Truth
“In machine coding rounds, you are not judged on elegance.
You are judged on whether your code works exactly as required.”
“Most candidates fail because they don’t know:
What should be a variable Where state should be stored” Today fixes that.
2️⃣ Core Idea of OOP (Only What Is Needed)
One Sentence Definition
A class is a blueprint. An object holds data and behavior together.
Real-World Analogy (Use This)
Think of a bank account:
Balance belongs to one account Deposit affects that account only Withdraw affects that account only That’s an object with state.
3️⃣ Small Code to See & Understand (No Abstraction)
Example 1 — Minimal Class
class Counter:
def __init__(self):
self.value = 0
def increment(self):
self.value += 1
How to Think (IMPORTANT)
It lives inside the object Every object has its own copy c1 = Counter()
c2 = Counter()
c1.increment()
print(c1.value) # 1
print(c2.value) # 0
🧠 Machine coding expects you to reason like this.
4️⃣ Small Practice (Warm-up, Mandatory)
Task (Do This Before Problems)
Implement this class:
Class Name: SimpleCalculator
Methods:
- add(a, b) → returns sum
- subtract(a, b) → returns difference
Rules:
👉 This is just to get comfortable with syntax.
5️⃣ Key Rules for Machine Coding (Day 1 Rules)
Read this carefully. These are non-negotiable.
Do not change method signatures Do not add extra parameters Return values — don’t print Assume tests will call methods directly 6️⃣ Main Problem 1 — Bank Account (State-Based)
Problem Statement
You are given a class BankAccount.
Each BankAccount object represents one user’s bank account.
Class Definition (DO NOT MODIFY)
class BankAccount:
def __init__(self, account_number: str, balance: int):
pass
def deposit(self, amount: int) -> bool:
pass
def withdraw(self, amount: int) -> bool:
pass
def get_balance(self) -> int:
pass
Requirements (Very Strict)
__init__
deposit(amount)
If amount <= 0 → return False withdraw(amount)
If amount <= 0 → return False If amount > balance → return False Deduct amount from balance get_balance()
Example Usage (Tests Will Be Similar)
acc = BankAccount("A123", 1000)
acc.deposit(500) # True
acc.withdraw(200) # True
acc.withdraw(2000) # False
acc.get_balance() # 1300
What This Problem Tests
7️⃣ Main Problem 2 — Digital Wallet (Slightly Trickier)
Problem Statement
You are given a class Wallet.
A wallet starts empty and stores money in integer units.
Class Definition (DO NOT MODIFY)
class Wallet:
def __init__(self):
pass
def add_money(self, amount: int) -> bool:
pass
def spend_money(self, amount: int) -> bool:
pass
def get_money(self) -> int:
pass
Requirements
add_money(amount)
If amount <= 0 → return False spend_money(amount)
If amount <= 0 → return False If amount > balance → return False get_money()
Return current wallet balance Hidden Edge Case (Important)
Multiple method calls will be made Balance must be preserved correctly 8️⃣ No-Guess Hint Section (Read Only If Stuck)
Initialize balance in __init__ Do not use class variables Every object should behave independently Tests will create multiple objects 9️⃣ Evaluation Checklist (Use This to Judge Solution)
A solution is correct if:
✅ All methods return correct values ✅ Balance updates correctly 🔟 Reflection (Candidate Must Answer)
Answer in words:
Why do we use self.balance instead of a normal variable? What happens if two Wallet objects exist? What breaks if balance is global? ⭐ Day 1 Golden Rule
In machine coding,
wrong place for state = guaranteed failure.
🧪 Day 1 — Test File (DO NOT MODIFY STUDENT CODE)
👉 Instruction to Candidate
Implement the required classes in solution.py.
Do NOT change class names or method signatures.
Run this file to verify correctness.
📁 Expected Folder Structure
day1/
│
├── solution.py # Candidate writes code here
└── test_day1.py # Provided test file (below)
📄 (Skeleton Given to Candidate)