Data Structures & AlgorithmsVery hardFlipkart/Uber OA · placement

Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is printed?

Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is printed?
def ok(s, skip=True):
    i, j = 0, len(s) - 1
    while i < j:
        if s[i] != s[j]:
            return skip and (ok(s[i + 1:j + 1], False) or ok(s[i:j], False))
        i += 1; j -= 1
    return True
print([ok(w) for w in ['abcda', 'eeccccbebaeeabebccceea', 'abca', 'cbbcc']])
  1. A. [False, False, True, True]
  2. B. [False, False, False, False]
  3. C. [False, True, False, True]
  4. D. [False, False, True, False]

Correct answer: A. [False, False, True, True]

On the first mismatch both choices (drop the left or drop the right character) must be tried; trying only one side misses cases like 'cbbcc'. Output: [False, False, True, True].

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic