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']])- A. [False, False, True, True]
- B. [False, False, False, False]
- C. [False, True, False, True]
- 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].
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Items K, L, M, N, O are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- Remove the n-th node from the END of a linked list in one pass, using a dummy node and a gap of n between two pointers. What is printed?…
- In a singly linked list with only a head pointer, which operation CANNOT be done in O(1) time?
- Run-length encoding: each maximal run is written as the character followed by its length. What is printed? python s = "yyyyyyyyxx" out, i…
- In the binary array [0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1] you may flip at most 2 zeros to ones. What is the length of the longest run…
- Delete duplicates from a SORTED linked list. Version A keeps one copy of each value; version B removes every value that appears more than…