Parking Lot System
Core entities SEEDHE noun se nikalte hain: ParkingLot → Floor → ParkingSlot, aur Vehicle, Ticket, Payment. Sabse important design decision hai SLOT ALLOTMENT — kaunsi gaadi ko kaunsa slot milega. Ise ek interface (SlotAllocationStrategy) banao, kyunki interviewer AKSAR poochta hai "nearest-to-entry allotment kaise karoge?"
Fee calculation ko HARDCODE mat karo. Hourly, slab-based aur day-pass — teeno aa sakte hain, isliye FeeStrategy interface banao. Aur concurrency zaroor discuss karo: do gaadiyan ek hi slot na le lein, isliye slot booking ATOMIC honi chahiye (synchronized ya ConcurrentHashMap ka putIfAbsent).
enum VehicleType { BIKE, CAR, TRUCK }
class ParkingSlot {
private final String id;
private final VehicleType supportedType;
private volatile boolean occupied;
synchronized boolean assign(Vehicle v) {
if (occupied || v.getType() != supportedType) return false;
occupied = true;
return true;
}
}
interface SlotAllocationStrategy { Optional<ParkingSlot> find(Floor f, Vehicle v); }
interface FeeStrategy { double calculate(Ticket t, Instant exitTime); }
class ParkingLotService {
Ticket park(Vehicle v) { /* strategy se slot dhoondo, ticket banao */ }
Receipt unpark(String ticketId) { /* fee nikaalo, slot free karo */ }
}- Entities: ParkingLot → Floor → Slot, plus Vehicle, Ticket, Payment
- SlotAllocationStrategy aur FeeStrategy alag interfaces — dono badalte hain
- Concurrency zaroor discuss karo: slot assignment atomic honi chahiye
Sabse simple strategy hai FIRST AVAILABLE — jo pehla khaali slot mile de do. Real systems NEAREST TO ENTRY use karte hain, jiske liye har slot ki entry se distance store karni padti hai aur per-floor ek PriorityQueue rakhi jaati hai.
Ek aur variation hai SIZE-BASED FALLBACK — bike ka slot na ho to car slot de do (bada slot chhoti gaadi le sakti hai, ulta nahi). Ye batana ki "fallback allowed hai ya nahi" ek requirement question hai, aur ye poochna hi seniority dikhata hai.
class NearestSlotStrategy implements SlotAllocationStrategy {
// Har floor par distance se sorted PriorityQueue
private final Map<VehicleType, PriorityQueue<ParkingSlot>> freeSlots;
public Optional<ParkingSlot> find(Floor floor, Vehicle v) {
var queue = freeSlots.get(v.getType());
return Optional.ofNullable(queue.poll()); // O(log n)
}
}Fee ke teen common models hain: HOURLY (per hour rate), SLAB (pehle 2 ghante ₹30, uske baad ₹10/hour), aur DAY PASS (flat). Teeno FeeStrategy ke implementations hain — isliye is interface ka hona zaroori hai.
Edge cases zaroor bolo: 61 minute ko 2 ghante count karna (ceiling), free grace period (pehle 15 minute free), aur ticket kho jaane par maximum charge. Ye chhoti baatein interviewer ko dikhati hain ki tumne real system socha hai.