Data Structures & AlgorithmsVery hardGATE CS · DSA
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 ['tebbem', 'racecar', 'cbbcc', 'abcda']])- A. [False, True, True, False]
- B. [False, True, False, True]
- C. [False, True, False, False]
- D. [True, False, True, False]
Correct answer: A. [False, True, True, 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: [False, True, True, 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
- Binary search with mid = (lo + hi) // 2 (lo = 0, hi = n − 1, loop while lo ≤ hi) looks for 16 in [18, 39, 40, 44, 45, 56, 60, 62, 70, 84…
- For the sorted array [3, 3, 4, 5, 5, 7, 7, 8, 8, 8, 8, 10] (0-based), what are lowerbound(5) (first index with value ≥ 5) and…
- How many contiguous subarrays of [−2, 1, −3, −4, 8, 1, 4, −5, 6] have a sum divisible by 3? (Count equal prefix-sum remainders.)
- What does this loop-counting code print? (Python) python n, c = 16, 0 i = 2 while i <= n: c += 1 i = i i print(c)
- An equilibrium index i has (sum of elements left of i) = (sum of elements right of i), excluding a[i]. Which indices of [0, 9, 2, 3, 4, 2…
- A stack is implemented with two queues where push is costly (new element goes into the empty queue, then everything else is moved behind…