DSA interview question
A voice assistant sometimes produces sentences with the words in the wrong order and messy spacing, like " hello world ". A clean-up step must reverse the order of the words, remove leading and trailing spaces, and leave exactly one space between words. The same text utilities module also needs a palindrome check that ignores punctuation and case, so that "A man, a plan, a canal: Panama" counts as a palindrome. Both are two-pointer string problems, and word reversal has a neat O(1)-extra-space trick: reverse the whole string, then reverse each word back.
Example: "the sky is blue" → "blue is sky the".
string reverseWords(string s); // single spaces, no leading or trailing spaces
bool isPalindrome(const string& s); // letters and digits only, case-insensitive
| Input | Output | Explanation |
|---|---|---|
reverseWords("the sky is blue") | "blue is sky the" | |
reverseWords(" hello world ") | "world hello" | Leading and trailing spaces removed |
reverseWords("a good example") | "example good a" | Multiple spaces collapse to one |
reverseWords("single") | "single" | |
reverseWords(" ") | "" | Only spaces |
isPalindrome("A man, a plan, a canal: Panama") | true | Ignoring punctuation and case |
isPalindrome("race a car") | false | |
isPalindrome(" ") | true | No letters or digits: an empty string is a palindrome |
(Confirm: words are separated by spaces only; the palindrome check considers letters and digits and ignores case.)
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