Design Patterns interview question · Most asked
Explain the Strategy pattern with an example.
Summary
Strategy puts interchangeable algorithms behind one interface; a context holds one strategy and delegates to it, so the algorithm can be chosen at runtime without if/else. Shipping cost rules (standard, express, pickup) are a typical example.
Longer Answer
Each strategy is testable alone, and new ones are added without editing the context (Open/Closed). Selection usually happens once, through configuration or a lookup table. In Python, TypeScript, modern Java and PHP a strategy is often just a function or lambda; comparators passed to sort functions are strategies.
Code Example
STRATEGIES = { # strategies as plain functions in a lookup table
"standard": lambda subtotal: 0 if subtotal >= 5000 else 499,
"express": lambda subtotal: 1299,
"pickup": lambda subtotal: 0,
}
def total(subtotal, method):
return subtotal + STRATEGIES[method](subtotal)
for method in ["standard", "express", "pickup"]:
print(method, total(4200, method))
type Shipping = (subtotal: number) => number;
const strategies: Record<string, Shipping> = { // strategies as plain functions in a lookup table
standard: (subtotal) => (subtotal >= 5000 ? 0 : 499),
express: () => 1299,
pickup: () => 0,
};
const total = (subtotal: number, method: string) => subtotal + strategies[method](subtotal);
for (const method of ["standard", "express", "pickup"]) console.log(method, total(4200, method));
import java.util.List;
import java.util.Map;
import java.util.function.IntUnaryOperator;
public class Main {
static final Map<String, IntUnaryOperator> STRATEGIES = Map.of( // strategies as lambdas in a lookup table
"standard", subtotal -> subtotal >= 5000 ? 0 : 499,
"express", subtotal -> 1299,
"pickup", subtotal -> 0);
static int total(int subtotal, String method) { return subtotal + STRATEGIES.get(method).applyAsInt(subtotal); }
public static void main(String[] args) {
for (String method : List.of("standard", "express", "pickup")) System.out.println(method + " " + total(4200, method));
}
}
<?php
$strategies = [ // strategies as closures in a lookup table
"standard" => fn(int $subtotal) => $subtotal >= 5000 ? 0 : 499,
"express" => fn(int $subtotal) => 1299,
"pickup" => fn(int $subtotal) => 0,
];
$total = fn(int $subtotal, string $method) => $subtotal + $strategies[$method]($subtotal);
foreach (["standard", "express", "pickup"] as $method) echo "$method ", $total(4200, $method), "\n";
Key Points
- Interchangeable algorithms behind one interface.
- Context delegates; no if/else.
- Often just a function.
- Pick via lookup table or configuration.
Deep dive: Design Patterns, Explained Simply: Strategy
Explanation
What interviewers look for
A realistic example, the Open/Closed benefit, and recognising functions as strategies.
Follow-up questions
- How is Strategy different from State?
- How do you avoid an if/else just to pick the strategy?
More most-asked Design Patterns questions
- What problem does the Builder pattern solve? Show an example.
- Explain the SOLID principles.
- What is a design pattern, and why are they useful?
- What is the difference between dependency inversion, dependency injection and inversion of control?
- Why is composition often preferred over inheritance?
- What is dependency injection, and what forms does it take?
- Explain the Factory Method pattern and how it differs from a simple factory.
- How do you implement a Singleton, and why is it often considered an anti-pattern?