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'))- A. False True True
- B. True False True
- C. True True False
- 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
- What is printed? python def removek(num, k): st = [] for d in num: while k and st and st[-1] > d: st.pop() k -= 1 st.append(d) st =…
- What is printed? python def divcount(a, k): cnt = {0: 1} s = 0 ans = 0 for x in a: s = (s + x) % k ans += cnt.get(s, 0) cnt[s] =…
- What is printed? python print(sorted(['b', 'A', 'a', 'B']), sorted(['b', 'A', 'a', 'B'], key=str.lower))
- What is printed? python print('a-b-c-d'.rsplit('-', 2), 'a-b-c-d'.split('-', 1))
- What is printed? python print('²'.isdigit(), '²'.isdecimal(), '٣'.isdecimal())
- What is printed? python m = ['abc', 'def'] print([''.join(t) for t in zip(m)])