Find Lowest Price Amazon OA Solution

Find Lowest Price Amazon OA Solution

An e-commerce company is currently celebrating ten years in business. They are having a sale to
honor their privileged members, those who have been using their services for the past five years.

They receive the best discounts indicated by any discount tags attached to the product. Determine
the minimum cost to purchase all products listed.

As each potential price is calculated, round it to the nearest integer before adding it to the total.

Return the cost to purchase all items as an integer.

There are three types of discount tags:

  • Type 0: discounted price, the item is sold for a given price.
  • Type 1: percentage discount, the customer is given a fixed percentage discount from the retail price.
  • Type 2: fixed discount, the customer is given a fixed amount off from the retail price.

Example
products = [[’10’, d0′, ‘d1′], [’15’, ‘EMPTY’, ‘EMPTY’], [’20’, ‘d1’, ‘EMPTY’]]
discounts = [[‘d0’, ‘1, ’27’], [‘d1’, ‘2’, ‘5’]]

The product array element are in the form [‘price’, ‘tag1’, ‘tag2’…..tagm-1.]
There maybe zero or more discount codes associated with the product.
Discount tags in product array maybe ‘EMPTY’ which is the same as NULL value.
The Discount Array elements are in form[‘tag’, ‘type’, ‘amount’]

1 If a privileged member buys product 1 listed at a price of 10 with two discount available:

Under discount d0 of type 1 , the discounted price is 10- 10*0.27 = 7.30, round 7. Under discount of d1 of type 2, the discounted price price is 10-5 = 5

The price to purchase the product :
1 is the lowest of the two, or 5 in this case.
The second product is priced at 15 because there are no discounts available
The third product is priced at 20 .Using discount tag d1 of type 2, the discount price is 20-5 = 15
the total price to purchase the three item is 5+15+15 = 35.

NOTES: Not all items will have the Maximum number of tags. Empty tags may just not exist in input or they maybe filled with the string Empty.
These are equivalent as demonstrated in the example above

Function Description

Complete the function. Find LowestPrice in the editor below.
[string] products[n][m]: a 2D array of product descriptors as strings:price followed up by up to m-1 discount tags.
[string] discounts[3] : a 2D array of tag descriptors as string: tag, type amount. int: the total amount paid in for all listed products , discounts to privileged members pricing.

Constraints:
1 < n, m ,d < 1000 // meant less or equal to.

Find Lowest Price Amazon OA Solution
Find Lowest Price Amazon OA Solution

SOLUTION

Program: Find Lowest Price Amazon OA Solution in Python

def findLowestPrice(products,discounts):
    def computePrice(map,price,discName):
        if (discName is None or discName=="EMPTY"):
            return price
        print(map,price,discName)
        d =map[discName]
        if (d[0][0] == 0): return d[0][1]
        elif (d[0][0]  == 1) : return round((1 - (d[0][1] / 100)) * price)
        else :return price - d[0][1]
    from collections import defaultdict
    map=defaultdict(list)
    for d in discounts:
        map[d[0]].append([int(d[1]),int(d[2])])
    total=0
    for prod in products:
        price = int(prod[0])
        newPrice = price
        for i in range(1,len(prod)):
            newPrice = min(newPrice, computePrice(map, price, prod[i]))
        total += newPrice
    return total
prod=[['10','sale','january-sale'],['200','sale','EMPTY']]
disc=[['sale','0','10'],['january-sale','1','10']]
print(findLowestPrice(prod,disc))

Program: Find Lowest Price Amazon OA Solution in Javascript

function getDiscountedPriceMap(price, discounts) {
	const tagPriceMap = new Map();
	for (let discount of discounts) {
		const [tag, type, amountStr] = discount;
		const amount = Number(amountStr);
		let discountedPrice;
		if (type === "1") {
			discountedPrice = price - (price * amount) / 100;
		} else if (type === "2") {
			discountedPrice = price - amount;
		} else {
			discountedPrice = amount;
		}
		tagPriceMap.set(tag, discountedPrice);
	}
	return tagPriceMap;
}
const sum = products
	.map((product) => {
		const [price, ...tags] = product;
		let lowestPrice = Infinity;
		const disMap = getDiscountedPriceMap(price, discounts);
		for (let tag of tags) {
			lowestPrice = Math.min(
				lowestPrice,
				tag === "empty" ? price : disMap.get(tag)
			);
		}
		return lowestPrice;
	})
	.reduce((a, b) => a + b, 0);
console.log(sum);

Program: Find Lowest Price Amazon OA Solution in C++

#include <iostream>
#include <utility>
#include <vector>
#include <unordered_map>

using namespace std;

class Solution {
    struct Discount {
        string tag;
        int type;              // 0 means price, 1 means percentage, 2 means fixed discount
        int amount;            // depend on type

        Discount(): Discount("", 0, 0) {}
        Discount(string tag, int type, int amount): tag(std::move(tag)), type(type), amount(amount) {}
    };
public:
    int lowestPrice(vector<vector<string>> products, vector<vector<string>> discounts) {
        // Save all discounts.
        unordered_map<string, Discount> m;
        for(auto d: discounts) {
            m[d[0]] = Discount(d[0], stoi(d[1]), stoi(d[2]));
        }

        int totalCost = 0;
        for (auto product: products) {
            int originalPrice = stoi(product[0]);
            int minPrice = originalPrice;
            // use valid discount if there is
            // compare all prices with different discount, get the minimum one
            for (int d = 1; d < product.size(); d++) {
                if (product[d] != "EMPTY") {
                    assert(m.count(product[d]));
                    auto discount = m[product[d]];
                    switch (discount.type) {
                        case 0: {
                            minPrice = min(minPrice, discount.amount);
                        }
                            break;
                        case 1: {
                            int price = originalPrice * (100 - discount.amount) / 100;
                            minPrice = min(minPrice, price);
                        }
                            break;
                        case 2: {
                            int price = originalPrice - discount.amount;
                            minPrice = min(minPrice, price);
                        }
                            break;
                    }
                }
            }
            totalCost += minPrice;
        }
        return totalCost;
    }
};

int main() {
    Solution s;
    cout << s.lowestPrice({{"10", "d0", "d1"}, {"15", "EMPTY", "EMPTY"}, {"20", "d1", "EMPTY"}}, {{"d0", "1", "27"}, {"d1", "2", "5"}}) << endl;
    return 0;
}

Program: Find Lowest Price Amazon OA Solution in Java

// "static void main" must be defined in a public class.
public class Main {
    public static void main(String[] args) {
        String[][] product = {{"10","d0","d1"},{"15","EMPTY","EMPTY"},{"20","d1","EMPTY"}};
        String[][] discount = {{"d0","1","27"},{"d1","2","5"}};
        
        System.out.println(totalPrice(product, discount));
    }
    
    private static int totalPrice(String[][] products, String[][] discounts) {
        HashMap<String, Discount> dic = new HashMap();
        for(String[] discount:discounts){
            int type = Integer.parseInt(discount[1]);
            int value = Integer.parseInt(discount[2]);
            dic.put(discount[0],new Discount(discount[0],type,value));
        }
        int res=0;
        for(String[] product:products){
            int price = Integer.parseInt(product[0]);
            int min = price;
            for(int i = 1;i<product.length;i++){
                int curprice = price;
                String index= product[i];
                if(!index.equals("EMPTY")){
                    Discount dis= dic.get(index);
                    if(dis.type==0){
                        curprice = dis.value;
                    }
                    else if(dis.type==1){
                        curprice = curprice*(1-dis.value/100);
                    }
                    else if(dis.type==2){
                        curprice-=dis.value;
                    }
                    min=Math.min(min, curprice);
                }
            }
            res+=min;
        }
        return res;
    }
    
    static class Discount{
        String index;
        int type;
        int value;
        
        Discount(String index, int type, int value) {
            this.index = index;
            this.type = type;
            this.value = value;
        }
    }
}

Amazon Online Assessment Questions:

Leave a Comment

eight + 10 =