FastPrepBasic Item Discounts

Basic Item Discounts

Palantir logoPalantirEasyNEW GRADOA
Learn

Problem statement

You are given a shopping cart and a set of item-specific percentage discounts. Item prices are expressed in integer cents.

For every item, apply the discount whose name matches the item's name. A discount applies to every cart item with that name. Ignore discounts whose names do not match any item.

Compute and return a three-element array containing:

  1. the subtotal, which is the sum of all original prices;
  2. the total discount, which is the sum of the rounded discount amounts; and
  3. the final total, which is the subtotal minus the total discount.

For each item, compute price * percentOff / 100 without intermediate rounding, then round the final discount to the nearest cent. An exact half-cent tie rounds to the nearest even integer, matching Python round().

Function

calculateItemDiscountTotals(itemNames: String[], prices: int[], discountNames: String[], percentOff: int[]) → long[]

Examples

Example 1

itemNames = ["Milk", "Bread"]prices = [500, 300]discountNames = ["Milk"]percentOff = [10]return = [800, 50, 750]

The subtotal is 800 cents. Milk receives a 50-cent discount, while Bread has no matching discount.

Example 2

itemNames = ["A", "B"]prices = [105, 115]discountNames = ["A", "B"]percentOff = [10, 10]return = [220, 22, 198]

The raw discounts are 10.5 and 11.5 cents. Ties-to-even rounding produces 10 and 12 cents.

Example 3

itemNames = ["Apple"]prices = [399]discountNames = ["Orange"]percentOff = [25]return = [399, 0, 399]

The only discount is unmatched, so it is ignored.

Constraints

  • 1 ≤ itemNames.length = prices.length ≤ 100000.
  • 0 ≤ discountNames.length = percentOff.length ≤ 100000.
  • 0 ≤ prices[i] ≤ 10^9.
  • 0 ≤ percentOff[i] ≤ 100.
  • Item and discount names are non-empty case-sensitive strings.
  • There is at most one discount for each discount name.
  • The subtotal and returned totals fit in a signed 64-bit integer.

More Palantir problems

See Palantir hiring insights
public long[] calculateItemDiscountTotals(String[] itemNames, int[] prices, String[] discountNames, int[] percentOff) {
    // write your code here
}
itemNames["Milk", "Bread"]
prices[500, 300]
discountNames["Milk"]
percentOff[10]
expected[800, 50, 750]
Checking account…