K Most Frequent Words Problem Statement
Given an array or list WORDS
of 'N' non-empty words and an integer 'K', identify the 'K' most frequent words and return them sorted by their frequency, in descending order.
Note:
If two words share the same frequency, return them in lexicographical order.
Additionally, consider optimizing your solution to achieve O(N * logK)
time complexity with O(N)
additional space.
Input:
N K
word1 word2 word3 ... wordN
Output:
wordA wordB ... wordK
Example:
Input:
N = 6, K = 2
WORDS = ['i', 'love', 'leetcode', 'i', 'love', 'coding']
Output:
['i', 'love']
Explanation:
The word 'i' appears twice, as does 'love'. The other words appear only once. Hence, 'i' and 'love' are the most frequent.
Constraints:
1 <= N <= 10^5
1 <= K <=
number of unique words
Time Limit: 1 sec

AnswerBot
4mo
Identify the K most frequent words in a list, sorted by frequency and lexicographical order.
Use a hashmap to store word frequencies
Use a min heap to keep track of K most frequent words
Sort the heap ba...read more
Help your peers!
Add answer anonymously...
Amazon Software Developer interview questions & answers
A Software Developer was asked 1mo agoQ. What is HTML?
A Software Developer was asked 1mo agoQ. What is MySQL?
A Software Developer was asked 2mo agoQ. Given two strings s and t, return true if they are equal when both are typed int...read more
Popular interview questions of Software Developer
A Software Developer was asked 1mo agoQ1. What is HTML?
A Software Developer was asked 1mo agoQ2. What is MySQL?
A Software Developer was asked 2mo agoQ3. What is the system design for the cart feature in an e-commerce website?
Top HR questions asked in Amazon Software Developer
A Software Developer was asked 1mo agoQ1. Tell me about a time you had to get to the root cause of a problem
A Software Developer was asked 5mo agoQ2. What are the short-term and long-term goals for the team or organization?
A Software Developer was asked 5mo agoQ3. Why do you want to work at Amazon?
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

