Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
15,583 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 | 15,583 views

2 Answers

0 like 0 dislike
Best answer

Stock Price Problem : 

Code (C++) 

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
int main() {
    ll n;
    cin>>n;
    ll i = 0 ; 
    ll s = 0 ; vector <ll> b(n+5,0);
    while(i<=n-1){
        cin>>b[i];
        s+=(b[i]);
        i++;
    }
    
    ll mx = 1e18 ; 
    i = 0 ;ll rr = 0 ; 
    long double v = 0 ; 
    while(i<=n-2){
        v = v + b[i];
        long double v55 = abs(s-v);
        
        long double t = i + 1 ; 
        long double t55 = n - i ; 
        
        ll g = floor(v/t);
        ll g55 = floor(v55/t55);
        
        ll d = abs(g55-g);
        if(d<mx){
            mx = d ; 
            rr = i + 1 ; 
        }
        
        i++;
    }
    cout<<rr;
    
    
    
    return 0;
}

by Expert (108,170 points)
edited by
0 0
compiler error was coming by this code
Please put the full working code of this Amazon OA Problem | Urgent | 2022
0 0
PYTHON SOLUTION PREFIX SUM

totalsum=sum(stockPrice)

running_sum=0
min_so_far=inf
result=None

for i in range(len(stockPrice)):
    running_sum+=stockPrice[i]
    leftavg=running_sum//(i+1)
    rightavg=(totalsum-running_sum)//(n-i+1)
    if abs(rightavg-leftavg)<min_so_far:
        min_so_far=abs(rightavg-leftavg)
        result=i+1

return result
0 like 0 dislike
//JAVA AND C++ are similar

public class MinNetPrice {

 

public static void main(String[] args){ MinNetPrice minNetPrice = new MinNetPrice(); System.out.println(minNetPrice.minNetPrice(new int[]{1,3,2,3})); } public int minNetPrice(int[] stockPrice){ int sum = 0; for(int i : stockPrice){ sum+=i; } int minNetPrice = Integer.MAX_VALUE, month=-1, n = stockPrice.length; int leftSum=0,rightSum = sum; for(int i=1;i<n;i++){ leftSum+=stockPrice[i-1]; rightSum-=stockPrice[i-1]; int currNet = Math.abs((leftSum/i)-(rightSum/(n-i))); if(minNetPrice>currNet){ minNetPrice = currNet; month = i; if(minNetPrice==0) break; } } return month; }

 

}
by (200 points)