AlgoMaster Logo

Design a Food Rating System

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force

Intuition:

In this approach, we maintain lists to track the food, cuisine, and ratings. Every operation such as adding, removing, or finding the highest rating will require iterating through these lists.

Approach:

  1. Keep a simple list of food items along with their respective cuisines and ratings.
  2. While changing ratings, directly modify the list for that food item.
  3. For finding the highest-rated food in a cuisine, iterate through the full list to find the food item with the highest rating for that cuisine.

Code:

2. HashMap with Sorting

Intuition:

Using HashMaps to store foods and their details allows for faster access and update. However, for finding the highest rated food of a specific cuisine, sorting is still required, which can be optimized further.

Approach:

  1. Use a HashMap to store food details, allowing O(1) access for updates.
  2. Store cuisines in a HashMap with a list of foods to facilitate easy sorting for rating retrieval.

Code:

3. TreeMap with HashMap

Intuition:

Leverage the TreeMap that keeps entries sorted based on keys, which can be used to maintain a sorted order of the ratings while supporting efficient lookup for finding the highest rated meal.

Approach:

  1. Utilize a HashMap for storing food ratings quickly accessible for updates.
  2. For each cuisine, use a TreeMap with the food names as keys, which automatically maintains a sorted order of foods by rating in descending order.
  3. Directly fetch the first entry of the TreeMap for highest-rated food retrieval.

Code: