Bird
0
0
LLDsystem_design~10 mins

Parking strategy pattern in LLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the ParkingStrategy interface.

LLD
public interface ParkingStrategy {
    boolean [1](Vehicle vehicle, ParkingLot lot);
}
Drag options to blanks, or click blank then click option'
AparkVehicle
BcanPark
CfindSpot
DreserveSpot
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that imply parking action instead of checking.
2fill in blank
medium

Complete the code to implement a strategy that parks only motorcycles.

LLD
public class MotorcycleParkingStrategy implements ParkingStrategy {
    @Override
    public boolean [1](Vehicle vehicle, ParkingLot lot) {
        return vehicle.getType() == VehicleType.MOTORCYCLE;
    }
}
Drag options to blanks, or click blank then click option'
AcanPark
BreserveSpot
CparkVehicle
DfindSpot
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that do not match the interface.
3fill in blank
hard

Fix the error in the ParkingLot class method to use the strategy correctly.

LLD
public class ParkingLot {
    private ParkingStrategy strategy;

    public ParkingLot(ParkingStrategy strategy) {
        this.strategy = strategy;
    }

    public boolean [1](Vehicle vehicle) {
        if(strategy.canPark(vehicle, this)) {
            // park vehicle logic
            return true;
        }
        return false;
    }
}
Drag options to blanks, or click blank then click option'
AfindSpot
BcanPark
CreserveSpot
DparkVehicle
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the method the same as the strategy's check method.
4fill in blank
hard

Fill both blanks to complete the code for setting and using a parking strategy.

LLD
public class ParkingLot {
    private ParkingStrategy [1];

    public void setStrategy(ParkingStrategy [2]) {
        this.strategy = [2];
    }
}
Drag options to blanks, or click blank then click option'
Astrategy
BparkStrategy
CnewStrategy
DparkingStrategy
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for field and parameter causing shadowing.
5fill in blank
hard

Fill all three blanks to complete the code for applying different parking strategies dynamically.

LLD
public class ParkingManager {
    private ParkingLot lot;

    public ParkingManager(ParkingLot lot) {
        this.lot = lot;
    }

    public void applyStrategy(ParkingStrategy [1]) {
        lot.[2]([3]);
    }
}
Drag options to blanks, or click blank then click option'
Astrategy
BsetStrategy
CapplyStrategy
DchangeStrategy
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names or mismatched parameter names.