AlgoMaster Logo
AlgoMasterSimplify a Delivery Feeeasy

Simplify a Delivery Fee

easy

Design and refactor a DeliveryFee calculator for a local courier service.

The fee rules are:

  • a distance of 0 or less is invalid;
  • distances from 1 through 10 km have a base fee of 50;
  • distances from 11 through 50 km have a base fee of 100;
  • distances above 50 km have a base fee of 160;
  • express delivery adds 40; and
  • membership subtracts 20.

Implement String quote(int distanceKm, boolean express, boolean member). It returns "INVALID_DISTANCE" for an invalid distance. Otherwise, it returns "FEE: <amount>".

The legacy starter turns the distance into several boolean flags, later converts those flags back into a fee, and nests every combination of express and membership. It also gets the two tier boundaries wrong.

Refactor the method into a calculation that reads in the same order as the requirements.

Example 1:

Input:

Output:

Explanation: Both upper bounds are inclusive. Express and membership then adjust the chosen base fee independently.

Example 2:

Input:

Output:

Explanation: Once invalid input is handled, every valid quote is just a base fee followed by zero, one, or two adjustments.

Constraints

  • -10^6 <= distanceKm <= 10^6
  • At most 100 calls will be made to quote.

Starter Code

The starter compiles, but the extra flags and nested combinations obscure two off-by-one errors.

How the design is graded

needs 7/10 to pass
  • Straight-line calculation

    Full marks when quote uses an early invalid-distance return, one clear distance-tier decision and two direct adjustments. Lose points for classification flags, encoded keys, lookup matrices, scoring systems or deeply nested combinations.

  • Correct boundaries

    Full marks when 10 km uses the 50 fee, 50 km uses the 100 fee and only larger distances use 160. Lose points for off-by-one boundaries or for applying member and express adjustments inconsistently.

  • Focused implementation

    Full marks for one stateless class with named fee constants or equally clear literals. Lose points for helper classes, mutable quote state, unnecessary configuration or 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 DeliveryFee()null
quote(10, false, false)"FEE: 50"
quote(50, true, true)"FEE: 120"

The two inclusive tier boundaries are where the legacy flag-based classification goes wrong.

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