Data Structures & AlgorithmsHardTCS NQT · Accenture

To count distinct values in every window of size k of an array in O(n) total time, which structure is most suitable?

  1. A. A hash map of value frequencies updated as the window slides
  2. B. A sorted list rebuilt for each window
  3. C. A binary search tree of all subarrays
  4. D. A matrix of all pairwise comparisons

Correct answer: A. A hash map of value frequencies updated as the window slides

Increment the count when a value enters and decrement it when it leaves; the number of non-zero counts is the distinct count of the window.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic