Prompt
Create a marquee effect: given a string, return an array with each rotation where the first letter moves to the end. Example: "abc" -> ["abc","bca","cab"]
Solution
This is string rotation, and the core insight is that a rotation by i is just two slices swapped: str.slice(i) + str.slice(0, i). Once you see that, "all rotations" is a map over the indices 0..n-1. No mutation, no character shuffling — each rotation is computed independently from the original string.
Complexity: n rotations, each built from O(n) slices → O(n²) time and space, which is optimal because the output itself is n strings of length n. When the output is inherently quadratic, say so — it preempts the "can you do better?" question with "not asymptotically; the answer is that big."
The naive alternative interviewers see: repeatedly doing s = s.slice(1) + s[0] and pushing each step. Also O(n²) and it works, but it's stateful — each rotation depends on the previous one, so an off-by-one anywhere corrupts everything after it. The slice-from-index version is a pure function of (str, i): easier to test, trivially parallel, no accumulated error. That's the same argument as derived state over mutated state in UI code.
Note the map over split('') is used purely for its index — the _ parameter name signals "value unused" deliberately. Array.from({length: str.length}, (_, i) => ...) is the equivalent without the throwaway split.
Red flag: an off-by-one in the slice boundaries — slice(i) + slice(0, i) must partition the string exactly; overlapping or gapping the two slices duplicates or drops a character. Test with "abc" by hand before running.
Say it: "Each rotation is slice(i) plus slice(0, i) — a pure function of the index — and the O(n²) is irreducible because the output is n strings of length n."