Amazon Interview Question for Interns


Country: India
Interview Type: Written Test




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

This is a simple dynamic programming problem. It is similar to Kadane's largest subarray sum problem.

{
public int getLongestArithmeticSeq(int[] array) {
	int difference = array[1] - array[0];		// common difference of arithmetic sequence
	int longestLength = Integer.MIN_VALUE;	// stores the longest length sequence found till now
	int currentLength = 1;		// stores length of current arithmetic sequence
	for(int iii = 1; iii < array.length - 1; iii++) {
		int currentDifference = array[iii + 1] - array[iii];
		if (currentDifference == difference) {		// check if these elements are part of the sequence
			currentLength++;		// if they are, increase currentLenght
		} else {			// otherwise set currentLength to 1, and common difference to new difference
			currentLength = 1;
			difference = currentDifference;
		}
		longestLength = Math.max(longestLength, currentLength);		// store the maximum length
	}
	return longestLength;
}

}

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

Yes, This is similar to kadanes algo. Question should have longest contiguous arithmetic sequence for more clarity. In the second example elements are 3,7,11,15,19

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

good one

- outdoor March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

question does not mentions whether sequence should be contigous
???

- arjun March 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is must continues, otherwise the sample -1 1 3 7 11 15 19 20 21 22 should return 6 instead of 5. So the solution seems simple here,

public static int GetTheLongestContinueArithmeticSequence(int[] input)
{
if (input == null) return 0;
int Steps = 0;
int Maxlen = 2;
int currentLen = 2;
if(input.Length == 1) return 1;
for (int i = 1; i < input.Length; i++)
{
int value = input[i] - input[i - 1];
if( value != Steps)
{
Steps = value;
if (currentLen > Maxlen)
{
Maxlen = currentLen;
currentLen = 2;
}
}
else{
currentLen++;
}
}
return Maxlen;
}

- samsakni April 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Y the ***? u r posting quesns without forming a meaningful sentence..i m asking u. dnt u knw hw 2 frm a sentnce? Anyway i knew ths questn already?

DNT EVER POST QUES LIKE THIS ANY MORE...U ***


Solution(only concept)


We iterate over the array and find the difference between the consecutive elements and keep track of the longest running count of same difference. If current difference is different than the previous difference then we reset the count. If the length of the longest running difference is k. Then the longest arithmetic sequence is of length k+1.


Code

package com.dsalgo;

public class MaxArithmeticSequence {

public static void main(String[] args) {
int[]arr={2,5,3,6,9,12,15,34,23};
findMaxArithmeticSequence(arr);
}

private static void findMaxArithmeticSequence(int[] arr) {
int maxLength=0;
int currentLength=0;
int prevDiff=0;
for(int i=1;imaxLength)
maxLength=currentLength;
}
else
{
currentLength=2;
prevDiff=arr[i]-arr[i-1];
}
}
System.out.println("max arithmetic sequence length = "+maxLength);
}

}

- subbu September 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL @subbu.

- Anonymous September 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public class LargestArithmeticSequenceInGiveArray {

int sequenceCount = 0;
int longestSequenceCount = 0;
int a_1 = 0;
int d = 0;

int indexCount = 0;
//a_n = a_1 + (n-1)d
public int findLongestArithmeticSequence(int[] givenArray)
{
if(givenArray.length <3)
{
return longestSequenceCount;
}

for(int i=0; i<givenArray.length;)
{
if(sequenceCount == 0)
{
a_1 = givenArray[i];
d = givenArray[i+1] - a_1;
sequenceCount++;
i++;
}
else if(sequenceCount>0)
{
if(a_1 + (sequenceCount)*d == givenArray[i])
{
sequenceCount++;
i++;
}
else
{
indexCount++;
if(longestSequenceCount < sequenceCount)
{
longestSequenceCount = sequenceCount;
}
sequenceCount=0;
i = indexCount;
}
}

if(i == givenArray.length)
{
if(sequenceCount > longestSequenceCount)
longestSequenceCount = sequenceCount;
}


}
return longestSequenceCount;
}


public static void main(String[] args) {
//-1 1 3 7 11 15 19 20 21 22
int[] givenNumber = new int[10];
givenNumber[0] = -1;
givenNumber[1] = 1;
givenNumber[2] = 3;
givenNumber[3] = 7;
givenNumber[4] = 11;
givenNumber[5] = 15;
givenNumber[6] = 19;
givenNumber[7] = 20;
givenNumber[8] = 21;
givenNumber[9] = 22;

LargestArithmeticSequenceInGiveArray l = new LargestArithmeticSequenceInGiveArray();
System.out.println(l.findLongestArithmeticSequence(givenNumber));
}

}

- Razor February 03, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public class LargestArithmeticSequenceInGiveArray {

int sequenceCount = 0;
int longestSequenceCount = 0;
int a_1 = 0;
int d = 0;

int indexCount = 0;
//a_n = a_1 + (n-1)d
public int findLongestArithmeticSequence(int[] givenArray)
{
if(givenArray.length <3)
{
return longestSequenceCount;
}

for(int i=0; i<givenArray.length;)
{
if(sequenceCount == 0)
{
a_1 = givenArray[i];
d = givenArray[i+1] - a_1;
sequenceCount++;
i++;
}
else if(sequenceCount>0)
{
if(a_1 + (sequenceCount)*d == givenArray[i])
{
sequenceCount++;
i++;
}
else
{
indexCount++;
if(longestSequenceCount < sequenceCount)
{
longestSequenceCount = sequenceCount;
}
sequenceCount=0;
i = indexCount;
}
}

if(i == givenArray.length)
{
if(sequenceCount > longestSequenceCount)
longestSequenceCount = sequenceCount;
}


}
return longestSequenceCount;
}


public static void main(String[] args) {
//-1 1 3 7 11 15 19 20 21 22
int[] givenNumber = new int[10];
givenNumber[0] = -1;
givenNumber[1] = 1;
givenNumber[2] = 3;
givenNumber[3] = 7;
givenNumber[4] = 11;
givenNumber[5] = 15;
givenNumber[6] = 19;
givenNumber[7] = 20;
givenNumber[8] = 21;
givenNumber[9] = 22;

LargestArithmeticSequenceInGiveArray l = new LargestArithmeticSequenceInGiveArray();
System.out.println(l.findLongestArithmeticSequence(givenNumber));
}

}

- nick.dom1519 February 03, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Dear Pintu Gupta , Please post the questions clearly so that even beginners like me can also understand

- kgpgeek August 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<math.h>

int glas(int *a,int n)
{
int i,j,k;
if(n==1||n==2)
{
return n;
}
else
{
i=j=k=0;
int d=a[1]-a[0];
int max=-1;
int count=2;
for(int i=1;i<n;)
{
count=2;
while((i+1<n)&&a[i+1]-a[i]==d)
{
count++;
i++;

}
if(max<count)
{
max=count;

}
d=a[i+1]-a[i];
i++;
}
return max;
}


}

int main()
{
int n;
cin>>n;
int *a=new int[n];

for(int i=0;i<n;i++)
cin>>a[i];
cout<<glas(a,n);
getch();
return 0;
}

- pintuguptajuit(PINTU GUPTA) March 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

if only continuous sequence is require then output of 2nd test case should be 4 not 5.
correct me if I`m wrong.

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

def find_max_seq(a):
    count = 0
    cur_count = 0
    for i in range(1,len(a)):
        if a[i]-a[i-1] == 1:
            cur_count += 1
        else:
            cur_count = 0
        if cur_count > count:
            count = cur_count
    return count+1 if count > 0 else count

if __name__ == '__main__':
    print find_max_seq([3,2,5,8])
    print find_max_seq([-1,1,3,7,11,15,19,20,21,22])
    print find_max_seq([-2,-1,0,1,2,3,7,11,15,19,20,21,22])

- Deepak March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

wtf does it mean? plz clarify this

- zyfo2 March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int getElementNumofSequence(int[] a){
		int diff = a[1] - a[0];
		int diff2 = 0;
		int max=0, count =1;
		for(int i=1; i<a.length -1; i++) {
			diff2 = a[i+1] - a[i];
			if (diff == diff2) {
				count++;				
			} else {
				diff = diff2;
				if (max < count)
					max = count;
				count = 1;
			}
		}
		
		return max+1;
	}

- lin March 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c code please

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

def find_consective(num):
        num.sort()
        length = max_length = 1
        prev = num[0]
        for i in range(1, len(num)):
              if num[i] - prev ==  1:
                      length += 1
                      max_length = max(max_length, length)
              else:
                      length = 1
         return max_length

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

This problem can be solved using arithmetic sequence formula too.
a_n = a_1 + (n-1)d

we calculate a_1 and d from first two elements and then go for finding third element. by using the above formula and compare it with the current element of the array. If it matches, then we increment sequenceCount, if not, we reset all the set of variables.

public class LargestArithmeticSequenceInGiveArray {

int sequenceCount = 0;
int longestSequenceCount = 0;
int a_1 = 0;
int d = 0;

int indexCount = 0;
//a_n = a_1 + (n-1)d
public int findLongestArithmeticSequence(int[] givenArray)
{
if(givenArray.length <3)
{
return longestSequenceCount;
}

for(int i=0; i<givenArray.length;)
{
if(sequenceCount == 0)
{
a_1 = givenArray[i];
d = givenArray[i+1] - a_1;
sequenceCount++;
i++;
}
else if(sequenceCount>0)
{
if(a_1 + (sequenceCount)*d == givenArray[i])
{
sequenceCount++;
i++;
}
else
{
indexCount++;
if(longestSequenceCount < sequenceCount)
{
longestSequenceCount = sequenceCount;
}
sequenceCount=0;
i = indexCount;
}
}

if(i == givenArray.length)
{
if(sequenceCount > longestSequenceCount)
longestSequenceCount = sequenceCount;
}


}
return longestSequenceCount;
}


public static void main(String[] args) {
//-1 1 3 7 11 15 19 20 21 22
int[] givenNumber = new int[10];
givenNumber[0] = -1;
givenNumber[1] = 1;
givenNumber[2] = 3;
givenNumber[3] = 7;
givenNumber[4] = 11;
givenNumber[5] = 15;
givenNumber[6] = 19;
givenNumber[7] = 20;
givenNumber[8] = 21;
givenNumber[9] = 22;

LargestArithmeticSequenceInGiveArray l = new LargestArithmeticSequenceInGiveArray();
System.out.println(l.findLongestArithmeticSequence(givenNumber));
}

}

- Razor February 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class LargestArithmeticSequenceInGiveArray {

	int sequenceCount = 0;
	int longestSequenceCount = 0;
	int a_1 = 0;
	int d = 0;
	
	int indexCount = 0;
	//a_n = a_1 + (n-1)d 
	public int findLongestArithmeticSequence(int[] givenArray)
	{
		if(givenArray.length <3)
		{
			return longestSequenceCount;
		}
		
		for(int i=0; i<givenArray.length;)
		{
			if(sequenceCount == 0)
			{
				a_1 = givenArray[i];
				d = givenArray[i+1] - a_1;
				sequenceCount++;
				i++;
			}
			else if(sequenceCount>0)
			{
				if(a_1 + (sequenceCount)*d == givenArray[i])
				{
					sequenceCount++;
					i++;
				}
				else
				{
					indexCount++;
					if(longestSequenceCount < sequenceCount)
					{
						longestSequenceCount = sequenceCount;
					}
					sequenceCount=0;
					i = indexCount;
				}
			}
			
			if(i == givenArray.length)
			{
				if(sequenceCount > longestSequenceCount)
					longestSequenceCount = sequenceCount;
			}
			
			
		}
		return longestSequenceCount;
	}
	
	
	public static void main(String[] args) {
		//-1 1 3 7 11 15 19 20 21 22 
		int[] givenNumber = new int[10];
		givenNumber[0] = -1;
		givenNumber[1] = 1;
		givenNumber[2] = 3;
		givenNumber[3] = 7;
		givenNumber[4] = 11;
		givenNumber[5] = 15;
		givenNumber[6] = 19;
		givenNumber[7] = 20;
		givenNumber[8] = 21;
		givenNumber[9] = 22;
		
		LargestArithmeticSequenceInGiveArray l = new LargestArithmeticSequenceInGiveArray();
		System.out.println(l.findLongestArithmeticSequence(givenNumber));
	}

}

- Razor February 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class LargestArithmeticSequenceInGiveArray {

int sequenceCount = 0;
int longestSequenceCount = 0;
int a_1 = 0;
int d = 0;

int indexCount = 0;
//a_n = a_1 + (n-1)d
public int findLongestArithmeticSequence(int[] givenArray)
{
if(givenArray.length <3)
{
return longestSequenceCount;
}

for(int i=0; i<givenArray.length;)
{
if(sequenceCount == 0)
{
a_1 = givenArray[i];
d = givenArray[i+1] - a_1;
sequenceCount++;
i++;
}
else if(sequenceCount>0)
{
if(a_1 + (sequenceCount)*d == givenArray[i])
{
sequenceCount++;
i++;
}
else
{
indexCount++;
if(longestSequenceCount < sequenceCount)
{
longestSequenceCount = sequenceCount;
}
sequenceCount=0;
i = indexCount;
}
}

if(i == givenArray.length)
{
if(sequenceCount > longestSequenceCount)
longestSequenceCount = sequenceCount;
}


}
return longestSequenceCount;
}


public static void main(String[] args) {
//-1 1 3 7 11 15 19 20 21 22
int[] givenNumber = new int[10];
givenNumber[0] = -1;
givenNumber[1] = 1;
givenNumber[2] = 3;
givenNumber[3] = 7;
givenNumber[4] = 11;
givenNumber[5] = 15;
givenNumber[6] = 19;
givenNumber[7] = 20;
givenNumber[8] = 21;
givenNumber[9] = 22;

LargestArithmeticSequenceInGiveArray l = new LargestArithmeticSequenceInGiveArray();
System.out.println(l.findLongestArithmeticSequence(givenNumber));
}

}

- Razor February 03, 2016 | 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