Bird
0
0

Given the following code snippet for two parking strategies, what will be the output when findSpot(vehicle) is called using NearestParkingStrategy?

medium📝 Analysis Q13 of 15
LLD - Design — Parking Lot System
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'));
A"Nearest spot found"
B"Random spot found"
Cundefined
DError: findSpot not defined
Step-by-Step Solution
Solution:
  1. Step 1: Identify which strategy instance is used

    The code creates an instance of NearestParkingStrategy and calls findSpot.
  2. Step 2: Check method output for that class

    NearestParkingStrategy.findSpot returns "Nearest spot found".
  3. Final Answer:

    "Nearest spot found" -> Option A
  4. Quick Check:

    Instance method output = "Nearest spot found" [OK]
Quick Trick: Instance method output matches class used [OK]
Common Mistakes:
MISTAKES
  • Confusing which strategy instance is created
  • Assuming random strategy is used
  • Expecting undefined or error without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes