Frontend School

Find Products in Price Range

DSA
easy

Find Products in Price Range

Easy
Arrays
Sorting
Searching

Overview

At Bigbasket, customers rely on efficient filtering options to navigate our vast product catalog. A common requirement is to quickly find products within a specific price range. Implementing an optimized solution for this not only enhances user experience by providing responsive search results but also reduces server load by processing data effectively.

What you need to do / Task

Implement a function findProductsInPriceRange that takes an array of product prices (integers), a minimum price, and a maximum price. The function should return a new array containing all product prices that are greater than or equal to the minimum price and less than or equal to the maximum price. The returned array must be sorted in ascending order.

Input & output

  • prices: An array of integers representing the prices of various products available in Bigbasket's catalog.
  • minPrice: An integer representing the inclusive lower bound of the desired price range.
  • maxPrice: An integer representing the inclusive upper bound of the desired price range.
  • Returns: An array of integers, sorted in ascending order, containing all product prices from prices that fall within the [minPrice, maxPrice] range.

Clarifications

  • What if no products fall within the range? The function should return an empty array [].
  • Are prices always non-negative? Yes, all prices[i], minPrice, and maxPrice will be non-negative integers.
  • What about minPrice > maxPrice? Assume valid range inputs where minPrice <= maxPrice.

Examples

1

Example 1

Input:

prices = [10, 2, 50, 20, 5, 30], minPrice = 10, maxPrice = 30

Output:

[10,20,30]

Note:

After filtering, prices 10, 20, 30 are within the range [10, 30]. Sorted, they are [10, 20, 30].

2

Example 2

Input:

prices = [1, 5, 10, 15, 20], minPrice = 7, maxPrice = 12

Output:

[10]

Note:

Only price 10 is within the range [7, 12].

3

Example 3

Input:

prices = [100, 200, 300], minPrice = 10, maxPrice = 50

Output:

[]

Note:

No prices fall within the range [10, 50].

Constraints

Constraints & assumptions

  • 1 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^9
  • 0 <= minPrice <= maxPrice <= 10^9
  • The input prices array is not guaranteed to be sorted.

Out of scope

  • Handling non-integer prices or currency formatting.
  • Optimizing for extremely large price ranges that cover almost all products (standard sorting and searching should suffice).

What interviewers evaluate

  • Correctness of the filtering logic based on the specified price range.
  • Efficiency of the solution, particularly in handling large input arrays (e.g., O(N log N) or better).
  • Proper application of sorting and/or searching algorithms.
  • Code clarity, organization, and graceful handling of edge cases.

Loading editor...