Q1 of 311 · Coding problems
Reverse a string
Write a function that reverses a string. What are the common approaches and their trade-offs?
The classic interview answer is to split into characters, reverse the array, and join. You can also build a new string from the end with a loop, or use two pointers on an array of characters. Strings are immutable in JavaScript, so every concatenation in a loop copies — split/reverse/join is simple and fast enough for interviews. Mention Unicode: spreading [...str] handles most BMP characters better than split(""), and grapheme clusters (emoji with modifiers) need Intl.Segmenter.
Examples
function reverseString(text) {
return text.split("").reverse().join("");
}
function reverseSpread(text) {
return [...text].reverse().join("");
}
reverseString("hello"); // "olleh"