Data Structures & AlgorithmsModerateProduct Company SDE · sliding window OA
Problem: Count the start indices i where a permutation of 'abc' begins in s = 'cbaebabacd' (the anagram-window problem). Run the code.
Problem: Count the start indices i where a permutation of 'abc' begins in s = 'cbaebabacd' (the anagram-window problem). Run the code.
from collections import Counter s, p = 'cbaebabacd', 'abc' need = Counter(p) print(sum(1 for i in range(len(s) - len(p) + 1) if Counter(s[i:i + len(p)]) == need))What is printed?
- A. 1
- B. 3
- C. 2
- D. 4
Correct answer: C. 2
The windows 'cba' at index 0 and 'bac' at index 6 are anagrams of 'abc'. Checking only the first window gives the trap 1.
More Coding Interview Problems questions
- 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 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…