Adobe Interview Question






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

So, something like a Sine wave, sorting a sine curve?
In any case, you can use any in place sorting algorithm such as QuickSort or heapsort with complexity O(nlogn) [best case]. Are we looking for better complexity?

- chennavarri November 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think we need to take under consideration that the sequence of numbers are already sorted in groups or may be not?

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

Actually, it doesn't make sense. any random set of numbers can be said as increasing and decreasing. For example, I just pressed numbers randomly on the keyboard and,
2 8 0 5 4 2 3 8 0 4 8
If you observe then, it increases to 8 then decreases to 0 then increases to 5 then decreases to 2..so on
any set of random numbers will have the behavior described in this problem. So its just a sort problem?

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

Agreed, I also couldn't think something else either. Does it make a difference if the question states each alternate increasing/decreasing sequences are of length 'k'?

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

Hey, just saw your comment.
Ya. It would make a difference. If the length of sequences are 'k' then there are n/k subsets which monotonically increase and then decrease. So once we find where the shift happens for a subset, now we can split this into two sorted arrays and merge. total: operations for every subset would be 2k (worst case)
In this way, we get all n/k subsets sorted in (n/k)*2k => 2n operations, now we merge n/k sorted arrays which should take (n/k)log(n/k). So total complexity would be O(2n + (n/k)log(n/k))
==> O(n/k * (2k+log(n/k) ) )
Best case would be O(n/k * log(n/k))
All of this with the condition that every k subset increases to a point and then decrease once. (Ex: the whole array can be a sine curve)
Correct me if am wrong. Thanks

- chennavarri November 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I've edited the comment to change complexity analysis, it didnt change. Complexity would be
==> O(n/k * (k+log(n/k) ) )
Best case would be O(n/k * log(n/k))

- chennavarri November 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Aaah! Thanks that sounds right and awesome explanation :)

- JoshMachine November 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have a small doubt..
How is the merging of k n/k sorted arrays n/k log (n/k).
For merging 2 sorted arrays of length n/k, its O(n/k)
but after this we need to merge 2n/k with n/k, 3n/k with n/k, ... (k-1)n/k with n/k,
So the complexity will be O(n/k+2n/k+3n/k+...+(k-1)n/k) = n/k * (k * (k-1)/2) = O(n*k)
if we keep just one element in a block its O(n*n)
we can choose this method provided k<logn

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

and even merging requires additional space.
Merging in above explanation @anonymous I mean that I am referring to merge of merge sort algorithm.
Is there any other merging algorithm that you were discussing?

- Shashi November 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you got confused.
There are n/k sorted arrays of length k. (you are thinking the other way i.e. there are k arrays of length n/k. thats wrong understanding)
So merging two arrays of length k is O(k), it's basically merge sort. For example, in merge sort you start of with k=1, so complexity is O(n/1 log (n/1)) ==> O(n logn). Thanks.

- chennavarri November 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@shashi: Well, merge sort can be done inplace but complicated. thomas baudel has a variant of inplace merge sort.
But if there's a simpler inplace sort for merging k sorted arrays then please suggest. Thanks

- chennavarri November 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@chennavarri
One more clarification plzz....
Even if there are n/k arrays of length k. For sorting first two we need O(k). now we need to sort 2k with k, so O(2k) .... O(n/k.k). Totally its O(k+2k+3k+...+(n/k -1)*k)
= O(k * (1+2+3...+n/k - 1)) = O((n*n)/k)
am I wrong?
You were telling that we are merging the n/k "k" length sorted arrays using merge operation right ?
How are we getting that log term, please explain...
Thanks
Shashi

- Shashi November 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Shashi sure I can explain. Consider u have 8 arrays of length k. Merge 1&2 and then 3&4 then 5&6 n 7&8. Now we have 4 2k length arrays. Repeat again. Merge 1&2 , 3&4
We r left with 2 arrays of size 4k each. Now merge these two we get an 8k length array.
Now why the log term. At every step we compare all 8k elements but how many steps are there??
One thing we observe is in the first step we had 8 arrays
Next step we had 4
& then we had 2. So basically the number of arrays halve every step sonnymver of steps = log8==3. Now to make it generic replace 8 with n/k.
If u want a pictorial representation wiki merge sort. The image explains it. Thanks

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

Thanks chennavarri

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

@Anonymous: Thanks for explanation. But going with ur explanation itself, it means that totally 8k elements will b compared and there r totally log8 such steps. Therefore the complexity should b O(8k log8). correct me if i m wrong...
now as u said, i will replace 8 with 'n/k'. then the complexity turns out to b: O((n/k)*k log 8)=O(n log 8)
Am I right???

- Ansh2rock January 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Chennavarri: good job. But if i have to merge 2 arrays of size n/2, then there will b totally n-1 comparisons in the worst case making it O(n).
Coming back to ur solution, to begin with we have 2 sorted arrays of k elements...merging them will take O(2k)...to generalize...if we have n/k sorted arrays of size k each...then at each step we will compare ((n/k)*k)=n elements making it of O(n)complexity...and there will b totally log(n/k) such steps...therefore the complexity of this solution should be O(n log n/k)...
in case of merge sort k=1 therefore O(n log n)
Correct me if i m wrong...

- Ansh2rock January 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ya, thats right. complexity is O(n log(n/k)) for merging k sorted arrays totaling n elements. I replaced n with n/k for merge sort's complexity and wrote the above comment, hmmm... guess thats not the right way for writing complexities.

- chennavarri January 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

u cant use merge sort.it will take o(n) space.

- Anonymous August 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think, the interviewer is looking for O(log N) solution to find the element, where it starts decreasing, then it is O(N) to sort it. I think binary search can be used here.

- Rocky November 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be done in O(N) time complexity. Keep on comapring the consecutive numbers until you get to the start of decreasing series, then make a note of this index and keep on going until you get the increasing series. Now make for reverse of this decreasing series.
Finally worst case time will be 2N, which is O(N).

- Raju November 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please explain it with an example:
Say the sequence is 2 8 0 5 4 2 3 8 0 4 8

- JoshMachine November 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ Raju
if O(n) is wort case then if you take each element as a bin. Then your worst case sorting algorithm for random numbers is O(n) which is bull shit.
I did not see any algorithm better than O(nlogn) till date for sorting random numbers.

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

I didnt get it. Expalin...

- RJ November 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The list in question is equivalent to a an unordered list, otherwise we could have said, 1) increasing or 2) decreasing or 3) increasing up to point then decreasing till the end or 4) decreasing up to a point then increasing till the end.
The only interesting part is "monotonically" increasing/decreasing, I don't think that would make much difference.

- Kannan November 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if we know the value of n and size of array then it can potentially be done in O(size/n)

- ace November 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

QuickSort or heapsort with complexity O(nlogn)

- weijiang2009 February 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

there is a sorting known as Bitonic sort and the pattern mentioned here is known as Bitonic sequence.

- Gaurav Gupta February 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

but bitonic sort is a kind of invert merge, where u start from end of 2nd array, rather than from the start of 2ns array, but still u need extra space.... u don't have that....

- Ankit Manocha February 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if we increase the size of array by 5 times then what change of time complexity of that array

- manoj kumar March 05, 2011 | 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