Practice
1. A class
CarBuilder constructs a Car object step-by-step using methods like setEngine(), setWheels(), and finally build(). The Car constructor is private. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the construction process
The class uses multiple setter methods to configure parts of the object before finalizing withbuild(), indicating stepwise construction.Step 2: Match pattern intent
Builder pattern separates construction from representation, ideal for complex objects with many optional parts. Factory Method uses inheritance, Prototype clones objects, Singleton restricts instance count.Final Answer:
Option A -> Option AQuick Check:
Stepwise object creation with private constructor -> Builder pattern [check]
Hint: Stepwise construction with private constructor -> Builder pattern [check]
Common Mistakes:
2. To add a
WeekendFeeStrategy for weekend pricing without modifying existing classes, which SOLID principle is being applied and what is the minimum required change?medium
Solution
Step 1: Identify SOLID principles involved
Open-Closed Principle (OCP) requires new functionality added via extension, not modification; Dependency Inversion Principle (DIP) requires depending on abstractions.Step 2: Implement WeekendFeeStrategy as new class implementing FeeStrategy
This allows ParkingLot to use polymorphism without changing existing code, preserving OCP and DIP.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy class + polymorphic use -> OCP and DIP preserved [check]
Hint: New strategy class + interface use = OCP and DIP [check]
Common Mistakes:
3. In a parking lot system, the
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronized, what exact thread interleaving causes double-booking?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but before marking occupied, context switches to Thread 2 which also reads available.Step 2: Both threads proceed to mark the spot occupied independently
This leads to double-booking as both think the spot was free at check time.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap -> both threads see available -> double-booking [check]
Hint: Check-then-act without sync -> double-booking race [check]
Common Mistakes:
4. Electric cars require charging spots in the parking module. What is the minimum change to the current Java design to support
ElectricCar and ChargingSpot without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses allows extension without changing existing class code.Step 2: Update compatibility map externally to maintain open-closed principle and avoid class explosion.
Options B, C, and D require modifying existing classes, violating OCP or causing tight coupling.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
Hint: Add enums + subclass + external map update, no existing class change [check]
Common Mistakes:
5. The ESPNcricinfo system needs to support
ElectricCar vehicles that require ChargingSpot parking spots. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclasses allows extension without changing existing class code.Step 2: Update compatibility map for new types
Updating SpotCompatibility.COMPATIBILITY_MAP supports new vehicle-spot matching without altering existing logic.Final Answer:
Option C -> Option CQuick Check:
Extend enums + new subclasses + update compatibility map -> no existing class changes [check]
Hint: Extend enums + new subclasses + compatibility map -> minimal change [check]
Common Mistakes:
