DSA interview question
You're working on a "smart undo" feature for a text editor that auto-completes symmetric brackets/tags. Specifically, given a string s, your job is to find the shortest palindrome you can build by adding characters only at the front of s. Unlike a previous task where you only needed the count of characters to add, here you need to return the actual resulting palindrome string.
Example: s = "aacecaaa" → adding "aa" to the front gives "aaaacecaaa", which is a palindrome — and it's the shortest possible.
string shortestPalindrome(string s);
Input s | Output | Explanation |
|---|---|---|
"aacecaaa" | "aaacecaaa" | Add "aa" at front (wait — verify with code, see note below) |
"abcd" | "dcbabcd" | Add "dcb" at front |
"" | "" | Empty string is already a palindrome |
"a" | "a" | Single char is already a palindrome |
"aaaa" | "aaaa" | Already a palindrome, nothing to add |
"abb" | "bbabb" | Add "bb" at front |
"abcde" | "edcbabcde" | Add "edcb" at front |
(Always run/trace through the actual algorithm to confirm exact output strings before an interview — don't trust hand-traced examples blindly.)
s, you can add characters only at the front. Return the shortest palindrome you can form."s? (Doesn't change the length of the answer, but does change what string you'd construct if there were any ambiguity — though here the answer is actually deterministic regardless.)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