Practice
1. In a Java codebase, a factory class provides methods to create related objects like
Button and Checkbox families without specifying their concrete classes. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the role of the factory class
The factory creates multiple related objects (Button, Checkbox) without exposing their concrete classes, indicating a family of products.Step 2: Match pattern intent
Abstract Factory pattern is designed to create families of related objects without specifying their concrete classes, unlike Builder (stepwise construction), Singleton (single instance), or Prototype (cloning).Final Answer:
Option C -> Option CQuick Check:
Abstract Factory creates related object families without concrete classes [check]
Hint: Abstract Factory creates related families, not single objects [check]
Common Mistakes:
2. In designing the traffic light system, should
Vehicle be an abstract class or an interface, considering vehicles have state like speed and direction? Why?easy
Solution
Step 1: Analyze state vs behavior requirements
Vehicles have shared state (speed, direction) and behavior; interfaces cannot hold instance state, so interface alone is insufficient.Step 2: Abstract class encapsulates shared state and common methods
Abstract class allows shared fields and partial implementation, enabling subclasses to extend and customize behavior.Final Answer:
Option C -> Option CQuick Check:
Stateful base class -> abstract class, not interface [check]
Hint: Stateful base class -> abstract class, stateless -> interface [check]
Common Mistakes:
3. In a multithreaded LRU Cache, if the
put() method is not synchronized, which exact sequence of operations between Thread 1 and Thread 2 can cause the cache to contain duplicate keys or corrupt the doubly linked list?medium
Solution
Step 1: Identify the race condition in put()
Without synchronization, two threads can both check for key existence and find it missing before either inserts, leading to duplicate nodes for the same key.Step 2: Trace the exact interleaving
Thread 1: check key -> false; context switch; Thread 2: check key -> false; both insert new nodes, corrupting the cache state.Final Answer:
Option D -> Option DQuick Check:
Concurrent check-then-act without lock -> duplicate keys inserted [check]
Hint: Check-then-act without lock causes duplicates [check]
Common Mistakes:
4. In a multi-threaded environment, if the
addProductToCart() method is not synchronized, which exact sequence of events can cause the cart to contain duplicate entries for the same product?medium
Solution
Step 1: Identify check-then-act race condition
Both threads check if product exists and see false before either adds it, due to lack of synchronization.Step 2: Both threads add product concurrently causing duplicates
Because the add operation is not atomic, both add the product, resulting in duplicates.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> duplicate entries [check]
Hint: Check-then-act gap -> duplicate add without sync [check]
Common Mistakes:
5. The system needs to support
ElectricCar and ChargingSpot without modifying existing classes. What is the minimum change to the current design to support this?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 SpotCompatibility map to include new types
Updating compatibility map is a configuration change, not a class modification, preserving OCP.Final Answer:
Option C -> Option CQuick Check:
Enum extension + new subclasses + compatibility map update -> no existing class change [check]
Hint: Extend enums + subclasses + config map -> no class edits [check]
Common Mistakes:
