Data Structures & AlgorithmsModerateAmazon · Microsoft OA
Problem: Is s made of one substring repeated two or more times? Trick: check whether s occurs in (s + s) with the first and last characters removed.
Problem: Is s made of one substring repeated two or more times? Trick: check whether s occurs in (s + s) with the first and last characters removed.
def rep(s):
return s in (s + s)[1:-1]
print(rep("abab"), rep("aba"), rep("abcabcabc"), rep("a"))What is printed?- A. True False True True
- B. True True True False
- C. True False True False
- D. False False True False
Correct answer: C. True False True False
If s = uᵏ (k ≥ 2), rotating by |u| reproduces s, so s appears inside s + s away from both ends. 'aba' has no such rotation. A single character cannot be split into two or more copies; (s+s)[1:-1] = '' does not contain 'a'.
More Coding Interview Problems questions
- Problem: Delete the fewest characters so that no two letters have the same (nonzero) frequency. python from collections import Counter def…
- Problem: An array of n + 1 integers holds values 1..n with exactly one value repeated (possibly several times). Find it in O(1) extra…
- Problem: Find all values appearing more than ⌊n/3⌋ times with the extended Boyer–Moore vote (two candidates), then verify. python def…
- Find the bug: Boyer–Moore majority vote is used WITHOUT the verification pass on an array that may have no majority. python def…
- A 'shuffle' swaps each position i with a position chosen uniformly from the WHOLE array (0..n−1), for i = 0..n−1. Why is this not a…
- What is the time complexity of this loop? python for i in range(1, n + 1): j = i while j <= n: work() # O(1) j += i