Top 6 Strategy pattern Interview Questions
Browse 6 real-world technical and behavioral interview questions about Strategy pattern. Review scenarios, edge cases, and architectural best practices.
Design a food-ordering system: customers place orders with restaurants, and the system assigns a delivery partner. Walk me through the classes.
A food ordering design has two lifecycles: the order moves from placed to delivered, while delivery partner assignment runs alongside and can fail and restart. Keep them separate, claim a partner atomically, rank candidates behind a strategy, and write cancellation windows as rules keyed on order state.
Design an elevator system for a building with N lifts. When someone presses the call button, which lift answers, and where in your code is that decided?
An elevator system splits into a state machine per lift that decides its own next floor, and a dispatcher that assigns each hall call to one lift through a pluggable strategy such as a nearest-suitable-car cost. Car buttons go straight to their lift, and one control loop owns all state, so no god class and no locks.
Design the classes for a notification service that sends email, SMS and push. Which design patterns earn their place, and which would you leave out?
Name what varies first: channel, text, the user's channel choices and retry. A notification service then needs a channel interface (a strategy) picked from a registry, templates as data, a preference policy and a retry policy splitting transient from permanent errors. Most other design patterns add classes, not value.
Startup fails with NoUniqueBeanDefinitionException after someone adds a second implementation. Do you reach for @Primary, @Qualifier or a list of beans, and which fix ages badly?
NoUniqueBeanDefinitionException means one injection point matches two beans by type. Fix it where the decision belongs: a qualifier at the consumer when callers need different beans, a List or Map when all are used, a conditional bean when only one should exist. A primary bean is the quick fix that ages worst.
Design a parking lot system — you have 90 minutes, working code at the end.
In a parking lot machine coding round, scope one deterministic flow of park, unpark and charge, model Vehicle, Slot, Level, Ticket and a ParkingLot facade, then push slot allocation and pricing behind strategy interfaces so review-time variants are configuration rather than surgery.
When would you use the strategy pattern instead of inheritance?
Use the strategy pattern instead of inheritance when behaviour varies independently from object identity. Composition avoids subclass explosion, enables runtime swapping and makes algorithms easier to test.