algorithm - Is there a significantly better way to find the most common word in a list (Python only) -
considering trivial implementation of problem, looking faster way find common word in python list. part of python interview received feedback implementation inefficient, failure. later, tried many algorithms found, , heapsearch based solutions bit faster, not overwhelmingly (when scaled tens of millions of items, heapsearch 30% faster; on trivial lengths thousand, same; using timeit).
def stupid(words): freqs = {} w in words: freqs[w] = freqs.get(w, 0) + 1 return max(freqs, key=freqs.get) as simple problem , have experience (although algorithms guru or competitive coder) surprised.
of course, improve skills , learn better way of solving problem, input appreciated.
clarification duplicate status: point find out if there (asymptotically) better solution , other similar questions have picked answer not better. if not enough make question unique, close question, of course.
update
thank input. regarding interview situation, remain impression hand written search algorithm expected (that may more efficient) and/or reviewer assessing code point of view of language, different constant factors. of course, can have own standards.
what important me validate if totally clueless (i had impression not) or write not best possible code. still possible better algorithm exists, if remained hidden community here few days, fine that.
i picking upvoted answer - seems fair so, though more 1 people privided usefull feedback.
minor update
it seems using defaultdict has noticeable advantage on using 'get' method, if statically aliased.
that sounds bad interview question, case of interviewer expecting answer. sounds s/he didn't explain s/he asking.
your solution o(n) (where n = len(words)), , using heap doesn't change that.
there faster approximate solutions...
Comments
Post a Comment