AlgoMaster Logo
AlgoMasterDesign Route Plannermedium

Design Route Planner

medium

A navigation app can plan the same trip for a driver, cyclist, or pedestrian. Each travel mode estimates time differently, but the planner should not contain a growing chain of formulas. It should validate a request, hand it to the selected strategy, and format the result.

The RoutePlanner context is already implemented in the starter code. Complete only the route strategy family:

  • RouteStrategy defines name() and estimate(distance, traffic).
  • DrivingStrategy returns the name "drive". For a non-zero distance, it estimates distance * 2 + traffic * 3 minutes.
  • CyclingStrategy returns the name "cycle". For a non-zero distance, it estimates distance * 4 + traffic minutes.
  • WalkingStrategy returns the name "walk". It estimates distance * 12 minutes and is unaffected by traffic.
  • Every strategy returns 0 when distance is 0.

The supplied planner starts in driving mode and lets callers replace the active strategy while it is running. It exposes this API:

  • setMode(mode) accepts "drive", "cycle", or "walk". An unknown mode returns false and leaves the current strategy unchanged.
  • currentMode() returns the active strategy's name.
  • estimateMinutes(distance, traffic) validates and delegates a route estimate. A negative distance or a traffic level outside 0 through 10 returns -1 and is not counted.
  • routeSummary(distance, traffic) returns "<mode>: <minutes> min", or "INVALID" for invalid input.
  • planCount() returns the number of valid estimates, including estimates made through routeSummary.

Do not rewrite RoutePlanner. Your strategies must make the provided workflow behave correctly.

Example 1:

Input:

Output:

Explanation: Driving ten kilometres takes 20 base minutes plus 6 minutes for traffic. The valid estimate moves the plan count to 1.

Example 2:

Input:

Output:

Explanation: Cycling uses four minutes per kilometre plus the traffic level, so the estimate is 8 * 4 + 3 = 35 minutes.

Constraints

  • -1 <= distance <= 50
  • -1 <= traffic <= 11
  • At most 100 calls in total are made across all methods.

How the design is graded

needs 7/10 to pass
  • Strategy separation

    Full marks when driving, cycling, and walking are separate classes behind one contract and `estimateMinutes` delegates without naming a mode. Lose points heavily when the planner branches on the mode or contains any travel-time formula.

  • Runtime replacement

    Full marks when `setMode` replaces the active strategy, an unknown mode returns `false` without changing it, and subsequent calls immediately use the replacement. Lose points when the mode is fixed at construction or when a second mode field can drift away from the strategy.

  • Structure and edge cases

    Full marks when each strategy owns both its name and formula, zero distance returns zero, invalid inputs return `-1` or `INVALID` without counting, and `routeSummary` uses the same estimation workflow exactly once. Lose points for printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new RoutePlanner()null
currentMode()"drive"
estimateMinutes(10, 2)26
planCount()1

A new planner drives by default. Ten kilometres takes 20 base minutes plus 6 minutes for traffic, and the completed plan is counted.

Run checks these cases. Submit also runs a larger hidden set.