Practice
1. In designing a collection class that supports iteration, should the iterator interface be an abstract class or an interface in Java, and why?
easy
Solution
Step 1: Understand Java interface vs abstract class
Interfaces define behavior contracts without state, allowing multiple implementations. Abstract classes can hold state but restrict multiple inheritance.Step 2: Apply to iterator design
Iterator interface defines traversal methods (e.g.,hasNext(),next()) without state, enabling different iterator implementations per collection type.Final Answer:
Option D -> Option DQuick Check:
Iterator as interface enables flexible, stateless traversal contracts [check]
Hint: Iterator interface defines behavior, no state needed [check]
Common Mistakes:
2. A class has a private constructor and a static synchronized
getInstance() method that returns the single instance. Which pattern is applied and why is this the CORRECT use case?easy
Solution
Step 1: Identify the pattern from constructor and 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 Method uses subclassing, Builder separates construction steps, and Prototype clones objects; none match the private constructor plus static synchronized method pattern.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Hint: Private constructor + synchronized static method -> Singleton [check]
Common Mistakes:
3. In the Jigsaw Puzzle System, the
Board class has a placePiece() method that first checks if a position is free and then places the piece. Without synchronizing placePiece(), what exact sequence of thread interleaving causes two threads to place different pieces at the same position (double-booking)?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 checks position free and sees true, but before placing, context switches.Step 2: Thread 2 also checks position free -> true and proceeds to place piece concurrently, causing double-booking.
Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act allows both threads to place pieces at same position [check]
Hint: Check-then-act without sync -> double-booking race [check]
Common Mistakes:
4. In a multi-threaded Java Observer implementation, if
notifyObservers() is not synchronized, which exact sequence of events can cause observers to receive inconsistent or duplicate updates?medium
Solution
Step 1: Identify race condition in notifyObservers()
Without synchronization, Thread 1 reads observer list size as 3, then context switches.Step 2: Thread 2 modifies observer list by removing one observer, reducing size to 2.
Thread 1 resumes and tries to notify 3 observers, causing out-of-date or duplicate notifications.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized notifyObservers() -> stale observer list read -> inconsistent notifications [check]
Hint: Unsynchronized notify reads stale observer list -> duplicate notifications [check]
Common Mistakes:
5. The Workflow Engine must support
ElectricCar vehicles requiring ChargingSpot compatibility. What is the minimum change to the current design to add this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot must not modify existing classes to respect OCP and avoid class explosion.Step 2: Use enums and subclassing
Adding new enum values and a new ElectricCar subclass extends functionality cleanly; updating compatibility map centralizes spot-vehicle matching without changing existing code.Step 3: Avoid modifying base classes or Executor
Modifying Vehicle or Executor violates OCP and risks introducing bugs.Final Answer:
Option D -> Option DQuick Check:
New enums + subclass + compatibility map update -> minimal, no existing class change [check]
Hint: Add enums + subclass + map update, no existing class change [check]
Common Mistakes:
