Practice
1. In designing the Amazon Locker Service, should the
Package entity be modeled as an abstract class or an interface, and why?easy
Solution
Step 1: Analyze Package state and behavior
Package has shared mutable state such as OTP, expiry time, and tracking info, which requires fields and common method implementations.Step 2: Abstract class vs interface suitability
Abstract classes can hold state and provide default behavior, interfaces cannot hold mutable state (prior to Java 8 default methods, but still no fields). Therefore, abstract class fits better.Final Answer:
Option B -> Option BQuick Check:
Shared mutable state -> abstract class needed [check]
Hint: Stateful entities -> abstract class; stateless behaviors -> interface [check]
Common Mistakes:
2. 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
Vehicle needs to holdlicensePlate(state) and provide defaultgetSize()behavior, which interfaces cannot hold as instance variables.Step 2: Choose correct OOP construct
Abstract classes can hold state and provide default implementations, interfaces cannot hold instance state (only constants).Final Answer:
Option A -> Option AQuick Check:
State + default behavior -> abstract class, not interface [check]
Hint: State needs abstract class; interfaces can't hold instance variables [check]
Common Mistakes:
3. You want to add a
WeekendFeeStrategy to the rate limiter's fee calculation without modifying existing classes. Which SOLID principle applies and what is the minimum change?medium
Solution
Step 1: Identify SOLID principle for adding new fee logic
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options for minimal change respecting OCP and DIP
Add weekend flag to RateLimiter and use if-else inside existing fee method adds weekend flag and if-else inside existing method, violating OCP by modifying existing class. Create new WeekendFeeStrategy implementing FeeStrategy interface; inject it via constructor (OCP and DIP enforced) respects OCP and DIP by adding new class and injecting it.Final Answer:
Option A -> Option AQuick Check:
Minimal change but violates OCP by modifying existing class [check]
Hint: OCP: add new class, not modify existing code [check]
Common Mistakes:
4. Consider a non-thread-safe iterator implementation over a shared collection. Without synchronization on
next(), which exact sequence of Thread 1 and Thread 2 calls can cause the iterator to return the same element twice?medium
Solution
Step 1: Trace the check-then-act gap
Thread 1 callshasNext()and sees true, but before callingnext(), context switches to Thread 2.Step 2: Thread 2 calls
Thread 2 consumes the next element, advancing the iterator state.hasNext()andnext()Step 3: Thread 1 resumes and calls
Thread 1 returns the same element it saw innext()hasNext(), causing duplicate return.Final Answer:
Option B -> Option BQuick Check:
Unsynchronized check-then-act causes duplicate element return [check]
Hint: Check-then-act gap causes duplicate iteration [check]
Common Mistakes:
5. In a parking lot system, if
parkVehicle() is not synchronized, which exact thread interleaving causes double-booking of the same spot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available and is preempted before marking it occupied. Thread 2 also reads available and proceeds to park.Step 2: Confirm both threads park concurrently causing double-booking
Both threads believe the spot is free and park, leading to inconsistent state and double-booking.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act without sync -> double-booking race condition [check]
Common Mistakes:
