Max Average Stock Price Amazon OA 2023 Solution

Want to solve Max Average Stock Price Amazon OA?, if yes then this article is for you.

We have research and collected a bundle of questions which was asked in Amazon OA in the year of 2022 and 2023. Today, we are going to see another problem from amazon oa called Stock Price which was asked in 2022, this questions might be still active in some of the Amazon OA so save all the solutions and lets see how can we solve this problem.

Table of Contents

Max Average Stock Price Amazon OA

The interns at Amazon were asked to review the company’s stock value over a period. Given the stock prices of n months, the net price change for the ith month is defined as the absolute difference between the average of stock prices for the first i months and for the remaining (n – i) months where 1≤ i <n. Note that these averages are rounded down to an integer.

Given an array of stock prices, find the month at which the net price change is minimum. If there are several such months, return the earliest month.

Note: The average of a set of integers here is defined as the sum of integers divided by the number of integers, rounded down to the nearest integer. For example, the average of [1, 2, 3, 4] is the floor of (1 + 2 + 3 + 4) / 4 = 2.5 and the floor of 2.5 is 2.

Example

stockPrice = [1, 3, 2, 3]

The minimum net price change is 0, and it occurs in the 2nd month. Return 2.

Function Description

Complete the function findEarliestMonth the editor below.

findEarliestMonth has the following parameter: int stockPrice[n]: the stock prices

Returns

int: the earliest month in which the net price change is minimum

Constraints

  • 2 ≤ n ≤ 105
  • 1 ≤ stockPrice[i] ≤ 109

Sample Input

STDIN               FUNCTION
------             ----------
5            ->    stockPrice[]
size n = 5
1            ->    stockPrice = [1, 3, 2, 4, 5]
3
2
4
5

Sample Output

2

Explanation

The net price change can be calculated as:

  • Month 1: [1] and [3, 2, 4, 5], their respective averages, rounded down = 1 and 3, net price change = 2
  • Month 2: [1, 3] and [2, 4, 5], averages = 2 and 3, net price change = 1
  • Month 3: [1, 3, 2] and [4, 5], averages = 2 and 4, net price change = 2
  • Month 4: [1, 3, 2, 4] and [5], averages = 2 and 5, net price change = 3

The minimum net price change is 1, and it occurs at month 2.

Sample Input 1

STDIN               FUNCTION
------             ----------
6            ->    stockPrice[]
size n = 6
1            ->    stockPrice = [1, 1, 1, 1, 1, 1]
1
1
1
1
1

Sample Output 1

1

Explanation

Since all the stock prices are all the same, the average is always 1 and the net price change is always 0. The earliest month this occurs is 1.

Stock Price Amazon OA
Stock Price Amazon OA

SOLUTION

Program: Max Average Stock Price Amazon OA Solution in Python

    #initialize month variable with 0
    month=0
    change=max(stockPrice)
    #Create a list to hold values
    l=[]
    total_sum = 0
    for i in range(len(stockPrice)):
        total_sum+=stockPrice[i]
    left = 0
    left_sum = 0
    while(len(stockPrice)>1):
        left = stockPrice.pop(0)
        l.append(left)
        #Now calculate the average
        left_sum += left
        avg1=left_sum //len(l)
        avg2=(total_sum-left_sum)//len(stockPrice)
        
        if(abs(avg1-avg2)

Amazon OA 2023 Questions with Solution

  1. Shopping Patterns Solution Amazon OA 2023
  2. Reorder Data in Log Files Solution Amazon OA 2023
  3. Top K Frequent Words Solution Amazon OA 2023
  4. Trees Height Solution Amazon OA SDE 2023
  5. Counting Binary Substrings Amazon OA 2023
  6. Grid Connections Amazon OA 2023
  7. Shipment Imbalance Amazon OA 2023
  8. Max Profit Amazon OA 2023
  9. Find Lowest Price Amazon OA 2023
  10. Decode String Frequency Amazon OA 2023
  11. Simple Cipher Amazon OA 2023
  12. Valid Discount Coupons Amazon OA 2023 Solution
  13. Count Maximum Teams Amazon OA 2023
  14. Minimum Coin Flips Amazon OA 2023
  15. Robot Bounded In Circle Amazon OA 2023
  16. Shopping Options Amazon OA 2023 Solution
  17. Fill The Truck Maximum Units on a Truck Amazon OA Solution
  18. Maximize Score After N Operations Number Game Solution Amazon OA 2023
  19. Slowest Key Amazon OA 2023 Solution
  20. Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
  21. Split String Into Unique Primes Amazon OA 2023 Solution
  22. Storage Optimization Amazon OA 2023 Solution
  23. Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
  24. Autoscale Policy Utilization Check Amazon OA 2023
  25. Optimal Utilization Solution Amazon OA 2023
  26. Merge Two Sorted Lists Solution Amazon OA 2023
  27. Two Sum Unique Pairs Solution Amazon OA 2023
  28. Amazon Music Pairs Amazon OA 2023 Solution
  29. Class Grouping Amazon OA 2023 Solution
  30. Find Max products Amazon OA 2023 Solution
  31. Get encrypted number Amazon OA 2023 Solution
  32. Find Total Imbalance Amazon OA 2023 Solution
  33. Find Total Power Amazon OA 2023 Solution

5 thoughts on “Max Average Stock Price Amazon OA 2023 Solution”

Comments are closed.