Amazon Interview Question for Software Engineer / Developers






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

How will 2 be handelled?
output can be 1,3,2,10
instead of 1,3,4,10

- Tarun Phaugat March 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

any will do good Tarun...it should be the max length one..bt, using the backtracking one may go back nd print all of them....

- nare March 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess it is different from LCS problem. LCS is the subsequence among 2 or more lists but in this case we have only a single list.

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

well lcs with same one in sorted order, find the lcs of array1 with array1 sorted...this problem is called longest increasing subsequence.....there is an standalone solution as well..with above DP:

- tera March 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If I am not wrong, in the LCS problem, the sub sequence has to be contiguous which is not the case here
So I am not sure LCS can be applied in this scenario ....

- abhimanipal April 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

working code in O(n2)

/* To find longest monotonically increasing subsequence */
	int a[] = { 1,4,6,2,3,0,5};
	int lcs[100], p[100];
	//initialize
	int size = sizeof(a)/sizeof(a[0]);
	for(int i=0; i < size; ++i)
	{
		lcs[i]=1;  p[i] = -1;
	}
	for(int i=0; i < size; ++i)
		for(int j=0; j < i; ++j)
		{
			if(a[i] > a[j])
			{
				//find max of lcs[i], lcs[j]+1
				if(  lcs[j]+1 > lcs[i] )
				{
					lcs[i] = lcs[j] + 1;
					p[i] = j;
				}
			}
		}
	int max=lcs[0], maxInd=0;
	for( int i=1; i < size; ++i)
		if( lcs[i] > max )
		{
			max = lcs[i];
			maxInd = i;
		}
	while( true )
	{
		cout << a[maxInd] << endl;
		maxInd = p[maxInd];
		if( maxInd == -1 )
			break;
	}

- e=mc2 March 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct solution

- siva.sai.2020 May 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi,
Can you please also mention the amazon location where this question was asked?

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

very good site people.csail.mit.edu/bdean/6.046/dp/

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

O(n) Solution

public class Amazon9 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int sIndex=0;
		int eIndex=0;
		int i=0;
		int j=0;
		int [] a = {8,6,5,1,9,3,7,4,2,10};
		int max=1;
		int count=1;
		while(j<a.length)
		{
			j++;
			if(j<a.length && a[j]>=a[j-1])
			{
				count++;
			}
			else
			{
				if(count>max)
				{
					max=count;
					sIndex=i;
					eIndex=j-1;
				}
				i=j;
				count=1;
			}

		}
		System.out.println("Sequence Size:"+max);
		for(i=sIndex;i<=eIndex;i++)
			System.out.print(a[i]+",");
	}

}

- Mayank April 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not read question properly :(

- Mayank May 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use patience sort :
en.wikipedia.org/wiki/Patience_sorting

Nice animation at :
wordaligned.org/articles/patience-sort

- Tejas May 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good one

- tej May 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

neat

- Anonymous June 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think patience sort is good but I'm getting a problem:
Suppose I/P array is {8,6,5,1,9,3,7,4,2,10}
first 8 is kept on a new pile
then 6, as it is smaller than 8 is again kept on a new pile
then 5 on a new pile
then 1 on a new pile
then 9 is kept over 8, as 9 is greater than 8
then 3 is put over 1
then 7 is put over 6
then 4 is put over 3
then 2 on a new pile
and now, what about 10?
where it should be kept?
over 9 or 7 or 5 or 4 or 2?
Now I think we can check for the top value in case of collision and keep on the pile having greatest value of the top so that we get longest sequece........
If anyone can help me out whether my idea is right or wrong?

- KK August 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@KK : I dont think that should matter. In case there are more than one options for a card to go, it should go on the pile which is the longest so far & not on the basis of the top values.

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

output can also be 1,3,7,10...wat about that?

- jimmy514in August 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{

	int a[] = { 1,4,6,2,3,0,5};
	int lcs[100], p[100];
	int i=0,j=0, maxIndex=0;
	//initialize
	int size = sizeof(a)/sizeof(a[0]);
	for(i=0; i < size; ++i)
	{
		lcs[i]=1;  p[i] = -1;
	}
	
	for(i=1; i< size; i++ )
	{
		for(j = i-1; j>=0; j-- )
		{
			if( (a[j] < a[i] ) && (lcs[i] < (lcs[j] + 1)))
			{
				lcs[i] = lcs[j]+1;
				p[i]  = j;
				if( lcs[maxIndex] < lcs[i] )
					maxIndex = i;
			}
		}
	}
	i = maxIndex;
	do
	{
		cout<<a[i]<<",";
		i = p[i];		
	}while(i!=-1);

	return 0;
}

Time complexity O(n2)

- siva.sai.2020 February 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use a stack. Push first number and check for larger number with top. When you see a small number than top the pop the top and push the number to stck. If the number is larger than top then push that number..

- Arunmon September 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It is Longest Common Subsequence Problem(LCS). It is easy to find an O(n^2) solution for this one. Use the following DP:
L(i) = max(L(j))+1 if a(j) <= a(i) for all j<i
else L(i) = max(L(j)) if a(j) > a(i)

Write a DP for the same computing bottom to top and get the soln....

- nare March 22, 2010 | 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