DSA interview question
You're building a "Did You Mean?" autocomplete-style typo-correction feature for a search engine. Internally, the team noticed that a surprising number of malformed queries become palindromes if you just prepend a few characters at the front (this is a real trick used to detect minimal corruption in symmetric tokens/IDs). Your task: given a string A, find the minimum number of characters you need to insert at the front of A to make the entire string a palindrome.
Example: "AACECAAAA" — if you prepend "AAAA" reversed appropriately, it becomes a palindrome. You need to find that minimum count.
int minCharsToMakePalindrome(string A);
Input A | Output | Explanation |
|---|---|---|
"ABC" | 2 | Insert "CB" at front → "CBABC" |
"AACECAAAA" | 2 | Insert "AA" at front → "AAAACECAAAA" |
"AACECAAAAA" | 0 | Already... wait, check — not a palindrome, verify with code |
"A" | 0 | Single character is always a palindrome |
"" | 0 | Empty string is trivially a palindrome |
"AAAA" | 0 | Already a palindrome |
"ABCD" | 3 | Insert "DCB" at front → "DCBABCD" |
(Always verify edge cases like empty string / single char / already-palindrome in your head before coding.)
1e5 or more, brute force O(n²) char comparison will TLE — signals you need an O(n) approach like KMP.)On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in