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
      • icon picker
        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)
      • 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 4 : Bank account and notification system.

Today we cover Inheritance, but in a strict, interview-realistic way:
❌ No design pattern talk
❌ No theory lectures
❌ No abstraction for abstraction’s sake
✅ Inheritance only when the problem forces it
✅ Heavy implementation with clear rules

🟢 Day 4 — Inheritance & Method Overriding (Machine Coding Style)

🎯 Day 4 Objective

By the end of Day 4, the candidate should be able to:
Implement inheritance when required
Share common data and behavior using a base class
Override methods correctly
Use super() properly
Handle different rules for different subclasses
Pass machine-coding tests involving parent–child classes
Today is about reusing structure, not logic guessing.

1️⃣ Teaching Script — What to Say

Start with This Reality

“Most of the time, you do NOT need inheritance. But when the problem says ‘There are multiple types of X’, you almost always do.”
Examples from interviews:
Employee → FullTime / PartTime
Account → Savings / Current
Vehicle → Bike / Car
Inheritance exists to share common structure, not to look smart.

2️⃣ Core Idea of Inheritance (Only What’s Needed)

A child class IS A parent class, but with extra or different behavior.
Child inherits:
Variables
Methods
Child can:
Override methods
Add new behavior

3️⃣ Small Code to Understand (Minimal, Clear)

Example — Employee Base Class

class Employee:
def __init__(self, emp_id: int, salary: int):
self.emp_id = emp_id
self.salary = salary

def calculate_pay(self) -> int:
return self.salary

Child Class

class PartTimeEmployee(Employee):
def calculate_pay(self) -> int:
return self.salary // 2

Key Observations

Same method name
Different behavior
Caller does NOT need to know type
🧠 Machine coding tests whether you override correctly, not creatively.

4️⃣ Mandatory Warm-up Practice

Task

Implement the following:
Base class: Shape
- area() → int

Child classes:
- Square (side)
- Rectangle (length, width)
Rules:
Use inheritance
Override area()
No prints, only return
👉 This ensures candidate understands override mechanics.

5️⃣ Machine Coding Rules (Day 4 Rules)

Use inheritance only where specified
Do NOT duplicate common fields
Call super().__init__() when required
Override only the required methods
Keep logic exactly as specified

6️⃣ Main Problem 1 — Account Types (Classic Interview Question)

Problem Statement

You are implementing a banking system with different account types.
All accounts have:
account number
balance
But withdrawal rules differ.

Class Definitions (DO NOT MODIFY)

class Account:
def __init__(self, account_number: str, balance: int):
pass

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

def get_balance(self) -> int:
pass


class SavingsAccount(Account):
def withdraw(self, amount: int) -> bool:
pass


class CurrentAccount(Account):
def withdraw(self, amount: int) -> bool:
pass

Requirements (Very Strict)

Account (Base Class)
Stores account_number and balance
get_balance() returns balance
withdraw() in base class should:
Always return False
(Base class should not allow withdrawal)
SavingsAccount
Cannot withdraw more than balance
If amount <= 0 → return False
If amount > balance → return False
Else:
Deduct amount
Return True
CurrentAccount
Can overdraft up to -1000
If amount <= 0 → return False
If balance - amount < -1000 → return False
Else:
Deduct amount
Return True

Example

sa = SavingsAccount("S1", 500)
sa.withdraw(600) # False
sa.withdraw(200) # True

ca = CurrentAccount("C1", 500)
ca.withdraw(1200) # True (balance becomes -700)
ca.withdraw(500) # False (would exceed overdraft)

7️⃣ Main Problem 2 — Notification System

Problem Statement

You are implementing a notification system.
Different notification types behave differently, but share a common interface.

Class Definitions (DO NOT MODIFY)

class Notification:
def send(self, message: str) -> bool:
pass


class EmailNotification(Notification):
def send(self, message: str) -> bool:
pass


class SMSNotification(Notification):
def send(self, message: str) -> bool:
pass

Requirements

Notification
Base class send() should:
Always return False
EmailNotification
If message length == 0 → return False
Else → return True
SMSNotification
If message length > 160 → return False
Else → return True

Important Note

No actual sending
Just enforce rules via return values

8️⃣ No-Guess Hints (Read Only If Stuck)

Base class may intentionally do nothing
Override only behavior, not structure
Use self.balance inherited from base
Do not add extra fields
Tests will treat objects polymorphically

9️⃣ Evaluation Checklist

A correct solution:
✅ Correct inheritance hierarchy
✅ Proper method overriding
✅ Correct use of super() if needed
✅ No duplicate fields
✅ Correct rule enforcement
✅ Passes all test cases

🔟 Reflection (Candidate Must Answer)

Why does base Account.withdraw() return False?
Why not duplicate balance in child classes?
What breaks if child does not override method?
When should inheritance be avoided?

⭐ Day 4 Golden Rule

Inheritance is a tool, not a default. Use it only when the problem forces it.

🧪 Day 4 — Test File

📁 Folder Structure

day4/
├── solution.py
└── test_day4.py

📄 (Given to Candidate)

class Account:
def __init__(self, account_number: str, balance: int):
pass

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

def get_balance(self) -> int:
pass


class SavingsAccount(Account):
def withdraw(self, amount: int) -> bool:
pass


class CurrentAccount(Account):
def withdraw(self, amount: int) -> bool:
pass


class Notification:
def send(self, message: str) -> bool:
pass


class EmailNotification(Notification):
def send(self, message: str) -> bool:
pass


class SMSNotification(Notification):
def send(self, message: str) -> bool:
pass

📄 test_day4.py (Evaluator File)

from solution import (
Account,
SavingsAccount,
CurrentAccount,
EmailNotification,
SMSNotification
 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.