Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
Count distinct values in every window of size k with a frequency map. What is printed?
Count distinct values in every window of size k with a frequency map. What is printed?
from collections import Counter
a, k = [3, 3, 2, 1, 3, 2, 3, 1, 2], 3
c, out = Counter(a[:k]), []
out.append(len(c))
for i in range(k, len(a)):
c[a[i]] += 1; c[a[i - k]] -= 1
if c[a[i - k]] == 0: del c[a[i - k]]
out.append(len(c))
print(out)- A. [2, 3, 3, 3, 2, 3, 3]
- B. [2, 3, 3, 3, 3, 3, 3]
- C. [2, 3, 3, 2, 3, 3, 3]
- D. [2, 3, 3, 3, 3, 2, 3]
Correct answer: A. [2, 3, 3, 3, 2, 3, 3]
Keys whose count drops to 0 must be deleted, otherwise len(c) still counts values that left the window. Output: [2, 3, 3, 3, 2, 3, 3].
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A min-stack keeps a second stack of running minimums. What is printed? python st, mn, out = [], [], [] for op in [7, 5, 7, 5, 5, "pop"…
- A hash-map two-sum returns the first index pair (i, j), i < j, found while scanning left to right. What is printed? python def twosum(a…
- A dynamic array doubles its capacity when full, copying all elements. Starting from capacity 1, what is the total number of element copies…
- Evaluate the postfix expression 7 4 1 ÷ 7 + 6 × 2 × + 3 − (all divisions are exact). What is its value, and what is the maximum number of…
- Asteroids move along a line: positive values move right, negative move left; on collision the smaller (by absolute value) explodes, equal…
- One Lomuto partition (pivot = LAST element; i marks the end of the 'less than pivot' region; when a[j] < pivot, increment i and swap a[i]…