Yahoo Interview Question for Software Engineer / Developers


Country: United States




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

We should ask how many values are in the array. If we have millions of elements, counting sort is the way to go since the range fits easily in memory. Otherwise, I would pick quick sort.

- Miguel Oliveira July 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes suppose the values start from 50,000 to 50000000. Since these are huge set of values, so I would also pick up counting sort as algorithm

- vgeek July 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

Miguel is right. The question is incomplete in the sense the total number of elements to be sorted are not mentioned. The choice of sorting algorithm depends on how many numbers are to be sorted and the specifics of the data, namely their distribution, frequency etc..

- Murali Mohan July 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 vote

This is how I would answer. Not sure if it is right though :)

1) Merge sort: If the data is too big to fit into memory, I will use something like external k-way merge sort. - O(nlogn)
2) Insertion sort: If the data is almost sorted, this is the way to go !
3) Quick sort: If the data is almost unsorted and is small to fit into memory and extra space is not allowed, this is the way to go !
4) Counting sort: If additional O(n) space is allowed, data is almost unsorted and small enough to fit into memory and most importantly, the difference between numbers is much lesser than the number of different numbers, this is the way to go !
5) Bubble sort: if the data is small enough to fit in memory, almost sorted in ascending order, this can be used. But I will still incline to use insertion sort.

- king July 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1) I don't think a k-way merge sort has O(n log n) like a traditional one.
4) Not O(n) space but O(range), where range is 50 000 000 - 50 000

- Miguel Oliveira July 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1 d

- pirate July 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ Miguel Oliveira for your 4th point its actually O(n+k) space where k is range

- MrA August 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@MrA, I was talking about king's 4th point which refers O(n) *additional* memory. Your "k" is the additional part, same as my O(range).

- Miguel Oliveira August 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This question would be best answered with more questions to narrow down the problem. Asking things like:

Where is the data coming from?
How will the data be used?
etc.

This will give you a better understanding of if the data is partially sorted or not and later of implementation, if the data needed to be randomly accessed. You can then sniff out if the worse case is for the sort you will be using will ever be reached and if you could optimize better with a sort because the data is identical to that of a best case.

- Jordan Smith August 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Vgeek,
Can you please tel me other questions asked in Yahoo as well.

- prashantpp July 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Quick sort can have a worst-case running time of O(n^2) if we don't implement the partitioning function properly. For instance, if we are so unlucky to pick the longest as the pivot! I think mergesort is a good call since it is very stable. Counting is good but the range of value is too big. It needs to additional spaces of O(array-size) and O(max(array-element)).

- Anonymous July 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Quick sort has a worst-case running time of O(n^2), no matter how good your partition function is (unless the number distribution is known in advance). Good partition functions can decrease the probability of hitting O(n^2) on a random input.

- Miguel Oliveira July 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes that would guarantee O(n log n), but I don't think it's possible to pick the median in O(1) throughout the algorithm without some special knowledge about the input.

- Miguel Oliveira July 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

No one has mentioned that the values will not fit into an unsigned 4 byte integer (int). You can use an 8 byte integer though. But there can be an excessive cost associated with using 8 byte integers, depending on the h/w.

But deciding which sort to use really depends on whether the amount of data to sort, as well as whether it is likely to be already partially sorted.

- dsalinas1973 November 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the values go up to 50 million whereas the unsigned 4 byte int holds values in the order of 4 billion..

- Miguel Oliveira November 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry, I read to many 0's. I had read 5 billion

- dsalinas1973 November 08, 2013 | 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