Reviewed by Aditya Kumar · Last reviewed 2026-03-24
**Why Descending Order Matters:** Top-N by frequency is the core of term frequency analysis, log analysis, and recommendation features (most-viewed items). **Scalability Tiers:** (1) Single file < 1GB: Counter + most_common()—in-memory, O(n log k) for top-k. (2)...
Why Descending Order Matters: Top-N by frequency is the core of term frequency analysis, log analysis, and recommendation features (most-viewed items).
Scalability Tiers: (1) Single file < 1GB: Counter + most_common()—in-memory, O(n log k) for top-k. (2) Multi-file/large: MapReduce pattern—map emits (word,1), reduce sums, final sort. (3) Spark: reduceByKey, then sortBy(col('count').desc()). (4) Streaming: maintain heap of top-K—O(n log k) space.
Cost: Full sort of 10M distinct words vs heap of top 1000—heap wins. Use most_common(1000) to avoid full sort when only top-N needed.
from collections import Counter
counts = Counter(words)
for word, count in counts.most_common():
print(f'{word}: {count}')
Red Flag: Sorting entire dict when only top-N needed. Pro-Move: 'We use most_common(5000) and persist to Redis for real-time trending—full sort runs weekly for analytics.'
According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.