Frontend School

Target Bundle Price Check

DSA
easy

Target Bundle Price Check

Easy
Dynamic Programming
Knapsack
Arrays

Overview

Razorpay helps millions of businesses accept, process, and disburse payments. One common business operation is creating product bundles or promotions. Merchants often need to determine if a specific combination of products can achieve an exact target price for a promotional bundle. This problem focuses on efficiently solving this calculation.

What you need to do / Task

Implement a function canAchieveTargetPrice that determines if a given targetPrice can be exactly matched by summing a subset of product prices from a provided list. Each product price can be used at most once.

Input & output

Input:

  • productPrices: An array of integers representing the individual prices of available products. Each price is a positive integer.
  • targetPrice: An integer representing the exact price that needs to be achieved for the bundle. targetPrice is a non-negative integer.

Output:

  • A boolean value: true if a subset of productPrices sums up exactly to targetPrice, false otherwise.

Clarifications

  • An empty productPrices list should only return true if targetPrice is 0. Otherwise, it's false.
  • Product prices are positive integers.
  • The order of products in the bundle does not matter.

Examples

1

Example 1

Input:

productPrices = [2, 3, 7, 8], targetPrice = 10

Output:

true

Note:

A bundle with prices [2, 8] sums up to 10.

2

Example 2

Input:

productPrices = [5, 10, 15], targetPrice = 12

Output:

false

Note:

No combination of products from [5, 10, 15] sums up to exactly 12.

3

Example 3

Input:

productPrices = [1], targetPrice = 0

Output:

true

Note:

An empty bundle (no products chosen) sums to 0. This is always achievable.

4

Example 4

Input:

productPrices = [], targetPrice = 5

Output:

false

Note:

No products are available to form a bundle, so 5 cannot be achieved.

Constraints

Constraints & assumptions

  • 1 <= productPrices.length <= 100
  • 1 <= productPrices[i] <= 1000
  • 0 <= targetPrice <= 1000
  • The sum of all product prices will not exceed 100,000.
  • Input arrays will contain only integers.

What interviewers evaluate

  • Correctness and Edge Cases: The solution must correctly handle all valid inputs, including edge cases like empty lists, zero target price, and scenarios where a target is achievable or not.
  • Algorithm Efficiency: The chosen algorithm should be optimal in terms of time and space complexity for the given constraints.
  • Code Readability and Structure: The code should be clean, well-structured, and easy to understand, reflecting good software engineering practices.
  • Problem-Solving Approach: Candidate's ability to break down the problem, identify patterns (e.g., dynamic programming), and articulate their thought process.

Loading editor...