Practice
1. In designing the Meeting Scheduler, should the
Attendee entity be modeled as an abstract class or an interface, considering that attendees can be both internal employees and external guests with different data and behaviors?easy
Solution
Step 1: Analyze state and behavior needs
Attendee has shared state (name, email) and some common behavior, which favors abstract class to avoid duplicating fields.Step 2: Evaluate interface limitations
Interfaces cannot hold state, so using interface would force duplication or external state holders, complicating design.Final Answer:
Option C -> Option CQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Shared state? Use abstract class, not interface [check]
Common Mistakes:
2. A class provides a
createIterator() method that returns an object allowing traversal over its elements without exposing its internal data structure. Which design pattern is being applied and why is this the correct choice?easy
Solution
Step 1: Identify the role of
This method returns an object that allows clients to traverse elements without exposing the collection's internal representation, which is the core intent of the Iterator pattern.createIterator()Step 2: Eliminate other patterns
Factory Method focuses on object creation, Decorator adds responsibilities dynamically, and Composite manages tree structures. None focus on traversal abstraction.Final Answer:
Option C -> Option CQuick Check:
Iterator separates traversal from collection internals [check]
Hint: Iterator hides structure, exposes traversal [check]
Common Mistakes:
3. In designing an Observer interface in Java, should it be an abstract class or an interface, and why? Consider that observers only define a single method
update() without shared state.easy
Solution
Step 1: Analyze Observer requirements
Observers only defineupdate()without shared state, so no need for instance variables or default behavior.Step 2: Choose interface for flexibility
Interfaces allow multiple inheritance and keep observers lightweight; abstract classes add unnecessary state and restrict inheritance.Final Answer:
Option B -> Option BQuick Check:
Observer interface with single method and no state -> interface preferred [check]
Hint: Observer has no state, so interface enables flexible multiple inheritance [check]
Common Mistakes:
4. In LinkedIn's
ConnectionManager, the method addConnection(User a, User b) checks if users are already connected before adding. Without synchronizing this method, what exact thread interleaving causes a double connection to be created?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can see no existing connection and proceed to add, causing duplicates.Step 2: Trace exact thread interleaving
Thread 1 callsisConnected(a,b)and gets false, then context switches before adding. Thread 2 also callsisConnected(a,b)and gets false, then both add connections concurrently.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap causes double connection addition [check]
Hint: Check-then-act without sync -> double add race [check]
Common Mistakes:
5. In a parking lot system, the
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronized, what exact thread interleaving causes double-booking?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but before marking occupied, context switches to Thread 2 which also reads available.Step 2: Both threads proceed to mark the spot occupied independently
This leads to double-booking as both think the spot was free at check time.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap -> both threads see available -> double-booking [check]
Hint: Check-then-act without sync -> double-booking race [check]
Common Mistakes:
