Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the Parking Strategy Pattern?
It is a design approach that helps decide how to park vehicles in a parking lot using different strategies, like parking nearest to the entrance or in the largest free space.
Click to reveal answer
intermediate
Why use the Strategy Pattern in parking systems?
It allows changing parking rules easily without changing the main system. For example, switching from first-come-first-served to reserved parking without big code changes.
Click to reveal answer
beginner
Name two common parking strategies.
1. Nearest Spot First: Park the vehicle in the closest available spot to the entrance. 2. Largest Spot First: Park the vehicle in the largest available spot to fit bigger vehicles.
Click to reveal answer
intermediate
How does the Parking Strategy Pattern improve scalability?
By separating parking logic into strategies, the system can add new parking rules without affecting existing code, making it easier to grow and maintain.
Click to reveal answer
intermediate
What role does the Context play in the Parking Strategy Pattern?
The Context is the parking lot system that uses a chosen strategy to decide where to park vehicles. It delegates the decision to the strategy object.
Click to reveal answer
What does the Parking Strategy Pattern help with?
AFixing broken cars
BBuilding physical parking lots
CChoosing different ways to park vehicles
DCalculating parking fees
✗ Incorrect
The pattern focuses on how to decide parking spots using different strategies.
Which of these is NOT a benefit of using the Parking Strategy Pattern?
AAutomatically cleans the parking lot
BEasier to add new parking rules
CImproves system flexibility
DSeparates parking logic from main system
✗ Incorrect
Cleaning the parking lot is unrelated to the strategy pattern.
In the Parking Strategy Pattern, what is the 'Context'?
AThe parking lot entrance
BA vehicle looking for parking
CA parking ticket machine
DThe parking lot system using a strategy
✗ Incorrect
The Context is the system that applies the chosen parking strategy.
Which strategy might park a vehicle in the closest spot to the entrance?
ALast Spot First
BNearest Spot First
CRandom Spot
DLargest Spot First
✗ Incorrect
Nearest Spot First chooses the closest available spot.
How does the Parking Strategy Pattern help when parking rules change?
ABy changing the strategy without rewriting the whole system
BBy rebuilding the parking lot
CBy hiring more parking attendants
DBy increasing parking fees
✗ Incorrect
The pattern allows swapping strategies easily to adapt to new rules.
Explain the Parking Strategy Pattern and how it helps manage parking decisions.
Think about how different parking rules can be swapped easily.
You got /3 concepts.
Describe the roles of Context and Strategy in the Parking Strategy Pattern with an example.
Imagine the system choosing where to park based on a rule.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using the Parking Strategy Pattern in a parking lot system?
easy
A. To store vehicle details in a database
B. To manage payment processing for parking fees
C. To allow different algorithms for finding parking spots without changing the main system
D. To control the physical gates of the parking lot
Solution
Step 1: Understand the role of strategy pattern
The strategy pattern lets you swap different algorithms easily without changing the main code.
Step 2: Apply this to parking system context
In parking, it means you can change how spots are found without rewriting the whole system.
Final Answer:
To allow different algorithms for finding parking spots without changing the main system -> Option C
interface ParkingStrategy { findSpot(vehicle): Spot; } correctly uses interface with method signature. Others are class, function, or object, not interface.
Final Answer:
interface ParkingStrategy { findSpot(vehicle): Spot; } -> Option D
Quick Check:
Interface = method signature only [OK]
Hint: Interface defines method signatures only [OK]
Common Mistakes:
Using class instead of interface for strategy
Defining functions outside interface context
Confusing object creation with interface definition
3. Given the following code snippet for two parking strategies, what will be the output when findSpot(vehicle) is called using NearestParkingStrategy?
class NearestParkingStrategy {
findSpot(vehicle) {
return "Nearest spot found";
}
}
class RandomParkingStrategy {
findSpot(vehicle) {
return "Random spot found";
}
}
const strategy = new NearestParkingStrategy();
console.log(strategy.findSpot('car'));
medium
A. "Nearest spot found"
B. "Random spot found"
C. undefined
D. Error: findSpot not defined
Solution
Step 1: Identify which strategy instance is used
The code creates an instance of NearestParkingStrategy and calls findSpot.
Hint: Instance method output matches class used [OK]
Common Mistakes:
Confusing which strategy instance is created
Assuming random strategy is used
Expecting undefined or error without reason
4. In the following code, what is the main issue that prevents the ParkingLot from using different parking strategies correctly?
class ParkingLot {
constructor() {
this.strategy = null;
}
setStrategy(strategy) {
this.strategy = strategy;
}
park(vehicle) {
return this.strategy.findSpot(vehicle);
}
}
const lot = new ParkingLot();
lot.park('car');
medium
A. No strategy is set before calling park, causing an error
B. The park method should not call findSpot
C. The strategy property should be a list, not a single object
D. The constructor should initialize strategy with a default value
Solution
Step 1: Analyze object initialization and method calls
The ParkingLot is created with strategy = null. No strategy is set before calling park.
Step 2: Understand consequence of null strategy
Calling this.strategy.findSpot(vehicle) when strategy is null causes an error.
Final Answer:
No strategy is set before calling park, causing an error -> Option A
Quick Check:
Null strategy causes error on method call [OK]
Hint: Always set strategy before use [OK]
Common Mistakes:
Thinking park method logic is wrong
Assuming strategy should be a list
Ignoring null initialization problem
5. You want to design a parking system that supports multiple vehicle types (car, bike, truck) and different parking strategies (nearest, random, reserved). Which design approach best uses the Parking Strategy Pattern to handle this complexity?
hard
A. Create separate parking lot classes for each vehicle type and hardcode the strategy inside each
B. Use a single ParkingLot class with a strategy interface; implement different strategies and select based on vehicle type at runtime
C. Store all vehicles in one list and assign spots randomly without strategy pattern
D. Use global variables to track vehicle types and apply strategies in procedural code
Solution
Step 1: Identify need for flexibility and scalability
Supporting multiple vehicle types and strategies requires flexible design without code duplication.
Step 2: Apply strategy pattern with runtime selection
Using one ParkingLot class with strategy interface allows swapping strategies dynamically based on vehicle type.
Step 3: Evaluate other options
Create separate parking lot classes for each vehicle type and hardcode the strategy inside each duplicates code, C ignores strategy pattern, D uses poor global state management.
Final Answer:
Use a single ParkingLot class with a strategy interface; implement different strategies and select based on vehicle type at runtime -> Option B
Quick Check:
Strategy pattern + runtime selection = best design [OK]
Hint: Use one class + strategy interface + runtime choice [OK]