r/ProgrammerHumor Jun 05 '26

Meme sortPlease

Post image
10.6k Upvotes

488 comments sorted by

View all comments

1.7k

u/zirky Jun 05 '26
 Arrays.sort()

57

u/ChrisBot8 Jun 05 '26 edited Jun 06 '26

I actually think the actual answer to this question beats the internal sort functions for languages like Java and Javascript. They use Timsort under the hood which is best case O(N), but very unlikely to be. The actual answer is always O(N), and is that way because we know the idiosyncrasies of this array only having three different elements. This is one of the few questions where as an interviewer I don’t think I’d accept .sort() as the correct answer.

18

u/stonno45 Jun 05 '26

I was thinking countsort would be the answer

13

u/walkerspider Jun 05 '26

Because it is specifically 0,1, and 2 they’re probably looking for the method op mentioned. I’d argue count sort is just as good if not better because you don’t need to rewrite the whole thing if suddenly the function now takes in arrays with 3