Practice
TokenBucketRateLimiter class, should the RateLimiter be an abstract class or an interface? Consider that the rate limiter maintains internal token state and exposes tryConsume() behavior.Solution
Step 1: Analyze state and behavior requirements
RateLimiter needs to maintain internal token count state and provide tryConsume() behavior.Step 2: Abstract class vs interface for stateful behavior
Abstract class can hold shared mutable state and provide partial implementation; interfaces cannot hold instance state.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared token state and behavior [check]
Vehicle be an abstract class or an interface, and why?Solution
Step 1: Analyze Vehicle's shared state and behavior
Vehicle has common fields (e.g., licensePlate) and methods (e.g., startEngine) that require shared implementation and state storage.Step 2: Abstract class vs interface suitability
Abstract classes allow shared state and method implementations, while interfaces cannot hold instance state. Thus, abstract class fits Vehicle better.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
Vehicle abstraction that requires storing state like speed and fuelLevel, and behavior like accelerate(), should Vehicle be an abstract class or an interface? Why?Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to hold shared mutable state likespeedandfuelLevel, which interfaces cannot hold as fields.Step 2: Evaluate abstract class vs interface
Abstract classes can have instance variables and default method implementations, enabling code reuse and shared state. Interfaces cannot hold instance state, only constants.Final Answer:
Option A -> Option AQuick Check:
Stateful abstraction -> abstract class, not interface [check]
RoomBookingManager class has a bookRoom() method that checks if a room is available and then marks it booked. Without synchronizing bookRoom(), which exact sequence of events between Thread 1 and Thread 2 causes a double-booking race condition?Solution
Step 1: Identify check-then-act gap
Without synchronization, Thread 1 reads availability as true but is preempted before booking; Thread 2 also reads true and proceeds.Step 2: Both threads book simultaneously causing double-booking
Because the availability check and booking are not atomic, both threads can book the same room concurrently.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
ElectricCar and ChargingSpot to the existing Abstract Factory design without modifying existing classes. What is the minimum change to support this extensibility in Java?Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Apply Java-specific extensibility
Adding new enum values and creating new classes for ElectricCar and ChargingSpot allows extension without modifying existing classes. Updating compatibility map centralizes compatibility logic.Step 3: Eliminate violating options
Options B and D modify existing classes, violating OCP. Create a new factory subclass for electric vehicles and modify the client code to use it. requires client code changes but not existing classes.Final Answer:
Option A -> Option AQuick Check:
New enums and classes added, existing classes untouched [check]
