Design a Food Rating System

Medium · Design · Array · Hash Table · String · Design · Heap (Priority Queue) · Ordered Set

Problem

Design a rating board for dishes that supports changing a dish's rating and asking for the best dish of a cuisine.

Implement FoodRatings:

Tests replay a call sequence: the first entry names the class and its constructor arguments, each later entry is a method with its arguments, and calls returning nothing report None.

Examples

Input: ops = ["FoodRatings","highestRated","changeRating","highestRated","highestRated"], args = [[["pho","ramen","tacos"],["asian","asian","mexican"],[7,9,5]],["asian"],["pho",9],["asian"],["mexican"]]
Output: [None, 'ramen', None, 'pho', 'tacos']

Ramen leads with 9. After pho rises to 9 the two tie, and "pho" is lexicographically smaller.

Input: ops = ["FoodRatings","changeRating","highestRated"], args = [[["stew","soup"],["home","home"],[4,6]],["soup",1],["home"]]
Output: [None, None, 'stew']

Soup drops from 6 to 1, so stew becomes the best home dish.

Input: ops = ["FoodRatings","changeRating","changeRating","highestRated","changeRating","highestRated"], args = [[["kimchi","miso","sushi"],["korean","japanese","japanese"],[9,12,8]],["sushi",16],["sushi",8],["japanese"],["miso",3],["japanese"]]
Output: [None, None, None, 'miso', None, 'sushi']

Sushi goes up to 16 and back to 8, so miso still leads with 12 until it falls to 3.

Constraints

Approaches

Follow-up

The lazy heap can grow with every change; a sorted set stays at n entries but needs deletion by key. The use cases ask the same questions in one function call: the best dish of each cuisine after a list of changes, and the top k dishes of one cuisine.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…