Interview question Check if one string is a rotation of other string
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This interview question asks whether one string can be obtained by rotating another string. It is a good test of pattern recognition because the best solution is not to simulate every rotation, but to spot a simpler property that all rotations share.
Use the Concatenation Insight
If s2 is a rotation of s1, then s2 must appear inside s1 + s1. For example, if s1 is waterbottle, then doubling it gives waterbottlewaterbottle, which contains every possible rotation of the original string.
That means the algorithm is:
- check that the strings have the same length
- concatenate
s1with itself - test whether
s2is a substring of the doubled string
Python Example
This is short, readable, and already optimal enough for most interview settings.
Why It Works
Every rotation splits a string into two pieces and swaps their order. If s1 is written as xy, then any rotation looks like yx. The doubled string xyxy always contains yx across the boundary between the two copies.
That is the core proof idea interviewers are usually looking for. The trick is not memorizing the answer, but seeing why doubling captures every rotation.
Complexity
Let n be the string length.
- length check is
O(1) - concatenation is
O(n) - substring search is typically
O(n)toO(n^2)depending on implementation details, but usually treated as linear or near-linear in interview discussion
In practice, the overall solution is considered O(n) with O(n) extra space for the doubled string.
Edge Cases to Mention
A strong interview answer should call out:
- different lengths means immediately false
- two empty strings can be considered rotations of each other
- identical strings are valid rotations
- repeated characters do not break the method
Mentioning these cases shows that you are thinking beyond the happy path.
Alternative but Worse Approach
You could rotate the first string one position at a time and compare after each rotation, but that is less elegant and usually slower.
This works, but it creates many intermediate strings and misses the cleaner observation.
What Interviewers Usually Want to Hear
In a coding interview, the strongest answer usually combines the code with the reason it works. Saying "double the string and search" is good, but explaining the rotation property behind it is what makes the answer complete.
Common Pitfalls
- Forgetting to check that the two strings have the same length.
- Reversing the example mentally and thinking any substring match is enough.
- Writing a brute-force rotation loop when the concatenation trick is simpler.
- Ignoring empty-string behavior and then giving an inconsistent answer.
- Claiming constant space even though
s1 + s1allocates a new string.
Summary
- A string rotation can be detected by checking whether
s2is insides1 + s1. - Equal lengths are required before doing the substring check.
- The concatenation approach is the standard interview solution.
- It is cleaner and usually faster than generating every rotation manually.
- Explaining why the doubling trick works is as important as writing the code.

