DSA interview question
A marquee sign scrolls text by moving its first character to the end, over and over. Given the current text s and a target goal, can the sign ever show goal? Trying every rotation is O(n²). The elegant observation: every rotation of s appears as a substring of s + s. For example, "abcdeabcde" contains "cdeab". So the answer is "same length, and goal occurs in s + s", which is a single substring search.
Example: ("abcde", "cdeab") → true; ("abcde", "abced") → false.
bool rotateString(const string& s, const string& goal);
Input (s, goal) | Output | Explanation |
|---|---|---|
("abcde", "cdeab") | true | Rotate by 2 |
("abcde", "abced") | false | Not a rotation |
("aa", "a") | false | Different lengths |
("a", "a") | true | Rotation by 0 |
("abab", "baba") | true | Rotate by 1 |
(Confirm: a rotation moves characters from the front to the back; zero rotations count.)
s + s contain every rotation?"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