Filter restaurants by vegan flag, max price and max distance, sorted by rating then ID

Quick Overview

Filter a list of restaurants by a vegan-friendly flag, a maximum price and a maximum distance, then return the IDs that remain sorted by rating and then by ID, both from highest to lowest. It tests careful filter conditions, inclusive bounds and sorting on two keys with a tie-break.

Filter restaurants by vegan flag, max price and max distance, sorted by rating then ID

Company: Yelp

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list `restaurants` where `restaurants[i] = [id, rating, vegan_friendly, price, distance]`. Filter the restaurants using three filters. The `vegan_friendly` filter is either 1 (only include restaurants whose own `vegan_friendly` flag is 1) or 0 (include any restaurant). In addition, `max_price` and `max_distance` are the maximum price and the maximum distance of the restaurants you should consider. Return the IDs of the restaurants that pass the filters, ordered by rating from highest to lowest. For restaurants with the same rating, order them by ID from highest to lowest. ### Function Signature ```python def filter_restaurants(restaurants: list[list[int]], vegan_friendly: int, max_price: int, max_distance: int) -> list[int]: ``` ### Rules - Each restaurant is a list of five integers in the order `[id, rating, vegan_friendly, price, distance]`. The vegan flag is 1 for true and 0 for false. - A restaurant passes when `price <= max_price` and `distance <= max_distance`, and, if the `vegan_friendly` argument is 1, its own vegan flag is also 1. Both limits are inclusive. - Order the passing restaurants by rating descending, then by ID descending. IDs are distinct, so the order is unique. - If no restaurant passes, return an empty list. ### Constraints - `1 <= len(restaurants) <= 10^4` - `len(restaurants[i]) == 5` - `1 <= id, rating, price, distance <= 10^5` - The `vegan_friendly` argument and every restaurant's vegan flag are 0 or 1. - `1 <= max_price, max_distance <= 10^5` - All IDs are distinct. ### Examples ```text Input: restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], vegan_friendly = 1, max_price = 50, max_distance = 10 Output: [3,1,5] ``` Only restaurants 1, 3 and 5 are vegan-friendly, and all three are within the price and distance limits (restaurant 1 sits exactly at distance 10, which is allowed). Their ratings are 4, 8 and 1, so the order is 3, 1, 5. ```text Input: restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], vegan_friendly = 0, max_price = 50, max_distance = 10 Output: [4,3,2,1,5] ``` With no vegan filter, every restaurant passes the price and distance limits. Restaurant 4 has the top rating of 10, restaurants 2 and 3 tie at 8 so the higher ID 3 goes first, and then come restaurant 1 (rating 4) and restaurant 5 (rating 1). ```text Input: restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], vegan_friendly = 0, max_price = 30, max_distance = 3 Output: [4,5] ``` Restaurants 1 and 2 cost more than 30, and restaurant 3 costs exactly 30 but is at distance 4. Restaurants 4 and 5 remain, and rating 10 comes before rating 1.

Overview: Filter a list of restaurants by a vegan-friendly flag, a maximum price and a maximum distance, then return the IDs that remain sorted by rating and then by ID, both from highest to lowest. It tests careful filter conditions, inclusive bounds and sorting on two keys with a tie-break.

Yelp logo
Yelp
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list restaurants where restaurants[i] = [id, rating, vegan_friendly, price, distance]. Filter the restaurants using three filters.

The vegan_friendly filter is either 1 (only include restaurants whose own vegan_friendly flag is 1) or 0 (include any restaurant). In addition, max_price and max_distance are the maximum price and the maximum distance of the restaurants you should consider.

Return the IDs of the restaurants that pass the filters, ordered by rating from highest to lowest. For restaurants with the same rating, order them by ID from highest to lowest.

Function Signature

def filter_restaurants(restaurants: list[list[int]], vegan_friendly: int, max_price: int, max_distance: int) -> list[int]:

Rules

  • Each restaurant is a list of five integers in the order [id, rating, vegan_friendly, price, distance] . The vegan flag is 1 for true and 0 for false.
  • A restaurant passes when price <= max_price and distance <= max_distance , and, if the vegan_friendly argument is 1, its own vegan flag is also 1. Both limits are inclusive.
  • Order the passing restaurants by rating descending, then by ID descending. IDs are distinct, so the order is unique.
  • If no restaurant passes, return an empty list.

Constraints

  • 1 <= len(restaurants) <= 10^4
  • len(restaurants[i]) == 5
  • 1 <= id, rating, price, distance <= 10^5
  • The vegan_friendly argument and every restaurant's vegan flag are 0 or 1.
  • 1 <= max_price, max_distance <= 10^5
  • All IDs are distinct.

Examples

Input: restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], vegan_friendly = 1, max_price = 50, max_distance = 10
Output: [3,1,5]

Only restaurants 1, 3 and 5 are vegan-friendly, and all three are within the price and distance limits (restaurant 1 sits exactly at distance 10, which is allowed). Their ratings are 4, 8 and 1, so the order is 3, 1, 5.

Input: restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], vegan_friendly = 0, max_price = 50, max_distance = 10
Output: [4,3,2,1,5]

With no vegan filter, every restaurant passes the price and distance limits. Restaurant 4 has the top rating of 10, restaurants 2 and 3 tie at 8 so the higher ID 3 goes first, and then come restaurant 1 (rating 4) and restaurant 5 (rating 1).

Input: restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], vegan_friendly = 0, max_price = 30, max_distance = 3
Output: [4,5]

Restaurants 1 and 2 cost more than 30, and restaurant 3 costs exactly 30 but is at distance 4. Restaurants 4 and 5 remain, and rating 10 comes before rating 1.