Design a Food Rating System

MediumHeapheap

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Design a system that tracks ratings for foods and can report the best food of a given cuisine. - FoodRatings(foods, cuisines, ratings) initializes the system: food foods[i] belongs to cuisine cuisines[i] and starts with rating ratings[i]. Food names are distinct. - changeRating(food, newRating) sets that food's rating to newRating. - highestRated(cuisine) returns the name of the food with the **highest rating** in that cuisine. If several foods tie, return the **lexicographically smallest** name. changeRating is only called with existing foods, and highestRated only with cuisines that have at least one food.

Examples

Input: ["FoodRatings","highestRated","changeRating","highestRated","changeRating","highestRated","highestRated"] [[["pho","ramen","tacos","sushi"],["vietnamese","japanese","mexican","japanese"],[8,9,7,9]],["japanese"],["ramen",5],["japanese"],["sushi",4],["japanese"],["vietnamese"]]
Output: [null,"ramen",null,"sushi",null,"ramen","pho"]
`ramen` and `sushi` tie at 9 and `ramen` is smaller. After ramen drops to 5, sushi leads; after sushi drops to 4, ramen leads again.
Input: ["FoodRatings","highestRated","changeRating","highestRated","changeRating","highestRated"] [[["b","a"],["x","x"],[3,1]],["x"],["a",3],["x"],["a",2],["x"]]
Output: [null,"b",null,"a",null,"b"]
Once `a` ties `b` at 3, the smaller name wins. Lowering `a` hands the lead back to `b`.

Constraints

  • `1 <= foods.length <= 2 * 10^4`, and `foods.length == cuisines.length == ratings.length`.
  • Names and cuisines have 1 to 10 lowercase English letters; all food names are distinct.
  • `1 <= ratings[i], newRating <= 10^8`
  • At most 2 * 10^4 calls in total.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More heap problems