Paypal Hackerrank 2023 | Products and Discounts | Software Engineer

I am typing this question from memory so it may not be exact. Ask any clarifications in the comments.

Question: Products and discounts
Find the minimum amount needed to purchase all products.

Inputs:

  1. 2D products array.

    1. It can have N no of products and each product can have 0 or more discount tags. Discount tags are given in the discount array which is the second input.
    2. Format: [[<product_price>, <name of discount tag 1>, < name of discount tag 2>, ...]]
    3. Examples: [["product1_price", "d1", "d2" ,"d3"]. ["product2_price", "EMPTY", "d2"], ["product3_price"]]
    4. Like seen in example, "EMPTY" could also mean null - it is not a tag. We have to ignore "EMPTY" tags. As seen for product3, we can have 0 discount tags too. There can be more than 1 "EMPTY" tag for a product.
    5. All elements are strings - even the product price.
    6. Product price can be between "1" and "100000". No fractions - all whole numbers.
    7. There can be a maximum of 100 products in the 2D array.
    8. Note that the product price given here in the original price of the product.
  2. 2D discounts array.

    1. These are discounts that can be applied to each product and each 1D array is exactly of size 3: in the format: [<discount_tag_name>, <discount_type>, <discount_amount>].
    2. Example: [["d1", 0, "500"], ["d2", 1, "40"], ["d3", 2, "200"]].
    3. Each discount tag has a unique name.
    4. Discount Type meaning:
      1. Type "0": Fixed price after discount is given in the array. In given example, for "d1" we have 0 type. This means the price of the product is 500 if we apply d1 discount. Original price can be 1000 or whatever. For type 0, the final price is the value found in the 3rd position in the discount tag array. So, no need to refer to the original price in the product array.
      2. Type "1": Percentage discount. For "d2", we have type 1 --> we need to to apply 40% discount to the original price of the product. Get original price from product array.
      3. Type "2": Discount amount is subtracted. In "d3", we have 200 as discount amount. We need to subtract this amount from the original price.
    5. There can be a maximum of 100 different discount tags per test case.

For product 1, we have 3 tags : d1, d2 and d3. We have to apply each discount based on the discount type and find the least value. For product 2, we ignore the empty tag and apply the d2 discount and take that value. For product 3, we do not apply any discounts and use the original price.

For each product, we have to find the minimum value that we can get after applying only 1 discount. If there are more than 1 discount tags for a product, we need to find which tag will give us the least final amount for that product and take that value. We cannot apply more than 1 discounts for a product. We have to find the best discount to apply for each product.

Obviously, for each product, we can only apply the discounts for it given in the product array. If there are no discounts, or if there only "EMPTY" tags for it, we simply take the original price as the best price for that product.

Fins such lowest price for each product and add them all up and return the final amount. Since type 1 discounts can make the final price float, round them to the nearest integer before adding to total amount --> MUST DO as it is a condition given in the problem statement.

Note:

  1. Note that all inputs are strings including price, discount amount, discount type.
  2. In java, both 2D arrays were given as 2D arraylists: List<List< String >> products, List<List< String >> discounts.
  3. There can be a maximum of 100 products with each product containing 0 or more tags. So each 1D array can be of different size.
  4. In discount array, each 1D array is of size 3 and there can be a maximum of 100 discounts.
  5. ASSUME that applying a discount always gives a lesser value than the original price.
  6. Covert to integer before adding to total value. Final output is a single integer.
  7. Can finally apply only 1 discount to a product. Find the best discount to apply. Find minimum value by applying each discount one by one. Cannot apply one discount over another discount. Always apply discount to original price.

Output: Minimum total amount of money needed to buy all the products. It is an integer.

I passed 5 test cases but all other test cases failed. Not sure what the corner cases were. Did not get TLE so my guess is either I made a mistake in rounding the float values or missed some corner case.

Comments (1)