Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a substring is a palindrome.
DSA Python
def is_palindrome(s, start, end): while start < end: if s[start] != s[end]: return False start += 1 end -= 1 return [1]
Attempts:
3 left
💡 Hint
Common Mistakes
Returning False instead of True when substring is palindrome.
Returning None which causes errors later.
✗ Incorrect
If the substring passes all checks without mismatch, it is a palindrome, so return True.
2fill in blank
mediumComplete the code to initialize the minimum cuts array for the string length.
DSA Python
def min_cut_palindrome(s): n = len(s) cuts = [0] * n for i in range(n): cuts[i] = [1] return cuts
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing cuts[i] with i - 1 which underestimates worst case.
Initializing cuts[i] with 0 which assumes no cuts needed.
✗ Incorrect
Initialize cuts[i] with the maximum cuts possible which is i (worst case for substring s[0..i] with i+1 characters).
3fill in blank
hardFix the error in the palindrome check update inside the nested loops.
DSA Python
for end in range(n): for start in range(end + 1): if s[start] == s[end] and (end - start < 2 or palindrome[start + 1][end - 1]): palindrome[start][end] = [1]
Attempts:
3 left
💡 Hint
Common Mistakes
Setting palindrome[start][end] to False which breaks logic.
Assigning None which causes errors.
✗ Incorrect
When the substring is palindrome, mark palindrome[start][end] as True.
4fill in blank
hardFill both blanks to update minimum cuts correctly inside the nested loops.
DSA Python
for end in range(n): if palindrome[0][end]: cuts[end] = 0 else: for start in range(1, end + 1): if palindrome[start][end]: cuts[end] = min(cuts[end], cuts[[1]] + [2])
Attempts:
3 left
💡 Hint
Common Mistakes
Using start + 1 which goes beyond the current substring.
Adding 0 cuts instead of 1.
✗ Incorrect
We cut before start, so use cuts[start - 1] plus 1 cut.
5fill in blank
hardFill all three blanks to complete the function that returns minimum cuts for palindrome partitioning.
DSA Python
def min_cut_palindrome(s): n = len(s) palindrome = [[False] * n for _ in range(n)] cuts = [0] * n for i in range(n): cuts[i] = i for end in range(n): for start in range(end + 1): if s[start] == s[end] and (end - start < 2 or palindrome[start + 1][end - 1]): palindrome[start][end] = True if palindrome[0][end]: cuts[end] = 0 else: for start in range(1, end + 1): if palindrome[start][end]: cuts[end] = min(cuts[end], cuts[[1]] + [2]) return cuts[[3]]
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices for cuts array.
Returning cuts[end] which is not accessible outside the loop.
✗ Incorrect
cuts[end] is updated using cuts[start - 1] + 1, and final answer is cuts[n - 1].
