ProgrammingVery hardAmazon OA · Flipkart coding round

What is printed?

What is printed?
def ok(s):
    def chk(l, r):
        while l < r:
            if s[l] != s[r]:
                return False
            l += 1
            r -= 1
        return True
    l, r = 0, len(s) - 1
    while l < r:
        if s[l] != s[r]:
            return chk(l + 1, r) or chk(l, r - 1)
        l += 1
        r -= 1
    return True
print(ok('abca'), ok('abc'), ok('racecar'))
  1. A. False True True
  2. B. True False True
  3. C. True True False
  4. D. False False True

Correct answer: B. True False True

Two pointers stop at the first mismatch and try deleting either end character. Deleting b from abca leaves aca, a palindrome, while abc cannot be fixed by one deletion.

More Python Programming questions

Exams that ask this topic