Data Structures & AlgorithmsModerateTCS NQT · string round
Problem: How many (possibly overlapping) occurrences of the pattern 'ana' are in 'banana'?
Problem: How many (possibly overlapping) occurrences of the pattern 'ana' are in 'banana'?
s = 'banana' p = 'ana' print(sum(1 for i in range(len(s) - len(p) + 1) if s[i:i + len(p)] == p))What is printed?
- A. 1
- B. 3
- C. 4
- D. 2
Correct answer: D. 2
The occurrences start at index 1 and index 3, and they overlap at the shared 'a'. Non-overlapping counting would also give 1, which is the trap.
More Coding Interview Problems questions
- Problem: Each element of [2, 3, 1, 1, 4] is the maximum jump length from that index. What is the minimum number of jumps to reach the last…
- Problem: A circular route has gas = [1, 2, 3, 4, 5] and cost = [3, 4, 5, 1, 2] (cost to travel from station i to i+1). Find the start…
- Problem: What is the minimum number of intervals to remove so that the rest are pairwise non-overlapping (touching at an endpoint is…
- Problem: The prefix sums of [2, 5, 1, 7, 3] are stored in a list p. What is printed by the code, which finds the first index whose prefix…
- Problem: Arrange the numbers 3, 30, 34, 5, 9 so that the concatenation is the largest possible. Run the code and state the printed string…
- Problem: How many integers x with 1 <= x <= 100 satisfy x^3 ≡ x (mod 7)? python print(sum(1 for x in range(1, 101) if pow(x, 3, 7) == x %…