Skip to content

Day 1 — Bank account + Wallet :

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 class
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)

self.value is state
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:
No printing
Just return values
No global variables
👉 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
Store state inside self
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__
Store account_number
Store initial balance
deposit(amount)
If amount <= 0 → return False
Otherwise:
Add amount to balance
Return True
withdraw(amount)
If amount <= 0 → return False
If amount > balance → return False
Otherwise:
Deduct amount from balance
Return True
get_balance()
Return current 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

Correct use of __init__
Correct state mutation
No shared/global state
Correct return values

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
Otherwise:
Increase wallet balance
Return True
spend_money(amount)
If amount <= 0 → return False
 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.