Max Average Stock Price Solution Amazon OA 2022

Table of Contents

Max Average Stock Price Solution

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.

Related: Best Top 10 Amazon Interview Questions 2022

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.

Max Average Stock Price Amazon OA
Max Average Stock Price Amazon OA

SOLUTION

Code: Max Average Stock Price Solution

public int maxAverage(int[] prices) {
 int n = prices.length;
 int[] postfix = new int[n];
 int sum = 0;
 for(int i=n-1; i>=0; i--) {
  postfix[i] = sum;
  sum+=prices[i];
 }
 int ans = 0;
 sum = 0;
 for(int i=0; i<n; i++) {
  sum+=prices[i];
  int preAverage = sum/(i-0+1);
  int postAverage = postfix[i]==0? postfix[i] : postfix[i]/(n-1-i);
  ans = Math.max(ans, Math.abs(preAverage-postAverage));
 }
 return ans;
}

Related: Best Top 10 Google Interview Questions

Related: Best Top 10 Microsoft Interview Questions

Amazon Online Assessment Interview FAQ

How do I prepare for an Amazon interview?

Recruiters offer their best tips for interviewing at Amazon
Prepare for behavioral-based interview questions.
Format responses using the STAR method.
Provide details.
Focus on “I” not “we”.
Don’t shy away from failures.
Know why you want to work at Amazon.
Ask for clarification when you need it.
Brush up on your writing skills.

What should I say in an Amazon interview?

You could touch on these topics: Amazon’s Products: If there is a particular produce that interests you, this is the time to mention it. For instance, if you’re interviewing for AWS, mention that it’s your default cloud development platform. People: If you know employees who worked at Amazon, you should mention it.

How difficult is Amazon interview?

Amazon’s interview process can be grueling. However, the good news is that it’s fairly consistent. Because we know the structure of the interview beforehand, it makes it much easier to prepare and minimizes surprises.

Can I crack Amazon interview?

To crack any technical interview all you just need is a laptop, a working internet connection will solve problems regularly and within a few months, you can learn the essentials. The interview procedure in Amazon is kind of similar to other big tech companies so let’s discuss that

Why do you want join Amazon?

Basically you see the huge positive impact Amazon as a company has on your life as a customer, and you’d love to work for such an organization, helping other customers to benefit from the services of the company.

Why do you want to join Amazon best answer?

“Amazon is the most customer-centric and innovative company in the world and I want to be part of the movement that is Amazon.” You can use an answer this short if you have a stellar resume and a hugely charismatic personality.

How many rounds is an Amazon interview?

In a four-round Amazon interview process, each round lasting 1 hour. Each round will begin with five minutes of introductions.

How do freshers get placed on Amazon?

Online application: The simplest way is to apply through Amazon’s job portal.
Campus placements: Amazon recruits from campuses like IITs, IIMs, BITS.
Hiring events: Keep an eye out for any Amazon hiring events in and around your city.

Leave a Comment

2 × 4 =