FastPrepPizza Price Calculator

Pizza Price Calculator

Microsoft logoMicrosoft● EasyNEW GRADONSITE INTERVIEW

Problem statement

Write a function that works out the price of a pizza from its size, its toppings, and whether it has a premium crust.

The price follows these rules:

  • size sets the base price: small is 5, medium is 10, and large is 15. The name must match exactly.
  • Each entry in toppings adds 2, even when the same topping appears more than once. toppings can be empty.
  • When premiumCrust is true, add 5.

Return the price. If size is not one of the three names, return -1.0.

Function

calculatePizzaPrice(size: String, toppings: String[], premiumCrust: boolean) → double

Examples

Example 1

size = "medium"toppings = ["mushroom","onion"]premiumCrust = truereturn = 19.0

A medium pizza is 10, two toppings add 4, and the premium crust adds 5: 10 + 4 + 5 = 19.

Example 2

size = "small"toppings = ["pepper"]premiumCrust = falsereturn = 7.0

A small pizza is 5, one topping adds 2, and there is no premium crust: 5 + 2 = 7.

Example 3

size = "large"toppings = ["ham","olive","basil","corn"]premiumCrust = truereturn = 28.0

A large pizza is 15, four toppings add 8, and the premium crust adds 5: 15 + 8 + 5 = 28.

Example 4

size = "huge"toppings = ["ham"]premiumCrust = truereturn = -1.0

huge is not one of the three size names, so the answer is -1.0, whatever else was ordered.

Constraints

  • 0 <= size.length <= 10
  • size contains only printable ASCII characters.
  • 0 <= toppings.length <= 100
  • 1 <= toppings[i].length <= 20
  • toppings[i] contains only lowercase English letters.
  • premiumCrust is true or false.

More Microsoft problems

See Microsoft hiring insights
Saved
public double calculatePizzaPrice(String size, String[] toppings, boolean premiumCrust) {
    // Write your code here.
}
Checking account…