BackmediumArrays PhonePe

Ecommerce Product Recommendation Solution

Problem Statement

Write a function to recommend products based on their ratings and prices. The function should take in a list of product information, where each product is an array containing the product's rating and price. The function should also take in a maximum allowed budget and return the product with the highest rating that is within the budget. If there are multiple products with the highest rating within the budget, return the product with the lowest price.

Example 1
Input
[[4, 10], [4, 15], [2, 5]]
Output
[[4, 10], [4, 15]]

Explanation: Step-by-step: We iterate through the list of products. For each product, we check if its rating is greater than the current max rating and its price is within the budget. If both conditions are met, we update the max rating and add the product to the result list. Finally, we return the result list.

Example 2
Input
[[5, 10], [5, 15], [3, 5]]
Output
[[5, 10], [5, 15]]

Explanation: Step-by-step: We iterate through the list of products. For each product, we check if its rating is greater than the current max rating and its price is within the budget. If both conditions are met, we update the max rating and add the product to the result list. Finally, we return the result list.

Constraints

  • 1 <= number of products <= 1000
  • 1 <= product rating <= 5
  • 1 <= product price <= 1000
  • 1 <= max allowed budget <= 1000
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free