Reported September 2026
Infosysstring

Detect Substring In Any Rotation

Reported by candidates from Infosys's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Infosys OA. Under 2s to a working solution.
Founder's read

Infosys reported this one in September 2026, and the input size is the whole story. Both strings can hit 10^4 characters, so generating every rotation and scanning each one is about 10^8 character comparisons before you even count the string building. That's the trap. The question is called "Detect Substring In Any Rotation" and it's a string matching problem wearing a rotation costume. If you see the doubling trick, it's a few lines. If you blank on it, StealthCoder runs invisibly on your screen during the live OA and hands you the approach so you don't freeze.

The problem

You are given two strings s and needle consisting of lowercase English letters.
A rotation of s is any string formed by moving a prefix of s to its end without changing character order. The original string is a rotation of itself.
Return true if needle occurs as a contiguous substring of at least one rotation of s, and false otherwise.
The empty needle occurs in every rotation, including the empty string.

Function
substringInAnyRotation(s: String, needle: String) → boolean

Examples
Example 1
s = "absdsdf"
needle = "fab"
return = true
Moving the prefix absd to the end produces sdfabsd, which contains fab. Equivalently, fab occurs in s + s.
Example 2
s = "absdsdf"
needle = "xyz"
return = false
No rotation of absdsdf contains xyz.

Constraints
0 <= s.length <= 10^4.
0 <= needle.length <= 10^4.
s and needle contain only lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is in Example 1 itself: every rotation of s is a substring of s + s. So the question becomes whether needle appears in s + s, with one catch. A needle longer than s can't fit in a single rotation, so return false when needle.length > s.length, otherwise you get false positives from the doubled string wrapping past one full cycle. Handle the empty needle first and return true. If s is empty and needle is non-empty, that's false. Built-in find or contains is fine for 10^4, but KMP gives guaranteed linear time if you want to be safe. The common pitfall is skipping the length check, or looping over rotations and slicing each one. StealthCoder is the hedge on the live OA if the doubling idea doesn't come to you under the clock.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Detect Substring In Any Rotation cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Infosys's OA.

Infosys reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Detect Substring In Any Rotation FAQ

What's the trick in the Infosys rotation substring problem?+

Concatenate s with itself. Every rotation of s appears as a substring of s + s, so you only search for needle once. Add a guard that needle can't be longer than s, and return true for an empty needle. That's the whole solution.

Why does brute force fail here?+

With lengths up to 10^4, building each rotation and searching it costs roughly n rotations times n work each, around 10^8 operations plus string allocation. That's risky. Searching s + s once is linear or near it.

What edge cases should I test before submitting?+

Empty needle returns true, even when s is empty. Empty s with a non-empty needle returns false. A needle longer than s returns false. Also test needle equal to s, since the original string counts as a rotation. Those four catch most failed cases.

Do I need to write KMP, or is built-in substring search fine?+

At 10^4 characters, a built-in find or contains passes comfortably. KMP or Z-function gives a worst-case linear guarantee if you want it. Write the simple version first, and only swap in KMP if you have spare time.

How do I prepare for this in 48 hours?+

Learn the s + s doubling idea and one linear matcher like KMP. Then practice rotation-style questions, such as checking whether one string is a rotation of another. The pattern repeats. Spend the rest of the time on edge-case handling, which is where people lose points here.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Infosys.

OA at Infosys?
Invisible during screen share
Get it