Rotation with at Most Three Mismatches
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that matters in this ZipRecruiter question, reported in February 2022, is the cap of three mismatches. Not zero. Three. You get two strings of equal length, up to 2000 characters, and you have to decide if some cyclic rotation of the second one differs from the first at three positions or fewer. Rotation by zero counts. It's a string problem with a brute-force answer that actually fits the limits, which is the whole trick. If you blank on the live OA, StealthCoder runs invisibly on your desktop and hands you the loop structure so you can type it out clean.
The problem
Return whether second can be cyclically rotated so that it differs from first at no more than three positions. The strings must have equal length. A rotation by zero positions is allowed. Function similarAfterRotation(first: String, second: String) → boolean Examples Example 1 first = "abcde" second = "cdeab" return = true A cyclic shift produces an exact match. Example 2 first = "abcdef" second = "xbcyez" return = true The zero rotation differs at exactly three positions. Constraints 0 <= first.length, second.length <= 2000 The strings contain lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Try every rotation shift from 0 to n-1. For each shift, compare first[i] against second[(i + shift) % n] across all i, count mismatches, and bail out early once the count passes three. That's O(n^2), so about 4 million comparisons at n = 2000. Fine. The pitfall is overengineering it with KMP, hashing, or FFT when the constraints don't ask for it. The second pitfall is edge cases: empty strings should return true (zero mismatches), and unequal lengths should return false even though the statement says they must match. Also check the rotation direction is consistent, though it doesn't matter since you try all shifts. Early exit on mismatch count keeps it fast in practice. If you freeze on the modulo indexing during the live OA, StealthCoder is the hedge that shows the working solution while you keep typing.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Rotation with at Most Three Mismatches 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Rotation with at Most Three Mismatches FAQ
What's the trick in Rotation with at Most Three Mismatches?+
There isn't a clever one. Try every shift from 0 to n-1, count positions where first[i] differs from second[(i+shift)%n], and return true if any shift gives three or fewer. The constraint of 2000 makes O(n^2) acceptable, so don't reach for anything fancier.
How hard is this ZipRecruiter question really?+
Easy to medium. The logic is a nested loop with a counter. Most people lose points on edge cases like empty strings or forgetting the zero rotation, not on the algorithm itself. If you can write modulo indexing cleanly, you're most of the way there.
Will O(n^2) pass with length 2000?+
Yes. That's roughly 4 million character comparisons in the worst case. Add an early break once mismatches exceed three and most shifts finish in a handful of steps. No need for hashing or string-matching algorithms.
What edge cases should I test?+
Two empty strings should return true. Identical strings should return true via zero rotation. Length 1 strings with different letters have one mismatch, so true. Strings of different lengths should return false to be safe. Also test example 2, where only the zero rotation works.
How do I prepare for this in 48 hours?+
Write the brute-force rotation loop from memory twice. Then practice similar cyclic-index problems using (i + shift) % n. Spend the rest of the time on edge cases and early exits. This pattern is simple enough that clean execution beats extra study.