Adobe Interview Question for Software Engineer / Developers






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

A min_heap of size 6 suffices.

- Lord Darth Plagueis August 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes this will take constant amount of memory. Duplicates are also handled.

- Psycho October 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

reheapify of min heap takes O(nlogn) time complexity.

- Adnan Ahmad September 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Adnan : Its actually O(nlog6) ~ O(2n) = O(n)
Min heap size is 6.

- Psycho October 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

takes 6 extra space then.

- Nobody August 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

look up quick-select or selection algo..linear time.. O(1) space

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

quick-select will modify the array which is a constraint of the problem.

- mg August 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sixthLargest(const int a[]) {
    int max[6] = {INT_MIN};
    for (int i = 0; i < N; ++i)
        if (a[i] > max[0]) max[0] = a[i];
    for (int x = 1; x < 6; ++x)
        for (int i = 0; i < N; ++i)
            if (a[i] > max[x] && a[i] < max[x - 1]) max[x] = a[i];
    return max[5];
}

T: O(6n) = O(n)
S: O(6) = O(1)

- mg August 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The algorithm fails if the input has duplicates.

Ex: for the input 6,1,6,3,4,2,5 the algorithm returns 1

- Ashok August 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The algorithm is correct, I interpreted incorrectly.

Thanks mg for the code

- Ashok August 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If n = 6, then this algorithm will take O(n*n) time.

- prem March 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi mg,nice work dude.but what is "INT_MIN"?

- ks September 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is simple selection problem

- ghouse August 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ghouse if u cn explain how it can be solved by selection

- ankit August 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this works..

int sixthLargest(int[] a){
	int[] max = {Integer.MIN_VALUE,Integer.MIN_VALUE,Integer.MIN_VALUE,
			Integer.MIN_VALUE,Integer.MIN_VALUE,Integer.MIN_VALUE};
	for(int i=0; i<a.length; i++){
		int index=-1;
		for(int j=0;j<max.length;j++){
			if(a[i]>max[j]){
				index = j;
				break;
			}
		}
		if(index!=-1){
			//move all max[index] -> max[index+1]
			for(int j=max.length-1; j>index; j--)
				max[j]=max[j-1];
				max[index]=a[i];
			}
		}
	return max[5];
}

The space complexity is O(1) - constant
The time complexity is O(n)

- Ashok August 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i don't think show ashok

- nav September 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can u explain ur code

- nav September 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

good sol given by mg but same as bubble sort o(k*n)

- nav September 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

good sol given by mg but same as bubble sort o(k*n)

- nav September 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Median of Medians algorithm for selection.

That can give you a linear time algorithm.

- Aditya October 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Mark virtual boundary in a single dimensional with distance of 5 elements.
2. Sort these boundaries
3. Pick 5 elements using (i+2)th where i = 0 to 4
4. Sort these elements and find the pivot.
5. Apply partition using the above pivot.
6. Divide the problem and start again if necessary.
If k=r, then return m
If k<r, then return k thsmallest of the set L .(Bound time T7n/10)
If k>r, then return k-r thsmallest of the set R

- Ankush Bindlish November 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use selection sort to sort in decending order & terminate @ the 6th instance !

- Sunil January 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Without changing the array and taking extra o(n) space and in o(n) time.

- Anonymous November 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Counting Sort.

It requires O(n) extra space with Linear time O(n). this sort also known as stable sort. Algorithm is in below.

for i =1 to K
do C[i] = 0;
for j = 1 to length[A]
do C[A[j]] = C[A[j]]+1

C[i] is now contains the number of elements equal to i

for i = 2 to k
do C[i] = C[i] + C[i-1];

C[i] now contains the number of elements less than or equal to i

for j = length[A] to 1
do B[C[A[j]]] = A[j]
C[A[j]] = C[A[j]]-1

here A is input array , B is output array and C is counter array

- Deepak Garg February 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1)read first six numbers and create a min-heap of six elements....so that the root has minimum of the six.
2)now when you get a new number >root then pop the root and make root =new number
3) heapify the 6 element heap; takes O(log6) constant time
4)scan like this for whole array
5) the root gives the 6th greatest number

Overall complexity= O(nlog(6))==O(n)

code

#include<iostream>
#include<queue>

int main()
{
int a[]={10,23,44,2,1,4,56,23,56,75,35,73,354,1223,9};
priority_queue< int, greater<int> > min_h(a,a+5);
for(i=6;i<15;i++)
{
if(a[i]>min_h.top()) {min_h.pop(); min_h.push(a[i]);}
}
cout<<"6th greatest is "<<min_h.top();

return 0;
}

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

my approach
Read first 6 elements and keep them sorted in an array of size 6.
Now start reading from 7th element and see via binary search if the element is larger than greatest then ignore else place it in the array.
Iterate till end.
The loop invariant at any time maintains 6 s,mallest numbers in that array and return the last element of the temp array in the end
Space O(6) --- ie Constant space
time O(n)

- Amit Priyadarshi September 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Since u r using array, insertion will take time. Use heap.

- Anonymous September 28, 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