Amazon Interview Question for Software Engineer / Developers


Country: India




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

it will be nlog(n), only base of log will change from 2 to 3.

- abhishekatuw March 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

then by the similar way, for n-way merge sort it would be nlogn (base n) means O(n), right?

- oni March 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

same doubt as oni;
n-way merge sort -> nlogn (base n) which is O(n) + O(n)-> this for merge step which will result in linear time O(n) time for the mergesort right.
As for k-way merge, O(nlogn (base k))

- S.A.M March 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it does not work that way, if u n-way merge, the there is no divide so it takes O(1), but u have to sort the entire array, if u use insertion sort for that sort, its O(n-square),

- both 3 way merge and 2 way merge, they might be O(n logn) base3 and base 2 respectively but former has a larger constant infront of it, as it grows to n it becomes O(n *n * 1)

- does not matter March 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't get confused by the log base 3. O(nlog_3(n)) is equal to O(nlog_2(n)/log_2(3)) = O(nlog_2(n)) since log_2(3) is a constant. Thus, the 3 way merge sort isn't faster than the traditional mergesort.

- ME June 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@abhishekatuw You said "it will be nlog(n), only base of log will change from 2 to 3"

The base of nlog(n) is 10, not 2. Based on what you said, nlog(n) is using based of 2.

Please explain this. Thanks.

- pingpong May 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@oni : nope in n way merge sortt it will not become O(n) because the merging algo then will not take O(n) like previous .

- geeks March 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

n-way merge sort -> nlogn (base n) which is O(n) + O(n)-> this for merge step which will result in linear time O(n) time for the mergesort right.
As for k-way merge, O(nlogn (base k))

- S.A.M March 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with @geeks .. in 2 way merge sort..in the merging process there are n comparisons and each comparison we compare 2 elements -> n * O(1) = O(n)
in 3 way merge sort , there will be n comparison and each time we compare 3 elements
-> n * O(2) = O(n)
and if k = n, the merging process will be O(n^2) . cos you compare n elements , n-1 elements and so on.

- rkt March 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the interviewer is looking for the Dutch National Flag algorithm!

- RKD March 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

AWESOME ANSWER...I REALLY APPRECIATE IT MAN

- ~@~ April 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It will still be O(nlogn) to the base 2. The reason being, we can represent a 3 way merge sort by T(n) = 3*T(n/3)+O(n). Applying masters theorm yeilds O(nlogn).

If the complexity would have been really decreasing, then why would have people stopped at 2, or 3? They could have gone for higher values on n then.

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

for normal merge sort, recurrence relation comes as t(n)=2t(n/2)+ c.n. 1..... bcoz in merge function per iteration compulsory only one comparison is required ..

in case of 3 way merge ... we require 2 comparisons per iteration hence t(n)=2t(n/2)+ c.n. 2= n + 2.c.n. log n (base 3)...eq(1) where as in 2-way merge t(n)=2t(n/2)+ c.n. 2= n + 1.c.n. log n (base 2)...eq(2)

for some same c , put n= 512 eq (1) gives t(n)= 512 + 2.c.512.log 512 (base 3) =512+5814.64 * c eq (2) gives t(n)= 512 + 1.c.512.log 512 (base 2) =512+4608 * c hence 3 way merge is not better.

i hope this may resolve issue. please correct if mistaken Thanks

- sushant September 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the answer is nlogn is still nlogn

- optimus July 15, 2013 | 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