You are given words (strings) either as a finite list or as an unbounded stream.
-
List version
: Given an array
words
and an integer
k
, return:
-
the
k
most frequent distinct words, and
-
the
k
least frequent distinct words.
Specify how you break ties (e.g., lexicographically, or any order).
-
Stream version (online)
: Design a data structure that supports:
-
ingest(word)
: process the next word in the stream
-
topK(k)
: return the
k
most frequent distinct words seen so far
-
bottomK(k)
: return the
k
least frequent distinct words seen so far
Discuss time/space complexity and how you would implement these operations efficiently.
-
Variant (discuss/design)
: Extend the stream design to support returning the
first
k
and/or
last k
words that are currently
non-repeating
(i.e., words with frequency exactly 1) according to stream arrival order.