Practice
1. In designing an Elevator system using the State pattern, should the ElevatorState be an abstract class or an interface, and why?
easy
Solution
Step 1: Analyze shared code in states
Elevator states often share common fields (e.g., reference to context) and helper methods, which favors an abstract class.Step 2: Evaluate interface limitations
Interfaces cannot hold state or provide default implementations (prior to Java 8), so they are less suitable if shared code exists.Final Answer:
Option B -> Option BQuick Check:
Shared code and state -> abstract class [check]
Hint: Shared code and state -> abstract class; behavior only -> interface [check]
Common Mistakes:
2. A class has a private constructor and a static synchronized
getInstance() method returning the single instance. Which pattern is applied and why is this the CORRECT use case?easy
Solution
Step 1: Identify Singleton pattern characteristics
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance with thread-safe lazy initialization.Step 2: Eliminate other creational patterns
Factory Method controls subclass instantiation, Builder separates construction from representation, and Prototype clones objects; none enforce single instance or thread safety.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Hint: Private constructor + synchronized getInstance() -> Singleton [check]
Common Mistakes:
3. In the Workflow Engine, if the
Executor class's executeStep() method is not synchronized, which exact thread interleaving causes two threads to execute the same step concurrently, leading to inconsistent state?medium
Solution
Step 1: Identify the check-then-act race
Without synchronization, Thread 1 checks step status as free, then context switches before marking running.Step 2: Thread 2 repeats check before Thread 1 marks running
Thread 2 also sees step free and proceeds, causing both to execute concurrently.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act -> double execution race [check]
Hint: Check-then-act without sync -> race condition [check]
Common Mistakes:
4. In the Tic-Tac-Toe game server, the
makeMove() method is not synchronized. Which exact sequence of events between Thread 1 and Thread 2 can cause two players to place their marks on the same cell (double-booking)?medium
Solution
Step 1: Identify the race condition in makeMove()
Without synchronization, the check-then-act sequence is vulnerable: Thread 1 checks if cell is empty and is preempted before marking.Step 2: Trace Thread 2's actions
Thread 2 also checks the same cell, sees it empty, and proceeds to mark it. Both threads mark the same cell, causing double-booking.Final Answer:
Option D -> Option DQuick Check:
Check-then-act gap without sync -> double-booking race [check]
Hint: Check empty -> context switch -> both mark same cell [check]
Common Mistakes:
5. Adding a
WeekendFeeStrategy to the parking lot pricing requires which minimum change to comply with SOLID principles?medium
Solution
Step 1: Identify the SOLID principle involved
The Open-Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without modifying existing classes. respects OCP by adding a new strategy class without modifying existing code. Add a booleanisWeekendflag toVehicleand change pricing logic accordingly. violates OCP by modifyingVehiclestate, which is unrelated to pricing and causes tight coupling.Final Answer:
Option B -> Option BQuick Check:
New strategy class -> OCP-compliant extension [check]
Hint: OCP: add new class, don't modify existing ones [check]
Common Mistakes:
