Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

if we use 2 element as a pivot (suppose 1st and last element) then we partisan the array into three part and we use quick shot in every partisan

So the new recurrence relation became

T(n) = 3 x T(n/3) + O(n)
we assume the array partisan in equal parts and finding the exact position of pivot is O(n)
now applying master theorem f(n) = O(n); a=3; b=3;
f(n) = O(n ^ (log a/log b))

so complexity is O(nlogn)

Now if we take k-1 pivot element the partisan the array in k part so
T()= k x T(n/k) + O(n)
which complexity is also O(n long)


so irrespective of how many partisan we do the complexity of quick sort remains same.

- Bidhan Pal June 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

Only the base of log will change according to the number of partitions created.
2 partitions - Base of log will be 2
3 partitions - Base of log will be 3
4 partitions - Base of log will be 4


So on

- Nitin Gupta May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correct. It should be noted, though, that all log bases are equivalent up to a constant factor. Also, the performance of quicksort cannot be improved by having more partitions because the constant factor C in front, as in CNlog(base whatever)N, will increase to more than offset anything gained by a larger base for the log.

- Anonymous May 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could you please elaborate the question?

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

In the quicksort algorithm,we select one pivot element and partition the array according to that one and then sort the two partitions.
What will be the changes in complexity if we select more than one pivot and partition the array in 3 or more partitions ??

- Ajax May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

please post the answer!!!and i m new to this (O) big o concept...i can only find the theory part .how to calculate thos big o!!!plz refer me some sites!!!thanks

- Anonymous May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Any of those variants of Quicksort are still NlogN. If you select k pivots, you will get something like (k-1) (base k+ 1) N operations for integer k >= 2. This expression is smallest for k=2 but still O(NlogN) for any constant K because log(base x)N is O(log(base y)N) for all x, y > 1. This is so because log(base x) N = [log (base y) N] / [log (base y) x], and that denominator is constant. So all log runtimes are separated by only a constant factor.

- eugene.yarovoi May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the answer to this question lies in the derivation of complexity for normal quicksort. Following a similar approach should help us in understanding the actual complexity in this case.

- Learner May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we choose two pivots then there will be three divisions, therefore complexity will be O(nlogn/log3). Similarly for three pivots it'll be O(nlogn/log4).

- Anonymous May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't have an answer, but this is what I think: If we partition with 2 pivots, then we make 3 parts and call partition again for each of those three.

function f(N)
          if N <=1 return;
          partition -> Order N
          f(N/3); f(N/3); f(N/3);

I am not sure how to solve this recurrence. Is it NLog(base 3) N?

- Anonymous May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the time complexity of 2 pivot is still O(nlogn)

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

but the lager the size of the unsorted array, the more efficient when 2 or 3 pivots are used.

- Anonymous June 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi dear Bidhan Pal .... I think the complexity depends on number of partitions..if partions is k them base of log will be k...so complexity will be nlog(base k)n

- topcoder June 02, 2012 | 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