string-rotation
interview-questions
coding-interview
programming
algorithms

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:

  1. check that the strings have the same length
  2. concatenate s1 with itself
  3. test whether s2 is a substring of the doubled string

Python Example

python
1def is_rotation(s1: str, s2: str) -> bool:
2    if len(s1) != len(s2):
3        return False
4    return s2 in (s1 + s1)
5
6
7print(is_rotation("waterbottle", "erbottlewat"))  # True
8print(is_rotation("hello", "llohe"))              # True
9print(is_rotation("hello", "lloeh"))              # False

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) to O(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.

python
1def slow_is_rotation(s1: str, s2: str) -> bool:
2    if len(s1) != len(s2):
3        return False
4
5    for i in range(len(s1)):
6        rotated = s1[i:] + s1[:i]
7        if rotated == s2:
8            return True
9    return False

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 + s1 allocates a new string.

Summary

  • A string rotation can be detected by checking whether s2 is inside s1 + 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.

Course illustration
Course illustration

All Rights Reserved.