Amazon OA
Question Details
Q1. Given an array of size n with integers in it. And given two numbers a and b, find the minimum possible score of ELIGIBLE sub-arrays of the given array. i)...
Full Details
Q1. Given an array of size n with integers in it. And given two numbers a and b, find the minimum possible score of ELIGIBLE sub-arrays of the given array.
i) The score of a sub-array is calculated as the number of distinct elements within that sub-array
ii) a sub-array is eligible if it contains atleast 1 occurence of a and b each.
variable length sliding window.
code:
vector<int> arr, int n, int a, int b;
int ans = n+1;
unordered_map<int, int> mp;
int i=0;
for(int j=0;j<n;j++){
mp[arr[j]]++;
if(mp[a] > 0 && mp[b] > 0)
ans = min(ans, (int)mp.size());
while(mp[a]>0 && mp[b]>0){
\t ans = min(ans, (int)mp.size());
\t mp[arr[i]]--;
\t if(mp[arr[i]] == 0)
\t mp.erase(arr[i]);
\t i++;
}
}
cout<<ans<<endl;
15/15 test cases
passed
Q2 There are multiple delivery centers and delivery warehouses all over the world! The world is represented by a number line from -10\u2079 to 10\u2079. There are n delivery centers, with the i-th one at location center[i]. A location x is called a suitable location for a warehouse if it is possible to bring all the products to that point by traveling a distance of no more than d.
At any one time, products can be brought from one delivery center and placed at point x. Given the positions of n delivery centers, calculate the number of suitable locations in the world. That is, calculate the number of points x on the number line (-10\u2079 \u2264 x \u2264 10\u2079) where the travel distance required to bring all the products to that point is less than or equal to d.
Note: The distance between point x and center[i] is |x - center[i]|, their absolute difference.
binary search.
i was not able to solve this problem fully. i tried using binary search twice to find the minimum x (minx) which satisfies the constraints and then maximum x (maxx) which satisfies the constraints.
if minx>maxx then ans = 0
else ans = maxx-minx+1
only 12/15 test cases
passed. Can someone please help with the complete solution and where exactly I am missing in this.
bool cal(vector<int> arr, int d, int x){
\tint sum = 0;
\tfor(int i:arr){
\tsum+=abs(x-i);
\t}
\treturn sum<=d;
}
vector<int> arr, int n, int d;
int l1 = -1e9, h1 = arr[n-1]-d;
int minx = h1;
while(l1<=h1){
int m = (l1+h1)/2;
if(cal(arr, d, m)){
minx = m;
\th1=m-1;
}else{
l1 = m+1;
}
}
l1=arr[0]+d, h1=1e9;
int maxx = l1;
while(l1<=h1){
int m = (l1+h1)/2;
if(cal(arr, d, m)){
maxx = m;
\tl1=m+1;
}else{
h1 = m-1;
}
}
if(maxx<mixx)return 0;
**return** (maxx-mix+1);
I was able to pass only 12/15 test cases. Can someone help me realize why this would fail and what would be the correct solution for this.
About This Question
This is a reported interview question from a amazon interview for a swe role during the oa round reported in 2024.
It covers the following topics: Arrays, Sliding Window, Binary Search, Behavioral, Two Pointers .