Facebook | Phone Screen | Valid Palindrome with max k deletions

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2021-12-26Added By Anonymous
Source: Link

Company: Facebook
Role: unknown
Interview Stage: Phone Screen
Location: unknown
Year: unknown

Original Post Content:

I had phone screen round in November and was asked Valid Palindrome II.
I was quite quick to present the O(n) time & space solution and maybe that's why interviewer went with the follow-up instead of 2nd question.

Follow up question was with max k deletions if we can make a string palindrome or not.

Eg - abceda, k = 2 , Output - False
abccda, k = 2, Output - True

Solution - I followed the same approach but then suddenly realized this is DP question and was surprised considering fb don't ask one.

My approach was to start from beginning and end towards the center, and whenever there is a mismatch, spin off two branches from there and reduce deletion count by 1. When we reach middle/or end < start, then we check if k >= 0, if yes, then we return True else False. We can short circuit by checking if k is negative and return False right away.
This would be O(2^n) in worst case, with DP, it could go to O(n^3) [I was storing start, end, index as the key]

Your thoughts? Could it be brought down to O(n^2) ?

Comments