Given a file, find the ten most frequently occurring words as efficiently as possible
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Finding the ten most frequent words in a file is mostly a streaming and counting problem, not a sorting problem. The efficient solution reads the file incrementally, normalizes tokens consistently, counts with a hash map, and then extracts only the top ten counts rather than sorting everything unnecessarily.
Stream the File Instead of Loading It All
For large files, do not read the whole file into memory just to count words. Process line by line:
This keeps memory usage reasonable because the file content is streamed while only the counts are retained.
Extract the Top Ten Efficiently
Once counting is done, Counter.most_common(10) is the simplest and usually the best answer:
For most practical workloads, this is both fast and readable. Under the hood, the important optimization is that you are not sorting the entire file content, only the map of unique words.
A Full Runnable Example
Putting the pieces together:
This is already efficient enough for a large range of real files and easy to adapt when you need additional filtering rules.
Tokenization Rules Matter
The code above treats contractions such as don't as one word because the regex allows apostrophes. That may or may not match your business rule. Decide early on:
- whether punctuation should be removed
- whether case should be normalized
- whether numbers count as words
- whether stop words should be excluded
Efficiency is meaningless if the tokenization rule is wrong for the analysis you need.
When a Heap Is Useful
If you are not using Counter.most_common(), a heap is a good alternative:
This avoids fully sorting all unique words when you only need a small top slice. That matters more when the vocabulary is huge.
Very Large Inputs and Limits
For a single ordinary text file, the bottleneck is usually I/O plus counting. For truly massive datasets, additional strategies may matter:
- process multiple files in chunks
- merge partial counts later
- use external tools or distributed processing
The main memory cost is the dictionary of unique words. If the text contains millions of distinct tokens, that can still become large even though the file itself is streamed. For that kind of workload, the algorithm remains conceptually the same, but the execution model changes from one in-process dictionary to staged aggregation across chunks or workers.
Common Pitfalls
- Reading the entire file into memory before counting.
- Sorting every counted word when only the top ten results are needed.
- Using inconsistent tokenization rules and then distrusting the final counts.
- Ignoring case normalization and treating
Wordandwordas different tokens accidentally. - Optimizing prematurely with exotic approaches before measuring whether a plain streaming
Countersolution is already sufficient.
Summary
- Read the file line by line to keep memory use under control.
- Normalize words consistently before counting them.
- Use
Counter.update()to accumulate frequencies efficiently. - Use
most_common(10)or a heap to extract only the top results. - Focus on correct tokenization first, then optimize the counting path if the dataset truly demands it.
Related reading
- Given a list of words and a sentence find all words that appear in the sentence either in whole or as a substring
- Good Python modules for fuzzy string comparison?
- Handle context in a chatbot
- How are the TokenEmbeddings in BERT created?
- How can I adapt the Levenshtein Distance algorithm to limit matches to a single word?
- How can I build a model to distinguish tweets about Apple Inc. from tweets about apple fruit?
- How can I Convert HTML to Text in C?
- How can I create a rag chain with langchain using a retriever when having multiple inputs?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.