Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: What is the length of the longest palindromic substring of 'abacdcaba'? Run the code.
Problem: What is the length of the longest palindromic substring of 'abacdcaba'? Run the code.
s = 'abacdcaba'
best = 0
for c in range(2 * len(s) - 1):
l, r = c // 2, c // 2 + c % 2
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
best = max(best, r - l - 1)
print(best)What is printed?- A. 11
- B. 9
- C. 7
- D. 3
Correct answer: B. 9
Expanding around the centre 'd' reaches both ends, so the whole string (length 9) is a palindrome. Using r − l + 1 after the loop gives 11, the inner "bacdcab" is 7, and "aba" is 3.
More Coding Interview Problems questions
- Problem: How many integers in [1, 10000] have a digit sum that is divisible by 7? Run the code. python print(sum(1 for x in range(1…
- Problem: How many integer solutions (a, b) with 1 <= a <= b <= 20 satisfy a b = 36? Run the code. python print(sum(1 for a in range(1, 21)…
- Problem: What is the length of the longest common subsequence of 'abcdef' and 'acebdf'? Run the DP. python a, b = 'abcdef', 'acebdf' dp =…
- Problem: What is the number of subsets of {1, 2, ..., 8} whose elements sum to a multiple of 8? Run the code. python print(sum(1 for m in…
- Problem: How many pairs (i < j) of [1, 4, 9, 16, 25] have a[j] - a[i] divisible by 3? Run the code. python a = [1, 4, 9, 16, 25]…
- Problem: What is the number of set bits in 2^30 - 1? Run the code. python print(bin(2 30 - 1).count('1')) What is printed?