Hiprup

Write a Java program to reverse a string without using the built-in reverse() method.

Because a Java String is immutable, you cannot reverse it in place — you must build a new character sequence. The two standard manual approaches are a backward loop that reads characters from the last index to the first, and a two-pointer swap over a char[].

Both run in O(n) time; the interviewer mainly wants to see clean index handling and awareness of object creation costs.

  • Backward loop — run for (int i = s.length() - 1; i >= 0; i--) and append s.charAt(i) to a StringBuilder, then call toString().

  • Two-pointer swap — call toCharArray(), swap chars[left] and chars[right] while left < right, and rebuild with new String(chars).

  • Avoid += concatenationresult += ch inside a loop creates a new String each iteration, giving O(n²) behavior.

  • Recursionreverse(s.substring(1)) + s.charAt(0) with an empty-string base case is a common follow-up, but it is O(n²) and can overflow the stack for long inputs.

  • Automation context — SDETs write similar string utilities in Selenium/TestNG frameworks for test-data builders, palindrome checks, and validating transformed UI text.

Key terms: String immutability, charAt(), StringBuilder, toCharArray(), two-pointer technique, O(n) time complexity

public class ReverseString {

    // Approach 1: backward loop with StringBuilder (no reverse() call)
    static String reverseWithLoop(String input) {
        StringBuilder reversed = new StringBuilder(input.length());
        for (int i = input.length() - 1; i >= 0; i--) {
            reversed.append(input.charAt(i));
        }
        return reversed.toString();
    }

    // Approach 2: two-pointer swap on a char array
    static String reverseWithSwap(String input) {
        char[] chars = input.toCharArray();
        int left = 0;
        int right = chars.length - 1;
        while (left < right) {
            char temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;
            left++;
            right--;
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        String input = "Selenium";
        System.out.println(reverseWithLoop(input)); // Output: muineleS
        System.out.println(reverseWithSwap(input)); // Output: muineleS
    }
}

The program reverses "Selenium" two ways without calling reverse(). reverseWithLoop walks the string from the last index down to 0 and appends each character to a StringBuilder, so the characters come out in reverse order; the builder is presized to input.length() so its internal array never has to grow. reverseWithSwap converts the string to a mutable char array and exchanges the outermost pair of characters, moving both pointers inward until they meet in the middle, then wraps the array in a new String. Both print "muineleS" and run in O(n) time; the swap version needs no StringBuilder at all — it allocates only the char array and the final String — making it the answer to lead with when the interviewer treats StringBuilder as built-in help.

Show both the backward loop and the two-pointer swap, then state the complexity — O(n) time — and mention that the char-array version avoids StringBuilder entirely, which matters to interviewers who count StringBuilder as built-in help. Expect follow-ups like reversing word order in a sentence, checking a palindrome, writing it recursively, or handling emoji, which char-by-char reversal breaks because it splits surrogate pairs.

The classic mistakes are starting the loop at s.length() instead of s.length() - 1 (StringIndexOutOfBoundsException on charAt) and reflexively reaching for StringBuilder.reverse(), which the question explicitly forbids.