Amazon Interview Question


Country: India
Interview Type: Phone Interview




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

2 4 6 7 .. can be a seq. also .

- Anon April 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. You can select any element in the array and find the seq.

- amitnagar21 April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. You can select any element in the array and find the seq.

- amitnagar21 April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This question is not well posed. For instance, the comments of OP do not match the expected output given in the question.

Why don't people put in some thought before posting the question?

In any case, in the worst case, there are \Theta(2^n) possible sequences. Does the guy want all of them?

- Anonymous April 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am asking about "Longest increasing subsequence" Data Structure problem.

Link : en.wikipedia.org/wiki/Longest_increasing_subsequence_problem

Please let me know if you come across the solution.

- amitnagar21 April 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about using an iterative approach like they use in the book... Using combinatorics we can think of the array as binary representation. Either 1, it's in the subset, or 0, it's not (2^n possibilities). Therefore we just need to generate an array of subsets that represent the binary representation of the current iteration.

Ex: {1,2,3,4,5,6,7} we step through and the current iteration is 0101101... well, using bitwise shifting we can include all the numbers in the 1's position rendering {2,4,5,7} as the subset.

This makes the problem really easy and iterative! We may be able to speed this up by a factor of 2 by taking the 2's compliment each time and only iterating 2^(n-1) times. But still is O(2^n).

Let me know what you think

- rdo April 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nvm you would need to make modifications for it to be in increasing fashion

- rdo April 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can just add a temp variable to keep track of the previous element and compare it to the current, and break out if it's less to iterate to the next configuration.

- rdo April 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about if numbers are:

[3,2,1,4]

so answer is:

[3,4]
[1,4]
[2,4]

?

- elbek June 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is correct. All possible increasing subsequence of all lengths.

- Psycho September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

// calling convention   Sequence(input, 100, 0, temp, 0);

void Sequence(int input[ ], int n, int index, int temp[ ] , int TIndex )
{
	if( n < 1 || tIndex < 0 )
	{
		printf("\n invalid input \n");
		return ;
	}
	else if( n == index )
	{
		// print temp[] array elements;
	}
	
	
	for (; index < n; index++)
	{
		if( (Tindex == 0 ) || temp[Tindex -1 ] < input[index] )
		{
			Sequence(input, n, index + 1, temp, Tindex );
			temp[ Tindex++] = input[ index ] ;
			Sequence(input, n, index + 1 , temp, Tindex );
		}
	}
}

- siva.sai.2020 April 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I guess each seq must have at least one unique element.

void PrintAllSeq(int start,vector<int>& seq)
{
    if (start == n) {
        PrintSeq(seq);
        return;
    }
    while((!seq.empty()) && (A[seq.back()] > A[start])) {
       seq.pop_back();
    }
    for (int i = start; i <n;++i) {
        if (seq.empty() || (A[seq.back()] < A[i])) {
            seq.push_back(i);
        } else {
            vector<int> newSeq = seq;
            PrintAllSeq(i, newSeq);
        }
    }
    PrintSeq(seq);
}
// we need to keep track of the uniqueness

void PrintSeq(vector<int>& seq)
{
    if (seq.empty()) return;
    bool hasUnique = false;
    for(int i =0; i<seq.size(); ++i) {
        if (!isPrinted[seq[i]]) {
           hasUnique = true;
           break;
        }
    }
    // this seq already subseq of some other seq
    if (!hasUnique) return;
    for(int i =0; i<seq.size(); ++i) {
        isPrinted[seq[i]] = true;
        cout << A[seq[i]] << ", ";
    }
    cout << endl;

}

- dumbhead April 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Dynamic Programming can solve this O(n)

- SteelRain May 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't believe it: if longest increasing subsequence can be solved in O(n log n) then this problem (where we need to find all unique longest sequences) cannot be solved faster..

- Anonymous May 13, 2012 | 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