## Amazon Interview Question

• 1
of 1 vote

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 .

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

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

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

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

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?

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

Please let me know if you come across the solution.

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

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

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

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

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.

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

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

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

?

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

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

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 );
}
}
}``````

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;

}``````

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

Dynamic Programming can solve this O(n)

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

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..

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.

### 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.