FastPrepDesign a Food Rating System

Design a Food Rating System

Apple logoApple● MediumFULLTIMEPHONE SCREEN

Problem statement

Design a food rating system. You are given three arrays of length n: foods, cuisines, and ratings. Food foods[i] belongs to cuisine cuisines[i] and starts with rating ratings[i].

Process the list operations in order. Each row is one of:

  • ["changeRating", food, newRating]: set the rating of food to newRating. The rating is written as a decimal string.
  • ["highestRated", cuisine]: report the food with the highest current rating in cuisine. If several foods share that rating, report the one whose name is lexicographically smallest.

Return an array with the answer to every "highestRated" operation, in order. "changeRating" operations add nothing to the result.

Function

runFoodRatings(foods: String[], cuisines: String[], ratings: int[], operations: String[][]) → String[]

Examples

Example 1

foods = ["bibimbap","tteok","japchae","udon","soba","tempura"]cuisines = ["korean","korean","korean","japanese","japanese","japanese"]ratings = [6,9,9,4,7,5]operations = [["highestRated","korean"],["highestRated","japanese"],["changeRating","japchae","3"],["highestRated","korean"],["changeRating","udon","7"],["highestRated","japanese"],["changeRating","soba","2"],["highestRated","japanese"]]return = ["japchae","soba","tteok","soba","udon"]
  • Korean foods are bibimbap (6), tteok (9), and japchae (9). The two 9s tie, and "japchae" is smaller than "tteok", so the first answer is "japchae".
  • Japanese foods are udon (4), soba (7), and tempura (5), so the second answer is "soba".
  • After japchae drops to 3, tteok (9) is the highest Korean food.
  • After udon rises to 7, it ties with soba. "soba" is smaller, so the answer stays "soba".
  • After soba drops to 2, udon (7) is the highest, so the last answer is "udon".

Example 2

foods = ["tacos","burrito","nachos"]cuisines = ["mexican","mexican","mexican"]ratings = [5,5,3]operations = [["highestRated","mexican"],["changeRating","burrito","2"],["highestRated","mexican"],["changeRating","nachos","5"],["highestRated","mexican"]]return = ["burrito","tacos","nachos"]
  • tacos and burrito both have rating 5, and "burrito" is smaller, so the first answer is "burrito".
  • After burrito drops to 2, tacos (5) is the highest.
  • After nachos rises to 5, it ties with tacos and "nachos" is smaller.

Example 3

foods = ["pho"]cuisines = ["vietnamese"]ratings = [1]operations = [["changeRating","pho","100000000"],["highestRated","vietnamese"]]return = ["pho"]

A cuisine with one food always reports that food, whatever its rating.

Constraints

  • 1 <= n <= 500
  • foods.length == cuisines.length == ratings.length == n
  • 1 <= foods[i].length, cuisines[i].length <= 10
  • foods[i] and cuisines[i] contain only lowercase English letters.
  • All names in foods are distinct.
  • 1 <= ratings[i] <= 10^8, and every newRating is also between 1 and 10^8.
  • 1 <= operations.length <= 500
  • A "changeRating" row has 3 entries and names a food in foods. A "highestRated" row has 2 entries and names a cuisine of at least one food.

More Apple problems

See Apple hiring insights
Saved
public String[] runFoodRatings(String[] foods, String[] cuisines, int[] ratings, String[][] operations) {
    // Write your code here.
}
Checking account…