Cracking The Coding Interview Together – Problem 1.9: String Rotation

In this chapter of Cracking the Coding Interview — Together, we break down the classic String Rotation problem exactly the way you’d reason through it in front of an interviewer — with clear thinking steps, verbal reasoning, and a safe, simple Java solution you can rely on.
Cracking The Coding Interview Together – Problem 1.9: String Rotation
Photo by Valentin Bolder / Unsplash

Today, we’re tackling one of those problems that looks too easy, which is exactly why interviewers love it. It tests whether you truly understand strings or whether you’re just hoping for divine intervention during interviews.

This is String Rotation, a beautiful conceptual problem that tests whether you can see patterns inside strings. And just like always, let’s break this down together — step by step, thought by thought — exactly how you would in a real interview.

As always, let’s follow our ritual.

Let’s get into today's problem! We always follow the format as follows

  1. Understand the problem statement together
  2. Ask any questions, that you would like to ask the interviewer
  3. I would urge you to take your time here and try to come up with your own solution before going forward
  4. Think how you would go about solving the problem(you would be talking out loud in front of the actual interviewer)
  5. Write code and understand how it works and explain it to your interviewer

Problem statement

String Rotation: Assume you have a method is Substring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to is Substring [e.g., "waterbottle" is a rotation of "erbottlewat" ]

Analyse the problem statement together

We’re given two strings — say s1 and s2. We need to check whether s2 is a rotation of s1.

Example:
"waterbottle""erbottlewat" is a rotation.

This means we can take "waterbottle", rotate some part of it around, and we should get the second string.

But here's the twist:
We’re only allowed one call to a method called isSubstring(s1, s2) that tells us if one string occurs inside another. That's it. No extra heroics.

Take some time and try to dig deep to see if there is anything unclear to you.

Questions for the interviewer

This is the important part where you must take some time to clarify your concerns/doubts/questions with the interviewer. Take a moment to think what you would like to ask and compare it with mine.

  1. Null constraints on strings - whether any string can be null or not?
  2. Empty constraints strings
  3. Do we have to also implement isSubstring(s1, s2) or can we assume that it is already provided?

Think about the Solution

Let's try to understand how we can solve this problem and before that try to understand what Rotation really means.

What Does “Rotation” Actually Mean?

This is where your interviewer is secretly checking whether you can visualize the underlying structure.

Let’s play with the example:

waterbottle

Rotations include:

aterbottlew
terbottlewa
erbottlewat

and so on.

Every rotation is essentially:

Cut the string at some index and swap the halves.

Example:

waterbottle  -> split after "wa"  ->  "terbottle" + "wa"
                                             |
                                           rotation

Now pause for a moment.

Look at the rotation again:

s2 = "erbottlewat"

Try doubling s1:

waterbottlewaterbottle

Do you see something?

If you search for "erbottlewat" inside that doubled string, it actually appears in full.

Boom.

This is the core trick.
This is the entire soul of the problem.

Whenever a string is rotated, the rotation will always exist as a substring inside the original string doubled.

Let’s express the rule:

If s2 is a rotation of s1:
    then s2 will always be a substring of s1 + s1

That’s the magic sentence that the interviewer wants to hear.

Why does this work?

Because doubling the string creates all possible combinations of prefix and suffix merges — which is exactly what rotations are.

Write code and explain

Implementation could be as follows:

public class StringRotation {

    public static boolean isSubstring(String s1, String s2) {
        return s1.contains(s2);
    }

    public static boolean isRotation(String s1, String s2) {
        // Step 1: length check
        if (s1 == null || s2 == null) return false;
        if (s1.length() != s2.length()) return false;

        // Step 2: concatenate s1 with itself
        String doubled = s1 + s1;

        // Step 3: use only ONE call to isSubstring
        return isSubstring(doubled, s2);
    }

    public static void main(String[] args) {
        System.out.println(isRotation("waterbottle", "erbottlewat")); // true
        System.out.println(isRotation("hello", "llohe")); // true
        System.out.println(isRotation("hello", "lohel")); // true
        System.out.println(isRotation("hello", "helol")); // false
    }
}

Time & space complexity

  1. We must touch each character of the string one time which effectively translates to O(n) where n is the length of the String
  2. Since we have doubled the String i.e. 2n, the effective space complexity will be - O(n)

Final Thoughts

This problem is one of those “aha!” problems that are meant to reveal whether you can notice hidden patterns inside simple structures. Once you see the doubling trick, it feels almost too easy — but the interviewer isn’t judging you on the code; they’re judging you on how you talk through it.

And with the approach we covered today, you're not just solving the problem — you're showing the interviewer that you can dissect and understand the deeper behavior of strings, not just manipulate them blindly.

As always, think out loud, reason clearly, and trust your patterns.

See you in the next one, my friend.
We’ll crack that together too. 🚀

Happy Coding!