FastPrepOptimize Discount Selection Under a Point Budget

Optimize Discount Selection Under a Point Budget

Palantir logoPalantirHardNEW GRADOA
Learn

Problem statement

You are given a shopping cart and a set of item-specific or category-wide percentage discounts. An item discount costs 2 points, a category discount costs 5 points, and you may spend at most 20 points.

Choose a subset of discounts that maximizes the cart's total rounded savings. For each item, apply its selected item discount first and its selected category discount second. Discounts stack multiplicatively, the total discount on one item is capped at 80%, and the final per-item discount is rounded to the nearest cent with exact half-cent ties going to the nearest even integer.

If several selections produce the same maximum savings, prefer the one using fewer points. If points are also equal, prefer the lexicographically smallest ascending list of original discount indices.

Return an integer array whose first value is the total points used and whose remaining values are the selected original discount indices in ascending order.

Function

optimizeDiscounts(itemNames: String[], prices: int[], categories: String[], discountTypes: String[], discountNames: String[], percentOff: int[]) → int[]

Examples

Example 1

itemNames = ["Milk"]prices = [500]categories = ["Dairy"]discountTypes = ["item"]discountNames = ["Milk"]percentOff = [20]return = [2, 0]

Selecting discount 0 costs 2 points and saves 100 cents.

Example 2

itemNames = ["Laptop"]prices = [1000]categories = ["Electronics"]discountTypes = ["item", "category"]discountNames = ["Laptop", "Electronics"]percentOff = [70, 50]return = [7, 0, 1]

Selecting both discounts costs 7 points and reaches the 800-cent cap, which is better than either discount alone.

Example 3

itemNames = ["A"]prices = [100]categories = ["X"]discountTypes = ["item", "category"]discountNames = ["A", "X"]percentOff = [80, 80]return = [2, 0]

Either discount alone reaches the 80-cent cap, and selecting both saves no more. The item discount wins because it uses fewer points.

Constraints

  • 1 ≤ itemNames.length = prices.length = categories.length ≤ 100000.
  • 0 ≤ discountTypes.length = discountNames.length = percentOff.length ≤ 20.
  • 0 ≤ prices[i] ≤ 10^9.
  • 0 ≤ percentOff[i] ≤ 100.
  • Every discount type is exactly item or category.
  • There is at most one item discount per item name and at most one category discount per category.
  • Names and categories are non-empty case-sensitive strings.
  • The maximum rounded total savings fits in a signed 64-bit integer.

More Palantir problems

See Palantir hiring insights
public int[] optimizeDiscounts(String[] itemNames, int[] prices, String[] categories, String[] discountTypes, String[] discountNames, int[] percentOff) {
    // write your code here
}
itemNames["Milk"]
prices[500]
categories["Dairy"]
discountTypes["item"]
discountNames["Milk"]
percentOff[20]
expected[2, 0]
Checking account…