sort stream on frequency and if frequency are same then sort based on value and print k value
403

you are given one stream and you have to sort them based on element frequency and if frequency are same then sort based on value and. print value upto k if stream reach k thresold then remove less frequncy element first if freq same for all then remove old element (which comes first in stream).
Asked in Amazon.
for example :

given steam: add(7), add(4), add(8), add(2), add(6), add(7), k = 3
output: [7], [4,7], [4,7,8], [2,4,8], [2,6,8], [7,2,6,]

explaination:
-> [7] - add 7
-> [4,7] - add 4 as both elem frequency same so sort based on value
-> [4,7,8] - add 8 again all elem freq are same so sort by value
-> [2,4,8] - adding 2 will reach k thresold then remove lower frequency elem first if freq are same then remove elem which come first so remove 7 and add 2.
-> [2,6,8] - adding 6 again reach thresold so remove whichever come first in this 4
-> [7,2,6] -> adding 7 will reach 7 frequency = 2, which is more then others so, it comes first and so on..

Comments (1)