Practice
1. A class provides a
createIterator() method that returns an object allowing traversal over its elements without exposing its internal data structure. Which design pattern is being applied and why is this the correct choice?easy
Solution
Step 1: Identify the role of
This method returns an object that allows clients to traverse elements without exposing the collection's internal representation, which is the core intent of the Iterator pattern.createIterator()Step 2: Eliminate other patterns
Factory Method focuses on object creation, Decorator adds responsibilities dynamically, and Composite manages tree structures. None focus on traversal abstraction.Final Answer:
Option C -> Option CQuick Check:
Iterator separates traversal from collection internals [check]
Hint: Iterator hides structure, exposes traversal [check]
Common Mistakes:
2. In a parking lot system, if
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 causes double-booking of the same spot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available, then context switches before marking it occupied.Step 2: Thread 2 also reads spot as available and proceeds to park, causing double-booking
Both threads believe the spot is free and park concurrently, violating mutual exclusion.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check availability -> context switch -> both park -> double-booking [check]
Common Mistakes:
3. The ATM system needs to support
ElectricCar vehicles requiring ChargingSpot types. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Apply minimal change
Add new enum constants ELECTRIC_CAR and CHARGING. Create ElectricCar subclass. Update compatibility map externally to include new types. No existing classes modified.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + external map update -> no existing class changes [check]
Hint: Add enums + subclass + external map update -> no existing class changes [check]
Common Mistakes:
4. To add support for ElectricCar objects requiring ChargingSpots in the existing LRU Cache design (which models Vehicle and Spot types), what is the minimum change that respects extensibility and avoids modifying existing classes?
hard
Solution
Step 1: Identify extensibility requirements
We want to add ElectricCar and ChargingSpot without modifying existing classes to respect OCP.Step 2: Evaluate options
Modify Vehicle base class to include a boolean isElectric field and add logic in Spot class to check this field. modifies existing Vehicle class, violating OCP. Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map; no existing classes modified. adds enum values and compatibility map without changing existing classes, preserving extensibility.Step 3: Confirm minimal change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map; no existing classes modified. adds new enum constants and new subclass, updating compatibility map externally, no existing class code changed.Final Answer:
Option A -> Option AQuick Check:
New enums and subclass added, existing classes untouched [check]
Hint: Add enums and subclass, avoid modifying existing classes [check]
Common Mistakes:
5. To add
ElectricCar needing ChargingSpot without modifying existing classes, what is the minimum change to the current design using enums VehicleType and SpotType and a compatibility map?hard
Solution
Step 1: Understand current enum and compatibility map design
VehicleType and SpotType enums define types; compatibility map links them. Adding new types requires updating enums and map.Step 2: Analyze minimal change without modifying existing classes
AddELECTRIC_CARtoVehicleType,CHARGINGtoSpotType, createElectricCarsubclass, and update compatibility map; no existing classes modified adds new enum values and classes without modifying existing classes, respecting OCP. ModifyVehiclebase class to include charging capability and add logic in existing spot classes modifies base Vehicle class, violating OCP but minimal code change.Step 3: Choose minimal change that avoids modifying existing classes
AddELECTRIC_CARtoVehicleType,CHARGINGtoSpotType, createElectricCarsubclass, and update compatibility map; no existing classes modified is minimal and respects OCP; ModifyVehiclebase class to include charging capability and add logic in existing spot classes modifies existing classes, violating requirement.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map update -> minimal change without existing class modification [check]
Hint: Add enums + subclass + map update; no existing class edits [check]
Common Mistakes:
