Given the trivial implementation of the problem, I'm looking for a much faster way to find the most common word in the Python list. As part of a Python interview, I received feedback that this implementation is so inefficient that it is basically a failure. Later I tried many algorithms that I found, and only some heapsearch-based solutions are a little faster, but not in the vast majority (when scaling to tens of millions of units, heapsearch is about 30% faster, at trivial lengths such as thousands, it's almost same using timeit).
def stupid(words): freqs = {} for w in words: freqs[w] = freqs.get(w, 0) + 1 return max(freqs, key=freqs.get)
Since this is a simple problem, and I have some experience (although I am not a guru algorithm or a competitive encoder anywhere), I was surprised.
Of course, I would like to improve my skills and find out that there is a much better way to solve the problem, so your contribution will be appreciated.
Clarification for recurring status: my task is to find out if there is actually a lot (asymptotically) a better solution, and other similar questions have picked up an answer that is not much better. If this is not enough to make the question unique, be sure to close this question.
Update
Thanks to everyone for input. Regarding the interview situation, I am left with the impression that the expected manual search algorithm (which may be somewhat more efficient) and / or the reviewer evaluated the code from the point of view of another language with different constant factors. Of course, everyone can have their own standards.
It was important for me to check how completely shameless I was (I got the impression that this was not so) or they just wrote not the best code. It is still possible that an even better algorithm exists, but if it has remained hidden to the community here for several days, I am fine with that.
I choose the most correct answer - it seems to be true, although more than one person shared useful feedback.
Minor update
Using defaultdict seems to have a noticeable advantage over using the get method, even if it is statically smoothed.