Data Structures & AlgorithmsHardProduct Company SDE · sliding window OA
Problem: What is the length of the shortest substring of 'aabbcxa' that contains every letter of 'abc'? Run the sliding-window code.
Problem: What is the length of the shortest substring of 'aabbcxa' that contains every letter of 'abc'? Run the sliding-window code.
from collections import Counter
s, t = 'aabbcxa', 'abc'
need = Counter(t)
have = Counter()
missing = len(t)
l = 0
best = float('inf')
for r, ch in enumerate(s):
if need[ch] and have[ch] < need[ch]:
missing -= 1
have[ch] += 1
while missing == 0:
best = min(best, r - l + 1)
have[s[l]] -= 1
if need[s[l]] and have[s[l]] < need[s[l]]:
missing += 1
l += 1
print(best)What is printed?- A. 3
- B. 5
- C. 6
- D. 4
Correct answer: D. 4
The window 'abbc' at indices 1 to 4 contains a, b and c and has length 4. No window of length 3 contains all three letters.
More Coding Interview Problems questions
- Problem: What is the length of the longest substring of 'banana' that occurs at least twice (occurrences may overlap)? Run the code…
- Problem: Subset sums of items [1, 2, 3] are computed with a bitset: reach starts as 1 and each item x does reach |= reach << x. How many…
- Problem: Maximum number of events attended when each event is [start, end] (day-inclusive) and you can attend one event per day. Events…
- Problem: How many decimal digits does 100! have? Run the code. python import math print(len(str(math.factorial(100)))) What is printed?
- Problem: What is the digit sum of 100!? Run the code. python import math print(sum(int(c) for c in str(math.factorial(100)))) What is…
- Problem: Longest path in a weighted DAG from vertex 0 with edges 0->1 (3), 1->2 (4), 0->2 (10). Run the DP over the topological order…