Pizza Shop (Google Tokyo)
Learn this problemProblem statement
A pizza shop offers n pizzas along with m toppings. A customer plans to spend around x coins. The customer should order exactly one pizza, and may order zero, one or two toppings. Each topping may be ordered only once.
Given the lists of prices of available pizzas and toppings, what is the price closest to x of possible orders? Here, a price is said to be closer to x when the difference from x is smaller. Note the customer is allowed to make an order that costs more than x.
Function
closestCost(pizzas: int[], toppings: int[], x: int) → int
Complete the function closestCost in the editor.
closestCost has the following parameters:
- 1.
int[] pizzas: an array of integers representing the prices of pizzas - 2.
int[] toppings: an array of integers representing the prices of toppings - 3.
int x: the budget in coins
Returns
int: the price closest to x of possible orders
Examples
Example 1
pizzas = [800, 850, 900]toppings = [100, 150]x = 1000return = 1000The customer can spend exactly 1000 coins (two possible orders).
Example 2
pizzas = [850, 900]toppings = [200, 250]x = 1000return = 1050The customer may make an order more expensive than 1000 coins.
Example 3
pizzas = [1100, 900]toppings = [200]x = 1000return = 900The customer should prefer 900 (lower) over 1100 (higher).
Example 4
pizzas = [800, 800, 800, 800]toppings = [100]x = 1000return = 900The customer may not order 2 same toppings to make it 1000.
Constraints
- Customer's budget:
1 <= x <= 10000 - Number of pizzas:
1 <= n <= 10 - Number of toppings:
0 <= m <= 10 - Price of each pizza:
1 <= pizzas[i] <= 10000 - Price of each topping:
1 <= toppings[i] <= 10000 - The total price of all toppings does not exceed
10000.
More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026