Frontend School

Cheapest Available Menu Items

DSA
easy

Cheapest Available Menu Items

Easy
Arrays
Sorting
Filtering

Overview

At Zomato, presenting relevant and attractive options to users is crucial for a great dining experience. One common scenario is highlighting the most affordable dishes from a restaurant that are currently available. This helps users quickly find budget-friendly choices and encourages more orders, especially during peak hours or for quick selections. This problem focuses on efficiently identifying and presenting these top cheapest items.

What you need to do / Task

Your task is to implement a function that takes a list of menu items and an integer N, and returns the N cheapest menu items that are currently marked as available.

Specifically, you must:

  1. Filter the menuItems array to include only those items where available is true.
  2. Sort the filtered available items by their price in ascending order.
  3. Return the first N items from the sorted list. If there are fewer than N available items, return all of them.

Input & output

Function Signature: function getCheapestAvailableItems(menuItems: MenuItem[], N: number): MenuItem[]

Input Parameters:

  • menuItems: An array of MenuItem objects. Each MenuItem object has the following structure:
    • id: string - A unique identifier for the menu item.
    • name: string - The name of the dish.
    • price: number - The price of the dish.
    • available: boolean - Indicates whether the dish is currently available.
  • N: number - The maximum number of cheapest items to return.

Return Value:

  • An array of MenuItem objects, sorted by price (ascending), representing up to N cheapest available items. If no items are available or N is 0, an empty array should be returned.

Clarifications

  • Empty Input: If menuItems is an empty array, the function should return an empty array.
  • N is zero: If N is 0, the function should return an empty array.
  • Tie-breaking: If multiple items have the same price, their relative order among themselves in the output array does not matter, as long as they appear before more expensive items.

Examples

1

Example 1

Input:

menuItems = [{"id": "1", "name": "Burger", "price": 150, "available": true}, {"id": "2", "name": "Fries", "price": 80, "available": true}, {"id": "3", "name": "Coke", "price": 50, "available": false}, {"id": "4", "name": "Pizza", "price": 300, "available": true}], N = 2

Output:

[{"id": "2", "name": "Fries", "price": 80, "available": true}, {"id": "1", "name": "Burger", "price": 150, "available": true}]

Note:

Coke is unavailable. Among the available items (Fries, Burger, Pizza), Fries (80) and Burger (150) are the two cheapest.

2

Example 2

Input:

menuItems = [{"id": "1", "name": "Salad", "price": 200, "available": false}, {"id": "2", "name": "Soup", "price": 100, "available": true}, {"id": "3", "name": "Bread", "price": 50, "available": false}], N = 3

Output:

[{"id": "2", "name": "Soup", "price": 100, "available": true}]

Note:

Only Soup is available. Since N=3 but only 1 item is available, the function should return the single available item.

3

Example 3

Input:

menuItems = [], N = 5

Output:

[]

Note:

The input menuItems array is empty, so an empty array is returned.

4

Example 4

Input:

menuItems = [{"id": "1", "name": "Pasta", "price": 250, "available": true}, {"id": "2", "name": "Garlic Bread", "price": 120, "available": true}], N = 0

Output:

[]

Note:

Since N is 0, no items should be returned.

Constraints

Constraints & assumptions

  • 0 <= menuItems.length <= 10^5
  • 0 <= N <= menuItems.length
  • menuItems[i].id is a unique string.
  • menuItems[i].name is a string.
  • 1 <= menuItems[i].price <= 1000 (prices can be integers or floats).
  • menuItems[i].available is a boolean.
  • The solution should be efficient enough to handle up to 10^5 menu items within typical time limits.

What interviewers evaluate

  • Correctness: Does the solution accurately filter, sort, and slice the menu items as per the requirements?
  • Efficiency: Is the chosen algorithm optimal in terms of time and space complexity for the given constraints?
  • Code Quality: Is the code clean, readable, and well-structured?
  • Edge Case Handling: Does the solution correctly handle scenarios like empty input arrays, N=0, or fewer available items than N?

Loading editor...