Answer: Reddit SWE Interview Question 2024 April | Heaps | on-campus

Answer · Posted Jun 2026

Solution 1: Top K Frequent Words — HashMap + PriorityQueue Approach We can solve this problem efficiently using a HashMap to count word frequencies, followed by a custom-ordered PriorityQueue (Heap): Iterate through the array and populate a HashMap with word -> frequency. Initialize a Min-Heap (PriorityQueue) to store the words. We define a custom comparator: If two words have the same frequency, the word that is lexicographically larger should be placed at the root of the Min-Heap. This is because ...

The full answer & interview discussion are available to premium members.

Log in Create a free account