Bird
0
0

In the following code, what is the main issue that prevents the ParkingLot from using different parking strategies correctly?

medium📝 Analysis Q14 of 15
LLD - Design — Parking Lot System
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');
ANo strategy is set before calling park, causing an error
BThe park method should not call findSpot
CThe strategy property should be a list, not a single object
DThe constructor should initialize strategy with a default value
Step-by-Step Solution
Solution:
  1. Step 1: Analyze object initialization and method calls

    The ParkingLot is created with strategy = null. No strategy is set before calling park.
  2. Step 2: Understand consequence of null strategy

    Calling this.strategy.findSpot(vehicle) when strategy is null causes an error.
  3. Final Answer:

    No strategy is set before calling park, causing an error -> Option A
  4. Quick Check:

    Null strategy causes error on method call [OK]
Quick Trick: Always set strategy before use [OK]
Common Mistakes:
MISTAKES
  • Thinking park method logic is wrong
  • Assuming strategy should be a list
  • Ignoring null initialization problem

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes