AlgoInsight

DSA problem

Reverse a String

EasyStringsPattern: Two PointersbeginnerFree

Given a string s, reverse it and return the reversed string.

String reversal is one of the most common building blocks in programming. The two-pointer approach works just like array reversal: swap characters at the start and end, then move inward. In languages where strings are immutable (Java, Python, JavaScript), you convert to an array of characters, reverse, and join back. Built-in helpers exist (StringBuilder.reverse() in Java, slicing [::-1] in Python, split('').reverse().join('') in JavaScript), but interviewers often want a manual implementation to demonstrate understanding. Reversing a string is also a sub-step in many other problems: checking palindromes, reversing words in a sentence, and certain encoding/decoding algorithms.

Examples

Example 1
Input: s = "hello"
Output: "olleh"
Explanation: Swapping characters from both ends produces the reversed string.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

stringtwo-pointers

Browse all DSA problems