Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional
Problem: The shortest palindrome obtainable by inserting characters only at the front of "abacd" has what length?
- A. 9
- B. 7
- C. 10
- D. 2
Correct answer: B. 7
The longest palindromic prefix of 'abacd' is 'aba', so only 'dc' must be prepended: 'dcabacd' has length 7 (KMP on s + '#' + reverse(s) finds that prefix). Prepending everything but the first letter gives 2n − 1 = 9, prepending the whole reverse gives 10, and 2 counts inserted characters.
More Coding Interview Problems questions
- Problem: How many distinct palindromic substrings does "abacaba" contain (single characters count as palindromes, and distinct strings…
- Problem: Among all distinct permutations of the letters A, B, C, D, arranged in dictionary order starting from rank 1, what is the rank of…
- Problem: For the words ["flowchart", "flow", "flight", "flower"], what is the length of their longest common prefix?
- Problem: In the string "abciiidef", what is the maximum number of vowels (a, e, i, o, u) in any contiguous substring of length 4?
- Problem: What is the minimum number of insertions plus deletions needed to convert "heap" into "pea" if only insertions and deletions are…
- Problem: How many binary strings of length 8 have no two consecutive 1s and begin with the digit 0?