Data Structures & AlgorithmsVery hardTCS Digital · Infosys SP
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 ['abca', 'abccdba', 'abcda', 'eeccccbebaeeabebccceea']])- A. [True, False, False, False]
- B. [True, False, True, False]
- C. [True, True, False, False]
- D. [False, False, False, False]
Correct answer: C. [True, True, False, False]
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: [True, True, False, False].
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
- How many contiguous subarrays of [4, 3, 1, 7, 5, 7, 4, 3, 2, 1] contain exactly 2 odd numbers? (Use exactly(k) = atMost(k) − atMost(k − 1).)
- Find the index of the first non-repeating character using a frequency table (−1 if none). What is printed? python from collections import…
- An equilibrium index i has (sum of elements left of i) = (sum of elements right of i), excluding a[i]. Which indices of [−4, 5, 8, −4, −4…
- A hash table with open addressing has load factor α = 0.75 and uniform hashing. What is the expected number of probes for an UNSUCCESSFUL…
- Insertion sort is run on an array of n elements that has exactly k inversions. What is its running time?
- A queue is built from two stacks: enqueue pushes onto IN; dequeue pops from OUT, first moving every element of IN onto OUT only when OUT…