I recently had an coding interview in one of the FAANG company(can't name at this point),
The Question was "Given a stream of numbers , and an integer "n"(n would be same throughout the stream) output the top 'n' most frequent elements, whenever query is asked in between the stream ('0' input means output "n frequent elements").
Any approach of possible optimal solutions with Time and Space Complexities?
My Brute Force :- I told him we can create a hashmap and store frequencies as we go on inputting the stream , then we keep on sorting on every input and query the k top elements
My Optimal Soultion :- I told him I will use the heaps to store the frequency of hashmap and will heapify on every input element and query top k elements .I couldn't code but was my approach correct?