Lesson 12 — Advanced
Machine Coding Round
Machine Coding round mein tumhe 45-60 minutes mein ek working system likhna hota hai. Clean code, design patterns, aur scalable design — sab test hota hai.
Machine Coding hota kya hai?
WHAT
Machine Coding ek interview round hai jisme tumhe ek real-world system code karna hota hai live — 45-60 minutes mein. Design patterns use karna, clean code likhna, aur scalability handle karna — sab test hota hai.
WHEN
Amazon, Google, Microsoft, Flipkart — sab companies ye round lete hain. DSA ke baad ye round hota hai jo tumhari design skills test karta hai.
WHERE
Parking Lot, Elevator System, Chat Application, Vending Machine, BookMyShow — popular machine coding problems hain jo frequently poochte jaate hain.
Problem-solving Approach
# Step 1: Requirements (5 min)
? Functional requirements kya hain?
? Non-functional requirements (scale, latency)?
? Edge cases kya ho sakte hain?
# Step 2: Design (10 min)
? Classes aur relationships banao
? Design patterns identify karo
? Interface design karo pehle
# Step 3: Coding (25 min)
? Clean, modular code likho
? Design patterns apply karo
? Error handling mat bhoolo
# Step 4: Testing (5 min)
? Basic test cases socho
? Edge cases handle karo
? Demo karo interviewer ko
# Golden Rules:
? Interface-first design karo
? Single Responsibility follow karo
? Error handling mat bhoolo
? Production-quality code likho
? Time management karo — don't overthink
Example: Parking Lot System
# Requirements:
1. Multiple floors, multiple vehicle types (bike, car, truck)
2. Parking spots type-wise allocated
3. Entry/exit points
4. Ticket generation with timestamp
5. Payment calculation
# Design:
from abc import ABC, abstractmethod
from enum import Enum
from datetime import datetime
class VehicleType(Enum):
BIKE = 1
CAR = 2
TRUCK = 3
class Vehicle:
def __init__(self, license_plate, vehicle_type):
self.license_plate = license_plate
self.vehicle_type = vehicle_type
class ParkingSpot:
def __init__(self, spot_id, vehicle_type):
self.spot_id = spot_id
self.vehicle_type = vehicle_type
self.is_occupied = False
self.vehicle = None
def park(self, vehicle):
if self.is_occupied:
return False
self.vehicle = vehicle
self.is_occupied = True
return True
def remove(self):
self.vehicle = None
self.is_occupied = False
class ParkingFloor:
def __init__(self, floor_id):
self.floor_id = floor_id
self.spots = []
def add_spot(self, spot):
self.spots.append(spot)
def find_spot(self, vehicle_type):
for spot in self.spots:
if (spot.vehicle_type == vehicle_type and
not spot.is_occupied):
return spot
return None
class ParkingLot:
_instance = None
@staticmethod
def get_instance():
if ParkingLot._instance is None:
ParkingLot._instance = ParkingLot()
return ParkingLot._instance
def __init__(self):
self.floors = []
def add_floor(self, floor):
self.floors.append(floor)
def park_vehicle(self, vehicle):
for floor in self.floors:
spot = floor.find_spot(vehicle.vehicle_type)
if spot:
spot.park(vehicle)
return Ticket(vehicle, spot, datetime.now())
return None # Full parking lot
class Ticket:
def __init__(self, vehicle, spot, entry_time):
self.vehicle = vehicle
self.spot = spot
self.entry_time = entry_time
Common Patterns in Machine Coding
# 1. Factory Pattern
? Objects create karna without specifying class
? VehicleFactory.create("car") — Car object
# 2. Strategy Pattern
? Behavior dynamically change karna
? PaymentStrategy: CreditCard, UPI, Cash
# 3. Observer Pattern
? Event notification
? ParkingLot full — Notify all apps
# 4. Singleton Pattern
? Single instance
? ParkingLot, Database connection
# 5. State Pattern
? Object state ke hisaab se behavior change
? Elevator states: Idle, Moving, DoorOpen
# 6. Decorator Pattern
? Additional behavior add karna
? Vehicle — Vehicle with Insurance — Vehicle with GPS
# Code Quality Tips:
? Meaningful variable names
? Method length < 20 lines
? DRY (Don't Repeat Yourself)
? Error handling (try/except)
? Type hints (Python 3.5+)
Interview Tips
# Before Coding:
1. Clarify requirements — 2 min waste better than 20 min wrong
2. Draw high-level design — interviewer se discuss karo
3. Identify core classes — interface-first approach
4. Talk through your thought process
# During Coding:
1. Start with interface/class definition
2. One feature at time — incremental development
3. Handle errors gracefully
4. Test as you go — don't wait till end
# After Coding:
1. Walk through the code
2. Discuss scalability
3. Mention improvements
4. Handle edge cases verbally
# Common Problems:
- Parking Lot: Singleton, Strategy, Factory
- Elevator: State, Observer
- Chat System: Observer, Strategy
- Vending Machine: State, Strategy
- BookMyShow: Singleton, Strategy, Observer
Exercise
Question: Parking Lot system mein sirf ek instance exist kare — kaunsa pattern use hoga? (1 word)
Question: Machine coding round mein sabse pehle kya karna chahiye coding shuru karne se pehle? (1 word)
Common mistakes
- Seedha coding shuru: Pehle design karo, phir code. Bina design ke code messy ho jaata hai.
- Time waste karna: 45 minutes mein complete karna hai — ek feature pe 20 min mat lagao.
- Error handling skip: Interviewer dekh raha hai ki tum production-quality code likh rahe ho ya nahi.
- Design patterns ignore: Interview mein patterns highlight karo — ye tumhari design thinking dikhata hai.
Machine Coding samajh aa gaya? Congratulations! System Design course complete ho gaya. Ab interview ke liye ready ho tum!