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
      • icon picker
        Day 3 : Library Management + Classroom Attendance System
      • 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 3 : Library Management + Classroom Attendance System

🟢 Day 3 — Multiple Objects & Object Interaction

🎯 Day 3 Objective

By the end of Day 3, the candidate should be able to:
Manage multiple objects at once
Store and retrieve objects using IDs
Write manager / controller classes
Handle interactions between objects
Implement problems where logic spans more than one class
Today is about coordination, not just state.

1️⃣ Teaching Script — What to Say

Start with This Reality

“In real systems, nothing works alone.”
Examples:
A library has many books
A classroom has many students
A game has many players
A booking system has many seats
Machine coding rounds almost always involve ​one manager class controlling many objects.

2️⃣ Core Idea of Day 3

One object manages many objects.
This manager:
Stores objects (list / dict)
Finds objects by ID
Calls methods on them
Enforces rules
This is usually where:
Bad design
Confusion
Bugs appear

3️⃣ Small Code to Understand (Very Important)

Example — Student Manager

class Student:
def __init__(self, student_id: int, name: str):
self.student_id = student_id
self.name = name


class StudentManager:
def __init__(self):
self.students = {}

def add_student(self, student: Student):
self.students[student.student_id] = student

def get_student(self, student_id: int):
return self.students.get(student_id)

Key Observations

StudentManager owns the collection
Student does NOT know about manager
IDs are used to fetch objects
.get() avoids crashes
🧠 This pattern appears everywhere in interviews.

4️⃣ Mandatory Warm-up Practice

Task

Implement these classes:
Class: User
- user_id (int)
- name (str)

Class: UserRegistry
Methods:
- add_user(user)
- get_user(user_id) → User or None
Rules:
Store users using dictionary
Do not print
Do not validate duplicates yet
👉 This is just to warm up object storage thinking.

5️⃣ Machine Coding Rules (Day 3 Rules)

Read carefully:
Manager class owns collections
Objects should not access manager
Use IDs for lookup
Return False / None for invalid operations
Do not guess missing behavior

6️⃣ Main Problem 1 — Library Management (Classic Interview Problem)

Problem Statement

You are building a library system.
A library contains multiple books
Each book can be borrowed by only one user at a time

Class Definitions (DO NOT MODIFY)

class Book:
def __init__(self, book_id: int, title: str):
pass

def borrow(self) -> bool:
pass

def return_book(self) -> bool:
pass

def is_borrowed(self) -> bool:
pass


class Library:
def __init__(self):
pass

def add_book(self, book: Book) -> bool:
pass

def borrow_book(self, book_id: int) -> bool:
pass

def return_book(self, book_id: int) -> bool:
pass

Requirements (Strict)

Book
Initially not borrowed
borrow():
If already borrowed → False
Else → mark borrowed, return True
return_book():
If not borrowed → False
Else → mark free, return True
Library
Store books using book_id
add_book(book):
If book_id already exists → False
Else add and return True
borrow_book(book_id):
If book does not exist → False
Else call book.borrow() and return result
return_book(book_id):
If book does not exist → False
Else call book.return_book() and return result

Example

lib = Library()
b1 = Book(1, "Python")

lib.add_book(b1) # True
lib.borrow_book(1) # True
lib.borrow_book(1) # False
lib.return_book(1) # True

What This Tests

Manager class logic
Delegating work to objects
Object isolation
ID-based lookup

7️⃣ Main Problem 2 — Classroom Attendance System

Problem Statement

You are building a classroom attendance system.
Classroom manages many students
Each student has present / absent state

Class Definitions (DO NOT MODIFY)

class Student:
def __init__(self, student_id: int, name: str):
pass

def mark_present(self) -> bool:
pass

def mark_absent(self) -> bool:
pass

def is_present(self) -> bool:
pass


class Classroom:
def __init__(self):
pass

def add_student(self, student: Student) -> bool:
pass

def mark_attendance(self, student_id: int, present: bool) -> bool:
pass

def get_present_count(self) -> int:
pass

Requirements

Student
Initially absent
Cannot mark present twice
Cannot mark absent twice
Classroom
Store students by student_id
Prevent duplicate students
mark_attendance():
If student does not exist → False
If present=True → call mark_present
Else → call mark_absent
get_present_count():
Return count of present students

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

Use dict[int, object]
Delegate logic to object
Do NOT duplicate logic in manager
Track boolean state
Count by looping over values

9️⃣ Evaluation Checklist

A correct solution:
✅ Uses manager class correctly
✅ Handles object lookup safely
✅ Prevents duplicate IDs
✅ Delegates logic properly
✅ Passes all edge cases
✅ No global state

🔟 Reflection (Candidate Must Answer)

Why should Library not store borrowed state itself?
Why is ID-based lookup preferred?
What breaks if objects manage each other?
Why is delegation important?

⭐ Day 3 Golden Rule

When objects interact, one must coordinate and others must obey.

🧪 Day 3 — Test File

📁 Folder Structure

day3/
├── solution.py
└── test_day3.py

📄 (Given to Candidate)

class Book:
def __init__(self, book_id: int, title: str):
pass

def borrow(self) -> bool:
pass

def return_book(self) -> bool:
pass

def is_borrowed(self) -> bool:
pass


class Library:
def __init__(self):
pass

def add_book(self, book: Book) -> bool:
pass

def borrow_book(self, book_id: int) -> bool:
pass

def return_book(self, book_id: int) -> bool:
pass


class Student:
def __init__(self, student_id: int, name: str):
pass

def mark_present(self) -> bool:
pass

def mark_absent(self) -> bool:
pass

def is_present(self) -> bool:
pass


class Classroom:
def __init__(self):
pass

def add_student(self, student: Student) -> bool:
pass

def mark_attendance(self, student_id: int, present: bool) -> bool:
pass

def get_present_count(self) -> int:
pass

📄 test_day3.py (Evaluator File)

from solution import Book, Library, Student, Classroom


def test_library():
print("Running Library tests...")

lib = Library()
b1 = Book(1, "Python")
b2 = Book(2, "DSA")

assert lib.add_book(b1) is True
assert lib.add_book(b2) is True
assert lib.add_book(b1) is False # duplicate

assert lib.borrow_book(1) is True
assert lib.borrow_book(1) is False
assert lib.return_book(1) is True
assert lib.return_book(1) is False

assert lib.borrow_book(99) is False # invalid ID

print("✅ Library tests passed")


def test_classroom():
print("Running Classroom tests...")

room = Classroom()
s1 = Student(1, "Amit")
s2 = Student(2, "Neha")

assert room.add_student(s1) is True
assert room.add_student(s2) is True
assert room.add_student(s1) is False # duplicate

assert room.get_present_count() == 0

assert room.mark_attendance(1, True) is True
assert room.mark_attendance(1, True) is False
assert room.get_present_count() == 1

assert room.mark_attendance(2, True) is True
assert room.get_present_count() == 2

assert room.mark_attendance(2, False) is True
assert room.get_present_count() == 1

assert room.mark_attendance(99, True) is False

print("✅ Classroom tests passed")


if __name__ == "__main__":
test_library()
test_classroom()

print("\n🎉 ALL DAY 3 TESTS PASSED SUCCESSFULLY")

🏁 What Day 3 Achieves

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