Amazon Interview Question for Developer Program Engineers






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

@Oshin,
The question what I posted is exactly what he asked me.

For Example: If we have { 5 6 7 3 4 5 6 3} list of 8 int numbers we have and

sum of first 3 adjacent numbers (5 +6 +7) =18
sum of next 3 adjacent numbers (6+7+3) =16
sum of next (7+3+4) = 14

like wise we need to find out maximum sum of all these ......Please let me know if you are still not clear......

- Anonymous August 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

So if the m is given instead of a constant number 3, we just remember the previous sum instead of summing up in every loop.


int maxSum = 0, currentSum = 0, size = a.length;
for (int i = 0; i <= (size - m); i++) {
if (i < m - 1) {
currentSum += a[i];
}
else if (i = m - 1) {
maxSum = currentSum;
}
else if (i > m - 1) {
currentSum = currentSum + a[i] - a[i - m];
if (currentSum > maxSum) {
maxSum = currentSum;
}
}
}

return maxSum;

- Ye August 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

above code needs a bit correction :

int maxSum = 0, currentSum = 0, size = a.length;
for (int i = 0; i <= size-1; i++) {
if (i <= m - 1) {
currentSum += a[i];
}
if (i == m - 1) {
maxSum = currentSum;
}
if (i > m - 1) {
currentSum = currentSum + a[i] - a[i - m];
if (currentSum > maxSum) {
maxSum = currentSum;
}
}

}
System.out.println(maxSum);

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

<pre lang="java" line="1" title="CodeMonkey21769" class="run-this">/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package subseq;


public class Main {


public static void main(String[] args) {
int a[] = {1,2,3,4,5,6,7,8};
int bigSum = 0,sum=0,index =0;
for(int i=0;i<a.length-2;i++)
{
int j = i;
sum=a[j]+a[j++]+a[j++];
if(sum>bigSum)
{
bigSum = sum;
index = i;
}
else
{

}
}
System.out.println("sum : "+bigSum+ " index: "+index);
}


}
</pre><pre title="CodeMonkey21769" input="yes">
</pre>

- Anonymous August 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

possible in O(N), any one knows better than this?

- Anonymous September 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Without touching all no. finding MAX sum is not possible....! so O(n)is the best sol... i think...! :)

- PKT November 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys..in the above code..ppl forgot to consider if the loop has -ve numbers in last 3 digits like {5,6,7,2,-1,0,19)...since we run the length-2..there can be a scenario where we can miss the last element.

- Newbie December 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort the list in descending order and then pick first m characters.

- guestt February 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is BY FAR the most appropriate answer... wait wait wait.. let me turn brain on.. NOPE reread the question

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

public static int getMax(int[] array, int m) {
		int maxsum=0,oldsum,currsum=0;
		for(int i=0;i<m;i++) maxsum+=array[i];
		oldsum=maxsum;
		for(int i=1;i<=(array.length-m);i++) {
			currsum = oldsum-array[i-1]+array[i+m-1];
			if(currsum>maxsum) {
				maxsum = currsum;
			}
			oldsum = currsum;
		}
		return maxsum;
	}

- Madhu February 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question does not ask for the max sum.. it asks for the tuple that makes the max sum !!



package testPrograms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class MaxSumTuple
{
private int[] tuple;

public MaxSumTuple()
{
tuple = new int[3];
}
public int[] findTuple(List<Integer> listInt)
{
int sumOld = 0,sumNew = 0;
if (listInt.size() <= 3)
{
for (int i = 0; i < listInt.size(); i++)
{
this.tuple[i] = listInt.get(i);
return tuple.clone();
}
}

for(int i=0;i<listInt.size() - 2;i++)
{
sumNew = listInt.get(i) + listInt.get(i+1) + listInt.get(i+2);
if(sumNew > sumOld)
{
sumOld = sumNew;
updateTuple(i,listInt);
}

}

return tuple.clone();

}

public void updateTuple(int index,List<Integer> list)
{
for(int i=0;i<tuple.length;i++)
{
tuple[i] = list.get(index + i);
}
}
public static void main(String []args)
{
List<Integer> list = new ArrayList<Integer>();
Random random = new Random();
for(int i=0;i<10;i++)
{
list.add(random.nextInt(100));
}

MaxSumTuple maxSumTuple = new MaxSumTuple();
int []tuple = maxSumTuple.findTuple(list);
System.out.println(list);
System.out.println(Arrays.toString(tuple));




}

}

- MS February 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

whattt...I am sure I get the question wrongly OR was he looking for a better solution than O(n) :O

- oshin August 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

better than O(n)?

- oshin August 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Try this i think it will work..



int maxsum=0,thissum=0;
for(int i=0;i<(size-2);i++)
{

thissum=a[i]+a[i+1]+a[i+2];
if(thissum<0) thissum=0;
if(maxsum<thissum) maxsum=thissum;
}

- Anonymous August 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

if m is given instead of 3 , how will we solve this

- xyz August 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if some variable m is given instead of 2, then complexity will become O(n2) from O(n).

Here it is:

package com.practice;

public class TupleWithMaxSum {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int[] intArray={1,2,16,4,5,6,7};
		int m=3;
		int max=intArray[0];
		
		for(int i=1;i<m;i++)
		{
			max= max + intArray[i];
		}
		int sum=0;	
		
		for(int i=1; i<intArray.length-m;i++)
		{
			for(int j=i;j<i+m;j++)
					sum=sum+intArray[j];
			if(sum>max)
				max=sum;		
			sum=0;
		}
		
		System.out.println("Max sum: "+ max);

	}

}

- Piyush October 28, 2010 | 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