Data Structures & AlgorithmsModerateTCS NQT · string basics
Problem: What is the length of the longest run of equal consecutive characters in 'aabbbbcc'? Run the code.
Problem: What is the length of the longest run of equal consecutive characters in 'aabbbbcc'? Run the code.
s = 'aabbbbcc'
best = cur = 1
for i in range(1, len(s)):
cur = cur + 1 if s[i] == s[i - 1] else 1
best = max(best, cur)
print(best)What is printed?- A. 3
- B. 5
- C. 2
- D. 4
Correct answer: D. 4
cur grows while a character repeats and resets to 1 when it changes, so the runs are aa (2), bbbb (4) and cc (2), and best is 4. Resetting cur to 0 instead of 1 undercounts every run by one and prints 3.
More Coding Interview Problems questions
- Problem: How many subsets of {1, 2, 3, 4} have a prime sum (the empty set has sum 0 and is not prime)? Run the code. python from itertools…
- Problem: How many ordered pairs (i, j) with i != j of [1, 2, 3, 4] have an even sum? Run the code. python a = [1, 2, 3, 4] print(sum(1 for…
- Problem: What does the following print? python from collections import Counter print(Counter('mississippi').mostcommon(1)) What is printed?
- Problem: What does the following print? python print(round(1.23456, 2), round(-2.5)) What is printed?
- Problem: What does the following print? python print(max([], default=-1), sum([]), divmod(-7, 3))
- Problem: What does the following print? python print(2 -1, 7 % -3)