Practice
1. In the ESPNcricinfo system, the
Match class has a private constructor and a static synchronized getInstance() method to ensure only one instance per match ID exists. Which design pattern is applied here and why is this the correct use case?easy
Solution
Step 1: Identify the pattern from constructor and getInstance()
The private constructor prevents direct instantiation, and the static synchronized getInstance() ensures controlled, thread-safe instance creation.Step 2: Why Singleton fits this use case
Singleton ensures only one Match instance per match ID exists, avoiding inconsistent state across threads.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + synchronized getInstance() -> Singleton [check]
Common Mistakes:
2. A class has a private constructor and a static synchronized
getInstance() method that returns the single instance. Which design pattern is applied and why is this the CORRECT use case?easy
Solution
Step 1: Identify the pattern from private constructor and static synchronized method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created lazily and safely in multithreaded contexts.Step 2: Eliminate other creational patterns
Factory controls creation but allows multiple instances; Builder separates construction steps; Prototype clones instances. None enforce single instance with thread safety.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> thread-safe Singleton [check]
Hint: Private constructor + synchronized getInstance() -> Singleton [check]
Common Mistakes:
3. In the Meeting Scheduler, the
RoomBookingManager class has a bookRoom() method that checks if a room is available and then marks it booked. Without synchronizing bookRoom(), which exact sequence of events between Thread 1 and Thread 2 causes a double-booking race condition?medium
Solution
Step 1: Identify check-then-act gap
Without synchronization, Thread 1 reads availability as true but is preempted before booking; Thread 2 also reads true and proceeds.Step 2: Both threads book simultaneously causing double-booking
Because the availability check and booking are not atomic, both threads can book the same room concurrently.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act without sync -> double-booking race [check]
Common Mistakes:
4. To add a
WeekendFeeStrategy for weekend pricing in the current booking system design, which minimal change respects the Open-Closed Principle (OCP)?medium
Solution
Step 1: Identify OCP requirement
OCP requires adding new functionality without modifying existing tested code.Step 2: Evaluate options
Add a boolean flagisWeekendinBookingand handle fees there. adds weekend flag in Booking, which violates OCP by modifying Booking class. Create a new class implementingFeeStrategyinterface for weekend fees and inject it without changing existing classes. creates a new FeeStrategy class, respecting OCP by extension. Modify the existingFeeCalculatorclass to add weekend logic with if-else conditions. and D modify existing classes, violating OCP.Final Answer:
Option C -> Option CQuick Check:
New FeeStrategy class -> extend without modifying existing code [check]
Hint: New strategy class -> extend, don't modify [check]
Common Mistakes:
5. ElectricCar pieces require special
ChargingSpot compatibility in the Jigsaw Puzzle System. What is the minimum design change to support ElectricCar and ChargingSpot without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
New piece and spot types must be added without modifying existing classes to respect OCP.Step 2: Evaluate minimal change approach
Adding new enum values and subclass for ElectricCarPiece plus updating compatibility map externally allows extension without modifying existing code.Final Answer:
Option A -> Option AQuick Check:
New enums and subclass extend system without changing existing classes [check]
Hint: Add enums + subclass + external map update = minimal invasive extension [check]
Common Mistakes:
