{"id":1034,"date":"2026-10-30T12:16:02","date_gmt":"2026-10-30T12:16:02","guid":{"rendered":"https:\/\/blog.debuggersspace.com\/?p=1034"},"modified":"2026-10-30T14:12:01","modified_gmt":"2026-10-30T14:12:01","slug":"reverse-a-string-without-using-a-built-in-reverse-function","status":"publish","type":"post","link":"https:\/\/blog.debuggersspace.com\/index.php\/2026\/10\/30\/reverse-a-string-without-using-a-built-in-reverse-function\/","title":{"rendered":"Reverse a string without using a built-in reverse function."},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>1 Minute, 56 Second                <\/div>\n\n            <\/div>\n<p class=\"wp-block-paragraph\">A warm-up coding question. The interviewer wants a correct loop, a clear explanation of cost, and maybe one edge case. Do not reach for <code>new StringBuilder(s).reverse()<\/code>: that is exactly what they asked you not to use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The two-pointer swap (O(n) time, O(n) space)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Strings in Java are immutable, so copy the characters into an array, swap from both ends towards the middle, and build a new string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static String reverse(String s) {\n    if (s == null) return null;\n    char[] a = s.toCharArray();\n    int i = 0, j = a.length - 1;\n    while (i &lt; j) {\n        char t = a[i];\n        a[i] = a[j];\n        a[j] = t;\n        i++;\n        j--;\n    }\n    return new String(a);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each character is touched once, so time is O(n). The char array takes O(n) extra space; in a language with mutable strings or arrays (C, or reversing a char array in place) the swap itself needs only O(1) extra space.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Building from the end<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>static String reverse2(String s) {\n    StringBuilder sb = new StringBuilder(s.length());\n    for (int i = s.length() - 1; i &gt;= 0; i--) sb.append(s.charAt(i));\n    return sb.toString();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid doing the same with <code>result = result + s.charAt(i)<\/code> in a loop: each concatenation creates a new string, which makes it O(n\u00b2).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Recursion (if they ask for it)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>static String reverseRec(String s) {\n    if (s.length() &lt;= 1) return s;\n    return reverseRec(s.substring(1)) + s.charAt(0);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Short, but O(n\u00b2) because of the substrings and concatenation, and it can overflow the stack for long strings. Say that before they ask.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Edge cases to mention<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>null<\/code> and the empty string.<\/li>\n\n\n<li>A single character or a palindrome (the output equals the input).<\/li>\n\n\n<li>Characters outside the Basic Multilingual Plane, such as emoji, are two <code>char<\/code>s (a surrogate pair). A plain char swap breaks them; reverse by code points if that matters.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Follow-ups<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reverse each word in a sentence but keep the word order.<\/li>\n\n\n<li>Reverse the order of words: &#8220;I like Java&#8221; becomes &#8220;Java like I&#8221;.<\/li>\n\n\n<li>Check whether a string is a palindrome.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>This answer was written by Debugger&#8217;s Space as practice material. It is asked in most Indian IT services interviews (TCS, Infosys, Wipro, HCLTech, Tech Mahindra, LTIMindtree, Accenture, Cognizant, Capgemini, IBM). See each company&#8217;s hiring steps and practice questions on <a href=\"https:\/\/www.debuggersspace.com\/companies\">Debugger&#8217;s Space<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reverse a string in Java without StringBuilder.reverse(): the two-pointer swap, building a new string, recursion, and the Unicode edge case interviewers like.<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[263],"tags":[234,236,270,235,275,231,237,229,264,233,288,228,232,230],"class_list":["post-1034","post","type-post","status-publish","format-standard","hentry","category-common-interview-questions","tag-accenture","tag-capgemini","tag-coding-interview-questions","tag-cognizant","tag-freshers","tag-hcltech","tag-ibm","tag-infosys","tag-java-interview-questions","tag-ltimindtree","tag-strings","tag-tcs","tag-tech-mahindra","tag-wipro"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/blog.debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"Reverse a string in Java without StringBuilder.reverse(): the two-pointer swap, building a new string, recursion, and the Unicode edge case interviewers like.","_links":{"self":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=1034"}],"version-history":[{"count":1,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1034\/revisions"}],"predecessor-version":[{"id":1075,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1034\/revisions\/1075"}],"wp:attachment":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=1034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=1034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=1034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}