Filter Restaurants by Vegan-Friendly, Price and Distance - Problem

Given an array restaurants where restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]. You need to filter the restaurants using three filters:

The veganFriendly filter will be either true (meaning you should only include restaurants with veganFriendlyi set to true) or false (meaning you can include any restaurant).

In addition, you have the filters maxPrice and maxDistance which are the maximum values for price and distance of restaurants you should consider respectively.

Return the array of restaurant IDs after filtering, ordered by rating from highest to lowest. For restaurants with the same rating, order them by id from highest to lowest.

For simplicity, veganFriendlyi and veganFriendly take value 1 when true, and 0 when false.

Input & Output

Example 1 — Basic Filtering
$ 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]], veganFriendly = 1, maxPrice = 50, maxDistance = 10
Output: [3,1,5]
💡 Note: Filter vegan-friendly restaurants: [1,4,1,40,10], [3,8,1,30,4], [5,1,1,15,1]. Sort by rating desc then id desc: rating 8 (id=3), rating 4 (id=1), rating 1 (id=5) → [3,1,5]
Example 2 — No Vegan Filter
$ 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]], veganFriendly = 0, maxPrice = 50, maxDistance = 10
Output: [4,3,2,1,5]
💡 Note: Include all restaurants within price/distance limits. Sort by rating desc then id desc: rating 10 (id=4), rating 8 (id=3,2 → 3>2), rating 4 (id=1), rating 1 (id=5)
Example 3 — Strict Distance Filter
$ Input: restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4]], veganFriendly = 0, maxPrice = 50, maxDistance = 3
Output: []
💡 Note: No restaurants within distance 3 (minimum distance is 4), so return empty array

Constraints

  • 1 ≤ restaurants.length ≤ 104
  • restaurants[i].length == 5
  • 1 ≤ idi, ratingi, pricei, distancei ≤ 105
  • 1 ≤ maxPrice, maxDistance ≤ 105
  • veganFriendlyi and veganFriendly are 0 or 1

Visualization

Tap to expand
Filter Restaurants Problem INPUT ID Rate Vegan Price Dist 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 Filters: veganFriendly = 1 maxPrice = 50 maxDistance = 10 ALGORITHM STEPS 1 Apply Vegan Filter Keep only vegan=1 rows IDs: [1, 3, 5] pass 2 Apply Price Filter Keep price <= 50 IDs: [1, 3, 5] all pass 3 Apply Distance Filter Keep distance <= 10 IDs: [1, 3, 5] all pass 4 Sort Results By rating DESC, then ID DESC Before: [1(r4), 3(r8), 5(r1)] Sort by rating: 3(8) > 1(4) > 5(1) After: [3, 1, 5] FINAL RESULT Filtered Restaurants: ID: 3 Rating: 8 | Vegan: OK Price: 30 | Distance: 4 ID: 1 Rating: 4 | Vegan: OK Price: 40 | Distance: 10 ID: 5 Rating: 1 | Vegan: OK Price: 15 | Distance: 1 OUTPUT: [3, 1, 5] Sorted by rating DESC then by ID DESC Key Insight: Use built-in filter() to apply all three conditions (vegan, price, distance) in sequence. Then use sort() with a custom comparator: first by rating (descending), then by ID (descending) for ties. Time Complexity: O(n log n) for sorting | Space Complexity: O(n) for filtered array TutorialsPoint - Filter Restaurants by Vegan-Friendly, Price and Distance | Built-in Filter and Sort Approach
Asked in
DoorDash 25 Uber 20 Amazon 15
15.2K Views
Medium Frequency
~15 min Avg. Time
489 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen