So here is the question:
Suppose you have 100 thousand integers which ranges from 1 to 1 million. Please sort out the integers. The time complexity should be O(n).
Anybody who shares his or her ideas is well appreciated.
Also, these algorithms are used to test the understanding of a software engineer on whether or not he knows the working of the code. Practising these problems before an interview will not only make you familiar with them but also give you confidence in explaining the solution to the interviewer.
It is easy to understand. An algorithm is a step-wise representation of a solution to a given problem. In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.
Sounds like a straightforward counting sort.
Space is constant. Runtime is O(n).
The hint should be that they range from 1 to 1 million.
See pigeonhole sort
Since the problem has fixed size and includes a finite set of instances, any sorting algorithm will terminate in O(1). You should tell the tester to go back to algorithm analysis school. One possible way to generalize this problem to an infinite set is: you have an array of size n with numbers ranging in [0, 10n]. Can you sort it in O(n)? That makes sense to me. Or you could parametrize the problem with the size of the array and the range of the integers and come up with some O(f(n,k)) bound. The problem is when you get a question like this in an interview, what do you do? Do you try to guess what the interviewer would like to hear or do you say something like "let me rephrase your question"? Or you just scoot towards the exit with a big smile?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With