Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
2,088 views

All past online assesments of Amazon can be found using the tag "amazon_oa" in the search bar.

Here is the link : https://www.desiqna.in/tag/amazon_oa

in Online Assessments by Expert (108,170 points)
edited by | 2,088 views

1 Answer

0 like 0 dislike
A customer wants to buy a pair of jeans, a pair of shoes, a skirt, and a top but has a limited budget in dollars. Given different pricing options for each product, determine how many options our customer has to buy 1 of each product. You cannot spend more money than the budgeted amount.


Example
priceOfJeans = [2, 3]
priceOfShoes = [4]
priceOfSkirts = [2, 3]
priceOfTops = [1, 2]
budgeted = 10


The customer must buy shoes for 4 dollars since there is only one option. This leaves 6 dollars to spend on the other 3 items. Combinations of prices paid for jeans, skirts, and tops respectively that add up to 6 dollars or less are [2, 2, 2], [2, 2, 1], [3, 2, 1], [2, 3, 1]. There are 4 ways the customer can purchase all 4 items.


Function Description


Complete the getNumberOfOptions function in the editor below. The function must return an integer which represents the number of options present to buy the four items.


getNumberOfOptions has 5 parameters:
int[] priceOfJeans: An integer array, which contains the prices of the pairs of jeans available.
int[] priceOfShoes: An integer array, which contains the prices of the pairs of shoes available.
int[] priceOfSkirts: An integer array, which contains the prices of the skirts available.
int[] priceOfTops: An integer array, which contains the prices of the tops available.
int dollars: the total number of dollars available to shop with.


Constraints


1 ≤ a, b, c, d ≤ 103
1 ≤ dollars ≤ 109
1 ≤ price of each item ≤ 109
Note: a, b, c and d are the sizes of the four price arrays

 

Python Code :

def shoppingOptions(pairOfJeans, pairOfShoes, pairOfSkirts, pairOfTops, dollars):
    """
    :type pairOfJeans: List[int]
    :type pairOfShoes: List[int]
    :type pairOfSkirts: List[int]
    :type pairOfTops: List[int]
    :type dollars: int
    :rtype: int
    """
    
    hash_map = {}
    count = 0
    
    for a in pairOfJeans:
        for b in pairOfShoes:
            curr_sum = a+b
            if curr_sum in hash_map:
                hash_map[curr_sum] += 1
            else:
                hash_map[curr_sum] = 1
            
    for c in pairOfSkirts:
        for d in pairOfTops:
            curr_val = dollars - (c + d)
            li_keys = [k for k in hash_map if k <= curr_val]
            values = [hash_map.get(k) for k in li_keys]
            count += sum(values)
            
    return count

 

 

by Expert (108,170 points)