In a per-interview question i have been asked that “How would you sort a list of a billion students, based on their overall score on a test? The roll number of student’s moves from 1-1B and range of marks is 10-100.”
Although any sorting algorithm would do but what would be the efficient one?
In a per-interview question i have been asked that How would you sort a
Share
Simply run counting sort on input, it’s
O(n)in this case, because range is bounded. Also it’s most efficient way because any way to output all of the students takes Ω(n).You can outputs student by looping on them for possible available scores (e.g if 90 possible scores exists, loop through students 90 times, in the first time output students with score 100, ….).
This task can be done by bucket sort. But first you should loop through the inputs, find each scores related student count, after that create a bucket for each score by considering its student count, then filling the bucket, note that you should create an array for bucket, also you should have an extra parameter which saves current item count in each bucket.
First approach (using counting sort directly) is O(n) with O(1) extra space, second approach is O(n) with O(n) extra spaces, but second one is faster, because it’s
2*n, and first one is90*n.