Practice
1. In designing the
User and Message classes for the messaging system, should User be an abstract class or an interface, and why?easy
Solution
Step 1: Analyze if User has state or just behavior
User holds common state such as userId and username, which requires fields and possibly default implementations.Step 2: Determine if abstract class or interface fits state and behavior
Abstract class supports shared state and partial implementation, while interface cannot hold instance state.Final Answer:
Option C -> Option CQuick Check:
User has common state -> abstract class fits better than interface [check]
Hint: Stateful base class -> abstract class, stateless -> interface [check]
Common Mistakes:
2. In designing the
Player abstraction for the Snake and Ladder game, should Player be an abstract class or an interface, and why?easy
Solution
Step 1: Analyze state and behavior requirements
ThePlayerhas common state such as current position on the board and methods like move().Step 2: Choose abstraction type
An abstract class allows sharing state and default method implementations, which interfaces (pre-Java 8) cannot provide.Final Answer:
Option B -> Option BQuick Check:
Abstract class holds shared state and behavior [check]
Hint: State + default behavior -> abstract class, not interface [check]
Common Mistakes:
3. In a parking system, should
Vehicle be an abstract class or an interface if it needs to hold state like licensePlate and provide default behavior for getSize()? Why?easy
Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold state likelicensePlateand provide default behavior, it requires fields and method implementations.Step 2: Abstract class vs interface capabilities
Abstract classes can have instance fields and concrete methods, while interfaces cannot hold instance state (only constants) and default methods cannot manage instance variables.Final Answer:
Option C -> Option CQuick Check:
State + default behavior -> abstract class, not interface [check]
Hint: State needs abstract class; interfaces lack instance fields [check]
Common Mistakes:
4. You want to add a
WeekendFeeStrategy to calculate notification fees differently on weekends. According to SOLID principles, what is the minimum change required in the current design that uses a FeeStrategy interface?medium
Solution
Step 1: Identify applicable SOLID principle
The Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it without modifying existing classes. adds a new class implementing FeeStrategy without modifying existing code, adhering to OCP. Others modify existing classes or interfaces, violating OCP.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends behavior without modifying existing code [check]
Hint: OCP: extend with new class, don't modify existing [check]
Common Mistakes:
5. 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:
