Feature #2: Suggest Items for Special Offer
Explore how to implement a feature that suggests sets of three products totaling $200 for special offers using Kotlin. Learn to apply hashing and sorting to find valid product triplets efficiently, reflecting real-world coding challenges in Amazon's shopping context.
We'll cover the following...
Description
In this scenario, Amazon held a lucky draw contest and the customers who won, have been given a $200 shopping credit. The restriction placed by Amazon is that the customers can only buy up to three products. Now, we want to help the customer by suggesting a list of triplets that contain products worth $200. In other words, a triplet will be a package deal containing three products that sum up to $200, and we want to suggest as many triplets as possible. To implement this feature, you will have access to a list of products that the customer is likely to buy. These products will include products from the person’s wishlist and other products based on previous purchases.
Let’s say we are given a list of numbers containing the prices of products that the customer is likely to buy: {100, 75, 150, 200, 50, 65, 40, 30, 15, 25, 60}. In this example, the following triplets sum up to 200: {25, 100, 75}, {40, 100, 60}, {60, 75, 65}. Therefore, these are the products we will suggest to the customer. Notice that a product can be part of multiple package deals. Your program should return the prices of these products in a list, such as: {{25, 100, 75}, {40, 100, 60}, {60, 75, 65}}. The order of product prices in the final output does not matter.
Solution
This feature can also be implemented by using a dictionary. For every item with price, ...