Data Structures & AlgorithmsModerateTCS NQT · string round
Problem: Find the longest palindromic substring length of 'forgeeksskeegfor' by expanding around each centre.
Problem: Find the longest palindromic substring length of 'forgeeksskeegfor' by expanding around each centre.
s = 'forgeeksskeegfor'
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. 7
- B. 12
- C. 4
- D. 10
Correct answer: D. 10
The longest palindrome is 'geeksskeeg' of length 10, centred between the double s. Expanding from every centre covers even and odd palindromes in O(n^2) time.
More Coding Interview Problems questions
- Problem: What does the following print? It counts distinct values in each window of size 3. python from collections import Counter a = [1…
- Problem: Rotate the array [1, 2, 3, 4, 5, 6, 7] to the right by k = 3 using the three-reversal method. What is printed? python a = [1, 2…
- Problem: How many unordered pairs of distinct elements of [1, 5, 3, 9, 7, 2] have absolute difference exactly 2? Run the code. python a =…
- Problem: Minimum cost to climb stairs where cost[i] is paid when standing on step i and you may take one or two steps. Starting positions…
- Problem: Houses in a circle hold the amounts [2, 3, 2]. You may not rob two adjacent houses, and the first and last are adjacent. Run the…
- Problem: What is the length of the longest common subsequence of 'ABCBDAB' and 'BDCABA'? python a, b = 'ABCBDAB', 'BDCABA' dp = [[0]…