Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

correct me if i wrong:

max = a[0]; loss = 0;
for(int i = 1; i < n; i++) {
    if(a[i] >= max) {
        max = a[i];
    } else {
        loss = max(loss, max - a[i]);
    }
}

- Anonymous October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ops sorry for confusing 'max' variable with the function name

- Anonymous October 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

comments to my solution:

if the stock prices climb, we just keep updating the maximal
bid price 'max' because there cannot be any loss if prices only get higher.

as soon as we encounter the first price that is lower than
the previous price, we calculate the loss as a difference
between 'max' and this current price, and so on..
we keep updating the 'max' and 'loss' variables in this way

- Anonymous October 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

great solution!!!!!!most optimised i thk.

- abhrajuit October 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Start from right end. For each number save in another array the number which is smallest number that falls to right of it. Call this the min array.Can be done in O(n).
Now start from beginning and initialize loss to 0.Now for each number find the difference between the number and corresponding index in the min array and if its greater than loss, make it the new loss. Report loss after the end of loop. This step should take O(n) time. Total time complexity = O(n) and space complexity = O(n).

- FragAddict October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

using namespace std;
int MaxLossInStockPrice(int inputArray[], int length)
{
	int maxLoss = 0;
	for(int i = 0; i < length; i++)
		for(int j = i; j < length; j++)
		{
			if(inputArray[i] - inputArray[j] > maxLoss)
			{
				maxLoss = inputArray[i] - inputArray[j];
			}
		}
	return maxLoss;
}

int main()
{
	int a[] = {1,2,3,7,5,8,9,4,6,10,12};
	int length = sizeof(a)/sizeof(*a);
	cout<<MaxLossInStockPrice(a, length);
}

- mingzhou1987 October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey1145" class="run-this">void getMaxPrice(int *arr, int nLength)
{
if (NULL == arr)
return;

for (int i = nLength-1;i > 0;i--)
arr[i] = arr[i-1] - arr[i];

int maxp = 1, minp = 1, maxValue = arr[1];
int maxtmp = 1, mintmp = 1, tmpValue = arr[1];
for (int i = 2;i < nLength;i++)
{
if ((tmpValue + arr[i]) > arr[i])
{
mintmp = i;
tmpValue += arr[i];
}
else
{
mintmp = maxtmp = i;
tmpValue = arr[i];
}

if (tmpValue > maxValue)
{
maxValue = tmpValue;
maxp = maxtmp;
minp = mintmp;
}
}

for (int i = 1;i < nLength;i++)
arr[i] = arr[i-1] - arr[i];
printf("%d - %d = %d\n",arr[maxp-1],arr[minp],maxValue);
return ;
}
</pre><pre title="CodeMonkey1145" input="yes">
</pre>

- Anonymous October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Total time complexity = O(n) and space complexity = O(1).

- Anonymous October 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int maxLoss(int a[],int n)
{
    int max=a[0],maxloss=0;
    for(int i=1;i<n;i++)
    {
            if(maxloss<max-a[i])
            maxloss=max-a[i];
            if(max<a[i])
            max=a[i];
    }
    return maxloss;

}

- Learn Android: http://learnandroideasily.blogspot.in/ October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Maxloss(i) = max(MaxLoss(i-1), (maxValTillNow - a[i]))

Base Case
if(i == 1)
  return a[0]-a[1]

Complexity O(n)

- Prateek Caire October 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

solution 1 is wrong

- andy October 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey98658" class="run-this">class MaxLoss {
public int maxLoss(int[] prices) {
int maxLoss = 0;
for(int i = 0; i < prices.length; ++i) {
int curMaxLoss = 0;
for(int j = i + 1; j < prices.length; ++j) {
if(prices[i] > prices[j]) {
curMaxLoss = Math.max(curMaxLoss, prices[i] - prices[j]);
}
}
maxLoss = Math.max(maxLoss, curMaxLoss);
}
return maxLoss;
}

public static void main(String[] args) {
int[] prices = {1, 2, 3, 7, 5, 8, 9, 4, 6, 10, 12 };
System.out.println(new MaxLoss().maxLoss(prices));
}
}
</pre><pre title="CodeMonkey98658" input="yes">
</pre>

- smffap November 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey65891" class="run-this">class MaxLoss {
public int maxLoss(int[] prices) {
int maxLoss = 0;
for(int i = 0; i < prices.length; ++i) {
for(int j = i + 1; j < prices.length; ++j) {
if(prices[i] > prices[j]) {
maxLoss = Math.max(maxLoss, prices[i] - prices[j]);
}
}
}
return maxLoss;
}

public static void main(String[] args) {
int[] prices = {1, 2, 3, 7, 5, 8, 9, 4, 6, 10, 12 };
System.out.println(new MaxLoss().maxLoss(prices));
}
}
</pre><pre title="CodeMonkey65891" input="yes">
</pre>

- smffap November 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int findMaxLoss(int array[]){
		int max = array[0];
		int res = -1;
		for (int i = 1; i < array.length; i++) {
			if(array[i] - max < 0)
				res = Math.max(res, max = array[i]);
			max = Math.max(max, array[i]);
		}
		return res;
	}

- nmc January 08, 2012 | Flag Reply


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