Data Structures & AlgorithmsHardProduct Company SDE · Infosys OA
Problem: A string s = "abacaba" is given. How many of its substrings are palindromes of odd length (counting index ranges, so repeated strings at different positions count separately)?
- A. 7
- B. 16
- C. 5
- D. 12
Correct answer: D. 12
Expanding around the 7 single-letter centres finds 7 + 3 + 1 + 1 = 12 odd palindromes (letters, aba/aca/aba, bacab, abacaba). Counting distinct strings gives 7, counting all odd-length substrings gives 16, and skipping the single letters gives 5.
More Coding Interview Problems questions
- Problem: A directed graph has 4 vertices (0–3) and arcs with weights: 0→1 (4), 1→2 (−3), 0→2 (2), 2→3 (1), 1→3 (5), 3→1 (−1). Start with…
- Array a = [4, 2, 6, 1, 5, 3, 3, 9, 2]. How many contiguous subarrays of length at least 3 have a sum divisible by 6?
- Array a = [3, 5, 8, 2, 7, 10, 4]. How many pairs of positions i < j have a[i] + a[j] equal to some value located strictly after position j?
- Sorted array b = [1, 2, 3, 4, 5, 7, 8, 9]. What is the largest product b[i] b[j] over index pairs with j - i >= 3?
- Sorted array [1, 3, 4, 6, 8, 9, 11]. How many index triples i < j < k satisfy c[k] - c[i] <= 4?
- How many strings of length 5 over the alphabet {a, b, c} have no two adjacent characters equal?