Frontend School

Find Budget-Friendly Popular Pins

DSA
easy

Find Budget-Friendly Popular Pins

Easy
Sorting
Searching
Arrays
Objects

Overview

Pinterest users discover and save a vast array of pins, including product pins. To enhance the user experience and help them find relevant products efficiently, we often need to filter and organize product listings based on various criteria. This problem focuses on identifying popular product pins that fall within a user's specified budget range and presenting them in an organized manner.

What you need to do / Task

Implement a function findBudgetFriendlyPopularPins that takes a list of Pin objects, a minimum price, a maximum price, and a minimum popularity score. The function should return a new list containing only those Pin objects that satisfy all of the following conditions:

  1. The pin's price is greater than or equal to minPrice.
  2. The pin's price is less than or equal to maxPrice.
  3. The pin's popularity score is greater than or equal to minPopularity.

The returned list of filtered pins must be sorted by their price in ascending order. If two pins have the same price, their relative order in the output list does not matter.

Input & output

  • pins: An array of Pin objects. Each Pin object has two properties: price (number) and popularity (number).
  • minPrice: A number representing the minimum acceptable price (inclusive).
  • maxPrice: A number representing the maximum acceptable price (inclusive).
  • minPopularity: A number representing the minimum acceptable popularity score (inclusive).
  • Returns: An array of Pin objects, filtered and sorted as described above.

Clarifications

  • If the input pins array is empty, return an empty array.
  • If no pins match all the specified criteria, return an empty array.
  • All prices and popularity scores are non-negative integers.

Examples

1

Example 1

Input:

pins = [{"price": 20, "popularity": 100}, {"price": 50, "popularity": 200}, {"price": 10, "popularity": 50}], minPrice = 15, maxPrice = 60, minPopularity = 80

Output:

[{"price": 20, "popularity": 100}, {"price": 50, "popularity": 200}]

Note:

Pin (price:10, popularity:50) is excluded because its price (10) is less than minPrice (15) and its popularity (50) is less than minPopularity (80). The remaining pins (20,100) and (50,200) match and are sorted by price.

2

Example 2

Input:

pins = [{"price": 5, "popularity": 10}, {"price": 10, "popularity": 20}], minPrice = 100, maxPrice = 200, minPopularity = 5

Output:

[]

Note:

No pins fall within the specified price range (100-200), so an empty array is returned.

3

Example 3

Input:

pins = [{"price": 30, "popularity": 50}, {"price": 10, "popularity": 120}, {"price": 20, "popularity": 80}], minPrice = 0, maxPrice = 100, minPopularity = 70

Output:

[{"price": 10, "popularity": 120}, {"price": 20, "popularity": 80}]

Note:

Pin (price:30, popularity:50) is excluded because its popularity (50) is less than minPopularity (70). The remaining pins (10,120) and (20,80) match and are sorted by price.

Constraints

Constraints & assumptions

  • 0 <= pins.length <= 10^5
  • 0 <= pin.price <= 10^9
  • 0 <= pin.popularity <= 10^9
  • 0 <= minPrice <= maxPrice <= 10^9
  • 0 <= minPopularity <= 10^9
  • All prices and popularity scores are integers.

Out of scope

  • Error handling for invalid input types (assume valid inputs as per constraints).
  • Handling extremely large numbers beyond standard integer limits.

What interviewers evaluate

  • Correctly filtering pins based on all three criteria (price range and popularity threshold).
  • Efficiently sorting the filtered pins by price.
  • Clarity, readability, and maintainability of the code.
  • Understanding of the time and space complexity of the solution.

Loading editor...