Amazon Interview Question for Applications Developers


Country: United States




Comment hidden because of low score. Click to expand.
6
of 6 vote

Algorithm:
Dynamic Programing : o(n)
1.for each kth element keep the best pair values and minimum value
2.for k+1 element compare with (k+1, minmum) vs (best pair) and accordingly update the minimum and best pair values

- om July 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one om, thumbs up!. Yeah, its a solution with O(n) time complexity. I too had thought on similar lines and wrote a small program to check the correctness of the idea and It works.

public static void maxBenefit(int[] values) throws Exception{
		if (values.length < 1) throw new Exception("Invalid input");
					
		int cMin = values[0], gMin = values[0], gMax = values[0], gdiff = 0;		
		
		for (int i=1; i < values.length; i++) {
			if (values[i] < cMin) {
				
				cMin = values[i];
			} else if ((values[i] - cMin) > gdiff) {
				
				gMin = cMin;
				gMax = values[i];
				gdiff = values[i] - cMin;
			}			
		}
		System.out.println("Max benefit of $" + gdiff + " happens by buying at $" + gMin + " and selling at $" + gMax);
	}

However, there can be many pairs with optimal (maximum) difference, and the above algorithm returns the first or the last of such pairs based on whether we use '>' or '>=' respectively in the condition below.

} else if ((values[i] - cMin) > gdiff) {

- Murali Mohan July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are the man :) ... awesome :) ... It looks simple but very difficult to get the spark ... :)

- pras July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 votes

Why bother with DP, when there is a very simple solution in O(n). The max benefit is the case where you buy at the lowest price and sell at the highest, with the condition that the buy has to be before sell.

public static int getMaxBenefit(int []S) {
	int maxBenefit = 0;
	int minPrice = Integer.MAX_VALUE;
		
	for (int i = 0; i < S.length; i++) {
		maxBenefit = Math.max(maxBenefit, S[i] - minPrice);
		minPrice = Math.min(S[i], minPrice);
	}
		
	return maxBenefit;
}

- oOZz July 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@oOZz

Your code is essentially reflecting the same idea. it's nothing new.

- Murali Mohan July 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Apostle The solution explained and the code written has nothing to do with DP, but it's a working solution, I give you that. The problem is not asking the "best pairs" and I see that you've added gMin, gMax, etc.

I am just pointing out that this solution does not require DP and it can be implemented in a simple way with less number of codes, to me that's valuable, and I am sure it is for the interviewer too.

- oOZz July 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@oOZz

I agree that the solution does not require a true DP implementation where the solutions to subproblems are constructed using solutions to sub-subproblems and by using tables for memoization.. etc etc..

This looks more like a max subsequence sum problem, where the solution is obtained by storing values of subproblems in a single variable instead of tables.
Overlapping subproblems and optimal substructure may not be quite apparent in this problem, though, the principles of dynamic programming as to building solution in a bottom-up way and checking 'whether or not the current value contributes to the solution' are still valid here and that is what @om seems to mean by DP

Nonetheless, I see that our implementations conform to @om's idea with mine being a bit more elaborate and instructive and yours short and sweet.

- Murali Mohan July 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A related problem that is more difficult and requires true DP style approach can be found at: hackerrank.com/challenges/stockmax

- Murali Mohan July 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@oOZz
Hey, but your solution is O(N^2), while the solution given by om is only O(N). However I totally agree with you that calling om's solution as "DP" is an unfortunate misuse of the language. lol.

- Chih.Chiu.19 July 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ Chih.Chiu.19
Why is my solution O(n^2)?
This is a linear algorithm, note that there is only one loop.

- oOZz July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

what would be the approach if we were allowed to buy & sell any number of times
eg 5 1 4 6 7 8 4 3 7 9
Buy at 1 ,sell at 7
then Buy at 3 sell at 9
Net profit=12

- mad coder July 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

There is a very simple solution in O(n). The max benefit is the case where you buy at the lowest price and sell at the highest, with the condition that the buy has to be before sell.

public static int getMaxBenefit(int []S) {
	int maxBenefit = 0;
	int minPrice = Integer.MAX_VALUE;
		
	for (int i = 0; i < S.length; i++) {
		maxBenefit = Math.max(maxBenefit, S[i] - minPrice);
		minPrice = Math.min(S[i], minPrice);
	}
		
	return maxBenefit;
}

- oOZz July 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

construct two arrays as follow:
F(i)= min(0<x<i)
G(i) = max(i<x<10)
where i [0,10)
so u will get for your test case

F = 5 1 1 1 1 1 1 1 1 1
G = 9 9 9 9 9 9 9 9 9 9

take the difference as follow
diff(i) = G(i+1) - H(i)

and take the max of it
answer = 9-1 = 8

your examples is simple
a good test case should be 8 9 11 12 7 6 4 9 1 4

F = 8 8 8 8 7 6 4 4 1 1
G = 12 12 12 12 9 9 9 9 4 4

diffence
: 4 4 4 1 2 3 5 0 3
so the answer is 5
which you can verify

- kkr.ashish July 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we need best time complexity... can we try to solve it in O(n)

- PKT July 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is O(n +n +n) = O (n)..wat else you want
F(k) = min(F(k-1),input_array(k))
G(k) = max(G(k+1),input_array(k))

so both of these are n comparisons
so O(n)
and subtraction and max is also O(n)
so total O(n)

- kkr.ashish July 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,
I think, this will produce incorrect result for array {3, 2, 1}. You will have F = {3, 2, 1}, G = {3, 2, 1}, won't you? And the wise-difference will be 0. But the answer seems to be -1.
By the way, your algorithm is incorrect only for strictly decreasing arrays.

- golodnov.kirill July 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

yes i agree @golodnov thanx for point it out

the difference shouldn't be elementwise it
should be
diff(i) = G(i+1) - H(i) because one will sell only aftr he has bought

- kkr.ashish July 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

mn[0]=1e9;
    mx[11]=maxx=0;
    for(int i=1;i<=10;i++) cin>>arr[i];
    for(int i=1;i<=10;i++) mn[i]=min(mn[i-1], arr[i]);
    for(int i=10;i>=1;i--) mx[i]=max(mx[i+1], arr[i]);
    for(int i=1;i<=10;i++) maxx=max(maxx, mx[i] - mn[i]);
    cout<<maxx<<endl;

this is the code, is their any better approach?

- raihanruhin July 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@above
for(int i=1;i<=10;i++) maxx=max(maxx, mx[i] - mn[i]);

this should be
for(int i=1;i<=9;i++) maxx=max(maxx, mx[i+1] - mn[i]);

and set maxx = -1e9
and it will work even if even in best case there is a lose in buying and selling as mentioned above

- kkr.ashish July 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

find the local maxima and minima of the given array for n elements..:)

- ram rs July 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int max_benefit = INT_MIN;
for(size_t i = 0; i < prices.size(); ++i)
{
	for(size_t j = i + 1; j < prices.size(); ++j)
	{
		if(prices[j] - prices[i] > max_benefit)
		{
			max_benefit = prices[j] - prices[i];	
		}
	}
}

This is O(n^2). Too lazy to figure out something more efficient

- Anonymous July 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This should work in O(n).

#include <stdlib.h>
#include <stdio.h>

int * bestDates(int *prices, int length);

int main(){

  int dateArray[] = {-1, 1, 4, 6, 7, 8, 4, 3, 7, 9, 1, 2, 0, 12};
  int *returnDates = bestDates(dateArray, 15);
  if(!returnDates){
    printf("Error\n");
    return 0;
  }else{
    printf("Best Dates:\n");
    printf("Buy: %d\n", returnDates[0]);
    printf("Sell: %d\n", returnDates[1]);
  }
  return 0;
}


int * bestDates(int *prices, int length){
  if(prices == NULL || length < 2){
    return NULL; //error
  }

  int bestBuyDate = 0; // intialize with first day
  int bestSellDate = 1; // intialize with second day
  int smallestDate = (prices[0] <= prices[1])?0:1; // initialize with smaller of both days

  for(int i = 2; i < length; ++i) {

    if( (prices[i] - prices[smallestDate]) > (prices[bestSellDate] - prices[bestBuyDate]) ){
      //Better dates found
      bestSellDate = i;
      bestBuyDate = smallestDate; //smallest was best with this sell date
    }

    if( prices[i] < prices[smallestDate] ){
      //New smallest item found
      smallestDate = i;
    }
  }
  int * bestReturn = malloc(sizeof(int) * 2);
  if(bestReturn == NULL) return NULL; //error
  bestReturn[0] = prices[bestBuyDate];
  bestReturn[1] = prices[bestSellDate];
  return bestReturn;
}

- Bill July 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Build a max heap and min heap data structure which only takes O(n) time (use heapify method). Once done look out the min element and maximum elements in both the heaps . the difference between these value is the answer

- Anonymous July 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Does your heap satisfy below condition....?
Condition: Share must be sold any day after buying date.

- PKT July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Build a max heap and min heap data structure which only takes O(n) time (use heapify method). Once done look out the min element and maximum elements in both the heaps which takes only O(1) time. the difference between these value is the answer

- Anonymous July 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Without using Extra memory and in O(n) it's possible
Ex : 5 3 4 6 2 8 4 1 7 9
Calculate the difference with minimum, If the difference becomes -Ve, Change the minimum to index where you are getting -Ve calue.
-4 1 3 -1 6 2 -1 6 7

- Anonymous July 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

CORRECTION :
Since 3-5 is -Ve and 5 is 1st element.
0 1 3 -1 6 2 -1 6 7

- Anonymous July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class HelloWorld{

public static void main(String []args){
int val[]={10,14,15,19,12,115,14};

int min=val[0];int diff=0;
for (int i: val)
//System.out.println(i);
{
if(i<=min)min=i;
else diff=(i-min)>diff?i-min:diff;
}
System.out.println(diff);
}}

- mohitcrox July 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

why not buy at 1,sell at 8 then again buy at 3 and sell at 9?
here max profit will be = 13

- akshay0007k July 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void getMaxProfit(int[] data) 
	{
		int len = data.length;
		int profit = Integer.MIN_VALUE;
		int s = 0; // sell index
		int b = 0; // buy index
		
		for(int i = 1; i < len; i++)
		{
			if (data[b] > data[i]) // minimizing the buy value
			{
				b = i;
			}
			
			if (data[s] < data[i]) // maximizing the sell value
			{
				s = i;
			}
			// updating the profit if sell index if greater than buy index
			if (s > b && profit < (data[s] - data[b])) 
			{
				profit = data[s] - data[b];
			}
		}
		
		System.out.println("Profit :" + profit);

}

Time complexity : O(1)
Straight forward and easy to understand.
Please point out any issue if you get.

- coder July 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

By mistake Time complexity is written as O(1)
It will be O(n).

- coder July 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void stock(int a[])
{
int cmin=a[0],i,gmin=a[0],gmax=a[0],gd=0;

for(i=1;i<6;i++)
{ if(cmin>a[i])
cmin= a[i];
else if (gd<(a[i]-cmin)){
gmin=cmin;
gmax=a[i];
gd=a[i]-cmin;
}}
printf("%d -%d = %d",gmax,gmin,gd);

}
main(){
int arr[6]={2,5,1,3,2,2};
stock(arr);
}

- mayank April 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

They are essentially interested in a case where the difference between the two value is maximum. These values are all positive. So, the difference between the maximum and the minimum is the answer! All we need to do is to find these max and min. They can be found in O(n). Please correct me if I didn't get the question right.

- Anonymous July 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the order of the max and min matters.For example if min occurs after max

- goue September 26, 2014 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More