Apple Interview Question
Senior Software Development EngineersCountry: United States
Interview Type: Phone Interview
for given size if it is less than, said 10, go for simple sort algorithm like insert, more than that, go for Quicksort.
Reason for Quicksort:
1. it is the fastest in real world (although theoretically merge sort/heap sort could always provide NlogN while Quicksort in worst case is N*N, but that rarely happen).
2. Quicksort doesn't need additional space compared to Merge sort.
Bad thing about Quicksort: It is not a in place sorting algorithm (meaning if two elements are already sorted at some point they could be swapped again later before algorithm finally ends).
I think more than the answer, the reason would be important. Quick sort for example is important because its average runtime is O(nlog(n)) and in most cases its better than other logarithmic algorithms ( Merge Sort and Heap Sort ). I would ask him though, why would you want to stop at implementing just one algorithm. How about an API that can use sort based on input size ( strategy design pattern ); because for different values of n different sorting algorithms can be usefule
- Chitresh June 29, 2013