BackmediumArraysInfosys

Optimal Product Selection Solution

Problem Statement

You are given a list of products, where each product is an array containing its rating and price. Implement a function that takes this list and a maximum allowed budget as input, and returns the product with the highest rating that is within the budget. If there are multiple products with the highest rating within the budget, return any one of them.

Example 1
Input
[[5, 50], [3, 30], [1, 10], [5, 50]]
Output
[5, 50]

Explanation: Step-by-step: Given the input list of products, we first sort the list in descending order based on the rating. Then, we iterate through the sorted list to find the first product that is within the budget. In this case, the first product with rating 5 and price 50 is within the budget, so we return it.

Example 2
Input
[[10, 100], [8, 80], [6, 60], [4, 40]]
Output
[10, 100]

Explanation: Step-by-step: Given the input list of products, we first sort the list in descending order based on the rating. Then, we iterate through the sorted list to find the first product that is within the budget. In this case, the first product with rating 10 and price 100 is within the budget, so we return it.

Constraints

  • 1 <= number of products <= 1000
  • 1 <= product rating <= 5
  • 1 <= product price <= 1000
  • 1 <= max allowed budget <= 1000
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Optimal Product Selection — Problem Statement & Solution Guide

ArraysMediumGeneral
TimeO(n)
|
SpaceO(1)

Problem Description

You are given a list of products, where each product is an array containing its rating and price. Implement a function that takes this list and a maximum allowed budget as input, and returns the product with the highest rating that is within the budget. If there are multiple products with the highest rating within the budget, return any one of them.

Examples

Example 1

Input

[[5, 50], [3, 30], [1, 10], [5, 50]]

Output

[5, 50]

Explanation: Step-by-step: Given the input list of products, we first sort the list in descending order based on the rating. Then, we iterate through the sorted list to find the first product that is within the budget. In this case, the first product with rating 5 and price 50 is within the budget, so we return it.

Example 2

Input

[[10, 100], [8, 80], [6, 60], [4, 40]]

Output

[10, 100]

Explanation: Step-by-step: Given the input list of products, we first sort the list in descending order based on the rating. Then, we iterate through the sorted list to find the first product that is within the budget. In this case, the first product with rating 10 and price 100 is within the budget, so we return it.

Constraints

  • 1 <= number of products <= 1000
  • 1 <= product rating <= 5
  • 1 <= product price <= 1000
  • 1 <= max allowed budget <= 1000

Optimal Approach & Strategy

The optimized approach is to use a single pass through the list of products to find the product with the highest rating that is within the budget. This approach has a time complexity of O(n), where n is the number of products. It can be implemented using a simple loop that keeps track of the current highest rating and corresponding product.

Brute Force Approach

The brute force approach is to iterate over the list of products and compare each product's rating and price with the current highest rating and budget. This approach has a high time complexity and is not efficient for large inputs. It can be improved by using a more efficient data structure.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function recommendProduct(products, budget) {
    let highestRatedProduct = null;
    let highestRating = -Infinity;
    for (let i = 0; i < products.length; i++) {
        if (products[i][1] <= budget && products[i][0] > highestRating) {
            highestRating = products[i][0];
            highestRatedProduct = products[i];
        } else if (products[i][1] <= budget && products[i][0] === highestRating) {
            highestRatedProduct = products[i];
        }
    }
    return highestRatedProduct;
}

Asked in Top Tech Interviews

Infosys

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.