Apple Interview Question for SDE1s


Country: United States




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

It's an array so I'd do a radix sort. 32 passes and done.

- Noobie January 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This needs O(kn) of time and space, in which k equals 32. Maybe using some O(nlogn) algs is better, because log(1e10) is less than 32.

- Eric October 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

What does " stored in an array" mean? If you are to write a function that takes an array as input then use quicksort - caller has figured out how to get the data in memory already :)

If you are two read the data from disk - then external merge sort is the way to go.

- Mak February 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

External Merge Sort!!!

- Sameer Shukla January 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 votes

Why use *External* merge sort if the elements are all in an array? Use Quicksort.

- Murali Mohan January 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

1 billion integers means 4GB or memory which is a strong indicative that they don't want you to do a traditional array sorting. I, too, believe that this should be solved using external sorting.

- Alin January 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yep, external sorting wins here. I'm pretty sure that this was the way a similiar problem (with additional conditions, of course), presented in the first article of "Programming Pearls" was solved.

- Fares January 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Alin

So with a RAM of more than 16GB on PCs and Smart Phones these days, do you still want to sort 4GB data using *external* merge sort?

- Murali Mohan January 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for the huge amount of data the quick short is the best when data are in worst case. other wise aply merg short.

- abc January 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

quick sort has O(n^2) worst case performance...... that's really the worst you can get with any comparison based sorting algorithm

- chriscow January 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

some one explain me about external sort plzzz??

- abc January 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

google it

- chriscow January 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you assume that the entire array fits in memory, I would choose heap sort since it can be done in place (O(1) additional space) and is guaranteed O(n lgn) time, where as quicksort requires worst case O(n) additional space (maybe the array fits in memory but not the additional space required for quicksort) and takes O(n^2) in the worst case.

Otherwise, if you don't assume the array can entirely fit in memory, external merge sort is a good option.

- c February 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

radix sort ..min time

- vsingla160 October 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why not just using counting sort. allocate 1 billion continuous memory of int with zero. considering each integer as the memory address, increment the corresponding memory address by one for each integer. Print out the memory address by number of times in each address. complexity O(2*n)

- Tony October 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Because it's a billion integers.
If we go by understanding integer data type, we can create an array of size equal to integer data space which is 4GB.
And take a single pass through billion integers to find the count of each and remember them in 4gb array.
Next pass go through 4b array and write the non-zero count numbers back into first array.

- Srinivasa Alamuru March 10, 2016 | 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