<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Java Interview questions: Write a String Reverser (and use Recursion!)</title>
	<atom:link href="http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/feed/" rel="self" type="application/rss+xml" />
	<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/</link>
	<description></description>
	<lastBuildDate>Fri, 30 Jul 2010 06:49:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Kristen</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-294009</link>
		<dc:creator>Kristen</dc:creator>
		<pubDate>Tue, 25 May 2010 21:54:03 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-294009</guid>
		<description>great post! This was alot of fun to code..
my solution was similar but required fewer calls.  it basically works from the outside in.

public String reverse(String s) {
   //work outside in, swap e and a..then return inner substring
        if(s != null &amp;&amp; s.length() &gt; 1) {
           return s.charAt(s.length()-1) + reverse(s.substring(1, s.length()-1)) + s.charAt(0);
        }
        return s;
    }</description>
		<content:encoded><![CDATA[<p>great post! This was alot of fun to code..<br />
my solution was similar but required fewer calls.  it basically works from the outside in.</p>
<p>public String reverse(String s) {<br />
   //work outside in, swap e and a..then return inner substring<br />
        if(s != null &amp;&amp; s.length() &gt; 1) {<br />
           return s.charAt(s.length()-1) + reverse(s.substring(1, s.length()-1)) + s.charAt(0);<br />
        }<br />
        return s;<br />
    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: busana muslim</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-283174</link>
		<dc:creator>busana muslim</dc:creator>
		<pubDate>Mon, 12 Apr 2010 08:42:31 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-283174</guid>
		<description>Great post. so full information. thanks</description>
		<content:encoded><![CDATA[<p>Great post. so full information. thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Santosh</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-278640</link>
		<dc:creator>Santosh</dc:creator>
		<pubDate>Tue, 23 Mar 2010 06:55:11 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-278640</guid>
		<description>public class StringReverseExample
{
    public static void main(String[] args)
    {
      String s1=&quot;Hello&quot;;
      String s2=&quot;&quot;;
     System.out.println(&quot;\nString before reverse:&quot;+s1);
    for(int i=s1.length()-1;i&gt;=0;i--)
     {
	  s2=s2+ s1.charAt(i);

     }
     System.out.println(&quot;String after reverse:&quot;+s2);
  }
}</description>
		<content:encoded><![CDATA[<p>public class StringReverseExample<br />
{<br />
    public static void main(String[] args)<br />
    {<br />
      String s1=&#8221;Hello&#8221;;<br />
      String s2=&#8221;";<br />
     System.out.println(&#8220;\nString before reverse:&#8221;+s1);<br />
    for(int i=s1.length()-1;i&gt;=0;i&#8211;)<br />
     {<br />
	  s2=s2+ s1.charAt(i);</p>
<p>     }<br />
     System.out.println(&#8220;String after reverse:&#8221;+s2);<br />
  }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Recursive String Recursion in Java &#171; techpuzzl</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-267677</link>
		<dc:creator>Recursive String Recursion in Java &#171; techpuzzl</dc:creator>
		<pubDate>Tue, 26 Jan 2010 14:32:36 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-267677</guid>
		<description>[...] Excellent article to further understand more about how this code works. [...]</description>
		<content:encoded><![CDATA[<p>[...] Excellent article to further understand more about how this code works. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: G. Edwin</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-262163</link>
		<dc:creator>G. Edwin</dc:creator>
		<pubDate>Mon, 04 Jan 2010 06:09:34 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-262163</guid>
		<description>Simply put each char in a stack until you get to the end of string / new line char.  

Then pop them out.  This is the best way to reverse a string. 

--Edwin.</description>
		<content:encoded><![CDATA[<p>Simply put each char in a stack until you get to the end of string / new line char.  </p>
<p>Then pop them out.  This is the best way to reverse a string. </p>
<p>&#8211;Edwin.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pa</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-259608</link>
		<dc:creator>Pa</dc:creator>
		<pubDate>Tue, 22 Dec 2009 20:43:27 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-259608</guid>
		<description>This really off course and completely unpractical. It is barely a college homework assignment. Java technology is a very wide field of practice.
It has no relevance to knowledge of what really does the job these days. These are frameworks and how to bind them into secure, stable and performing solutions. One should have knowledge of good Java practices and GoF on such low levels.</description>
		<content:encoded><![CDATA[<p>This really off course and completely unpractical. It is barely a college homework assignment. Java technology is a very wide field of practice.<br />
It has no relevance to knowledge of what really does the job these days. These are frameworks and how to bind them into secure, stable and performing solutions. One should have knowledge of good Java practices and GoF on such low levels.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-258838</link>
		<dc:creator>Tony</dc:creator>
		<pubDate>Sat, 19 Dec 2009 00:30:38 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-258838</guid>
		<description>Hi Stephan, I agree with pranav, but also see your point in thinking how will you use a language to solve a problem. 

Although I know StringBuffer I have used it for appending strings. I did write a string reverser just converted the string into an array of char read the contents into another char array converted back into a String. There are quite a few ways to solve it.

But gettinh back to pranav with all the new frameworks, doing a lot of the work for you, the skill will/should be, what framework would you choose and why. 

Would/why would you use ORM. 

Coding to java patterns. 

The problems related to mixing framworks.

There is so much java technology out there. So many different frameworks and even different versions.

Updating JAXB recently a lot of the JAXB related functions stopped working.

This happens a lot, a good interview is to ask the developer what would they do, how would they go about solving this problem or making sure the problem does not happen in the first place.

I find when I am preparing for  java interviews, I need to remove my java head and put on my old C coding head.</description>
		<content:encoded><![CDATA[<p>Hi Stephan, I agree with pranav, but also see your point in thinking how will you use a language to solve a problem. </p>
<p>Although I know StringBuffer I have used it for appending strings. I did write a string reverser just converted the string into an array of char read the contents into another char array converted back into a String. There are quite a few ways to solve it.</p>
<p>But gettinh back to pranav with all the new frameworks, doing a lot of the work for you, the skill will/should be, what framework would you choose and why. </p>
<p>Would/why would you use ORM. </p>
<p>Coding to java patterns. </p>
<p>The problems related to mixing framworks.</p>
<p>There is so much java technology out there. So many different frameworks and even different versions.</p>
<p>Updating JAXB recently a lot of the JAXB related functions stopped working.</p>
<p>This happens a lot, a good interview is to ask the developer what would they do, how would they go about solving this problem or making sure the problem does not happen in the first place.</p>
<p>I find when I am preparing for  java interviews, I need to remove my java head and put on my old C coding head.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-244443</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Fri, 18 Sep 2009 05:07:15 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-244443</guid>
		<description>@Pranav: But I assume - withouth the knowledge of StringBuffer - you had no problem writing a reverse() method and everything was ok?</description>
		<content:encoded><![CDATA[<p>@Pranav: But I assume &#8211; withouth the knowledge of StringBuffer &#8211; you had no problem writing a reverse() method and everything was ok?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pranav</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-244415</link>
		<dc:creator>pranav</dc:creator>
		<pubDate>Fri, 18 Sep 2009 01:36:23 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-244415</guid>
		<description>this is a ridiculous interview question. i was asked this question for a senior developer position.  i did not know about the stringbuffer class. does that mean i cannot do java? i have developed complex algorithms in java - but the interviewer did not even look at my resume after asking this question. it is a ridiculous way to judge somebody. 

it was a waste of time commuting all the way and not being asked a single question about my role at my current job.

you do not need to know these low level things, if you get the big picture. these kind of questions are good for college tests which have nothing to do with real world. in real life, you will never sit and reverse a string.

i hate when interviewers try to be too geeky. that&#039;s why they have 10+ years of exp and they still are coding reporting to a non-techy person who knows nothing about computers.</description>
		<content:encoded><![CDATA[<p>this is a ridiculous interview question. i was asked this question for a senior developer position.  i did not know about the stringbuffer class. does that mean i cannot do java? i have developed complex algorithms in java &#8211; but the interviewer did not even look at my resume after asking this question. it is a ridiculous way to judge somebody. </p>
<p>it was a waste of time commuting all the way and not being asked a single question about my role at my current job.</p>
<p>you do not need to know these low level things, if you get the big picture. these kind of questions are good for college tests which have nothing to do with real world. in real life, you will never sit and reverse a string.</p>
<p>i hate when interviewers try to be too geeky. that&#8217;s why they have 10+ years of exp and they still are coding reporting to a non-techy person who knows nothing about computers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andreas</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-239682</link>
		<dc:creator>Andreas</dc:creator>
		<pubDate>Tue, 18 Aug 2009 13:05:41 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-239682</guid>
		<description>Maybe missed it, but just wanted to show one (!) end-tail-rekursion-solution:

public static String reverse(String toReverse) {
   return reverse(toReverse, &quot;&quot;);
}

private static String reverse(String toReverse, String reverse) {
   if (toReverse == null &#124;&#124; reverse.length() == toReverse.length()) {
      return reverse;
   }
   reverse += toReverse.charAt(toReverse.length() - reverse.length() - 1);
   return reverse(toReverse, reverse);
}</description>
		<content:encoded><![CDATA[<p>Maybe missed it, but just wanted to show one (!) end-tail-rekursion-solution:</p>
<p>public static String reverse(String toReverse) {<br />
   return reverse(toReverse, &#8220;&#8221;);<br />
}</p>
<p>private static String reverse(String toReverse, String reverse) {<br />
   if (toReverse == null || reverse.length() == toReverse.length()) {<br />
      return reverse;<br />
   }<br />
   reverse += toReverse.charAt(toReverse.length() &#8211; reverse.length() &#8211; 1);<br />
   return reverse(toReverse, reverse);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kiran Talawai</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-235569</link>
		<dc:creator>Kiran Talawai</dc:creator>
		<pubDate>Sat, 11 Jul 2009 17:47:51 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-235569</guid>
		<description>Thanks am,
          Thanks for ur suggestion.. :-)</description>
		<content:encoded><![CDATA[<p>Thanks am,<br />
          Thanks for ur suggestion.. :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-226582</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Sun, 08 Mar 2009 08:24:40 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-226582</guid>
		<description>@am: Please go back and read the article. 

&quot;The best implementation in Java is to use the reverse method of the StringBuffer class in the JDK. It’s fast, efficient and knows how to handle unicode surrogate pairs, something most other solutions ignore.&quot;

Thanks
Stephan</description>
		<content:encoded><![CDATA[<p>@am: Please go back and read the article. </p>
<p>&#8220;The best implementation in Java is to use the reverse method of the StringBuffer class in the JDK. It’s fast, efficient and knows how to handle unicode surrogate pairs, something most other solutions ignore.&#8221;</p>
<p>Thanks<br />
Stephan</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: am</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-226581</link>
		<dc:creator>am</dc:creator>
		<pubDate>Sun, 08 Mar 2009 08:07:19 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-226581</guid>
		<description>Kiran:

your solution has terrible performance characteristics.

Never do this:

for(int k=0;k0&amp;&amp;m&gt;=m-1;m–){
temp=temp+” “+arr[m-1];

}

Repeated concatenation in Java is VERY SLOW. You end up doing O(N2) character copies because of the implicit StringBuilders..

The correct way to do it is:

StringBuilder temp = new StringBuilder(str.length());
for (...) {
   temp.append(arr[m-1]);
}

return temp.toString();

This avoids n(n-1)/2 character copies instead of just n.</description>
		<content:encoded><![CDATA[<p>Kiran:</p>
<p>your solution has terrible performance characteristics.</p>
<p>Never do this:</p>
<p>for(int k=0;k0&amp;&amp;m&gt;=m-1;m–){<br />
temp=temp+” “+arr[m-1];</p>
<p>}</p>
<p>Repeated concatenation in Java is VERY SLOW. You end up doing O(N2) character copies because of the implicit StringBuilders..</p>
<p>The correct way to do it is:</p>
<p>StringBuilder temp = new StringBuilder(str.length());<br />
for (&#8230;) {<br />
   temp.append(arr[m-1]);<br />
}</p>
<p>return temp.toString();</p>
<p>This avoids n(n-1)/2 character copies instead of just n.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kiran Talawai</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-224651</link>
		<dc:creator>Kiran Talawai</dc:creator>
		<pubDate>Wed, 21 Jan 2009 12:01:44 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-224651</guid>
		<description>public static void main(String[] args) {
	String str=&quot;This. is Mruthyu&quot;;
	String temp=&quot;&quot;;
	StringTokenizer stk = new StringTokenizer(str);
	String arr[] = new String[stk.countTokens()];
    while(stk.hasMoreElements()){
    	for(int k=0;k0&amp;&amp;m&gt;=m-1;m--){
    	temp=temp+&quot; &quot;+arr[m-1];
		
	}
    System.out.println(&quot;MMMMM&quot;+temp);
	}</description>
		<content:encoded><![CDATA[<p>public static void main(String[] args) {<br />
	String str=&#8221;This. is Mruthyu&#8221;;<br />
	String temp=&#8221;";<br />
	StringTokenizer stk = new StringTokenizer(str);<br />
	String arr[] = new String[stk.countTokens()];<br />
    while(stk.hasMoreElements()){<br />
    	for(int k=0;k0&amp;&amp;m&gt;=m-1;m&#8211;){<br />
    	temp=temp+&#8221; &#8220;+arr[m-1];</p>
<p>	}<br />
    System.out.println(&#8220;MMMMM&#8221;+temp);<br />
	}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kiran Talawai</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-224649</link>
		<dc:creator>Kiran Talawai</dc:creator>
		<pubDate>Wed, 21 Jan 2009 11:59:53 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-224649</guid>
		<description>/* The following code works fine in reverse the string without the stack interface
*/

public static void main(String[] args) {
	String str=&quot;This. is Mruthyu&quot;;
	String temp=&quot;&quot;;
	StringTokenizer stk = new StringTokenizer(str);
	String arr[] = new String[stk.countTokens()];
    while(stk.hasMoreElements()){
    	for(int k=0;k0&amp;&amp;m&gt;=m-1;m--){
    	temp=temp+&quot; &quot;+arr[m-1];
		
	}
    System.out.println(&quot;MMMMM&quot;+temp);
	}</description>
		<content:encoded><![CDATA[<p>/* The following code works fine in reverse the string without the stack interface<br />
*/</p>
<p>public static void main(String[] args) {<br />
	String str=&#8221;This. is Mruthyu&#8221;;<br />
	String temp=&#8221;";<br />
	StringTokenizer stk = new StringTokenizer(str);<br />
	String arr[] = new String[stk.countTokens()];<br />
    while(stk.hasMoreElements()){<br />
    	for(int k=0;k0&amp;&amp;m&gt;=m-1;m&#8211;){<br />
    	temp=temp+&#8221; &#8220;+arr[m-1];</p>
<p>	}<br />
    System.out.println(&#8220;MMMMM&#8221;+temp);<br />
	}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: java4all</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-185468</link>
		<dc:creator>java4all</dc:creator>
		<pubDate>Wed, 22 Oct 2008 20:43:22 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-185468</guid>
		<description>Good interview Questions can be found at http://java4all.info</description>
		<content:encoded><![CDATA[<p>Good interview Questions can be found at <a href="http://java4all.info" rel="nofollow">http://java4all.info</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-42752</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 05 Dec 2007 21:48:44 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-42752</guid>
		<description>&quot;Do I get a callback for a second interview?&quot; Sure ;-) Thanks for the long comment. We usually do a face to face as a second one.

&quot;The exception thrown when trying to reverse a null string should be a null pointer exception.&quot;

I&#039;m not certain on this one. Developers search for the cause of NPEs by looking for dots which dereference references. So throwing a NPE when there is no null pointer access doesn&#039;t help. I also think that throwing an NPE for 

int i = (Integer) null; 

was a wrong decision on Suns side. A AutoUnboxingException would have been more helpful and make this error much easier to detect. 

I&#039;ve added a tail recursive version here

http://stephan.reposita.org/archives/2007/11/12/string-reversing-part-ii-tail-recursion/</description>
		<content:encoded><![CDATA[<p>&#8220;Do I get a callback for a second interview?&#8221; Sure ;-) Thanks for the long comment. We usually do a face to face as a second one.</p>
<p>&#8220;The exception thrown when trying to reverse a null string should be a null pointer exception.&#8221;</p>
<p>I&#8217;m not certain on this one. Developers search for the cause of NPEs by looking for dots which dereference references. So throwing a NPE when there is no null pointer access doesn&#8217;t help. I also think that throwing an NPE for </p>
<p>int i = (Integer) null; </p>
<p>was a wrong decision on Suns side. A AutoUnboxingException would have been more helpful and make this error much easier to detect. </p>
<p>I&#8217;ve added a tail recursive version here</p>
<p><a href="http://stephan.reposita.org/archives/2007/11/12/string-reversing-part-ii-tail-recursion/" rel="nofollow">http://stephan.reposita.org/archives/2007/11/12/string-reversing-part-ii-tail-recursion/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: LingPipe Blog &#187; Blog Archive &#187; (Junior) Varsity Interview: String Reverse</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-42738</link>
		<dc:creator>LingPipe Blog &#187; Blog Archive &#187; (Junior) Varsity Interview: String Reverse</dc:creator>
		<pubDate>Wed, 05 Dec 2007 19:29:30 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-42738</guid>
		<description>[...] I&#8217;m cross-posting what was mostly a response to reading Stephans Blog [sic], which was itself a riff on Joel on Software&#8217;s Guerilla Guide to Interviewing. Both ask us to look at the string reverse problem. [...]</description>
		<content:encoded><![CDATA[<p>[...] I&#8217;m cross-posting what was mostly a response to reading Stephans Blog [sic], which was itself a riff on Joel on Software&#8217;s Guerilla Guide to Interviewing. Both ask us to look at the string reverse problem. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob Carpenter</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-42734</link>
		<dc:creator>Bob Carpenter</dc:creator>
		<pubDate>Wed, 05 Dec 2007 19:00:58 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-42734</guid>
		<description>Before reversing a &quot;string&quot;, we need to get clear on what we want.  At one level, a Java String object is just an array of chars.  At another level, it represents a sequence of unicode code points.  The tension arises from the fact that String(char[]) lets you construct a string with a sequence of chars that don&#039;t correspond to valid unicode code points.  This tension was ratcheted up in the 1.5 JDK when they moved to Unicode 4.0.

In the 1.5 JDK, Sun changed the behavior of the StringBuffer.reverse() method in a way that was not backward compatible with 1.4.  That is, there are StringBuffer instances for which reverse() in 1.4 returns a different value than in 1.5.  

The 1.5 version of reverse() is sensitive to surrogate pairs (unicode code points requiring more than 16 bits, and hence more than one char, in UTF-16).  In 1.5, both java.lang.StringBuilder and java.lang.StringBuffer use the implementation of reverse() from their common superclass, java.lang.AbstractStringBuilder.

Here&#039;s the first paragraph of doc for java.lang.StringBuffer.reverse() from JDK 1.5:

&quot;Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.&quot;

And here&#039;s the first paragraph of doc from java.lang.StringBuffer.reverse() from JDK 1.4:

&quot;The character sequence contained in this string buffer is replaced by the reverse of the sequence.&quot;

Following Stephan&#039;s suggestion to use the built-in has either a good or bad side effect.  Moving from 1.4 to 1.5 either breaks backward compatibility for the string as char sequence representation, or appropriately handles unicode 5.0 in the string as sequence of code points representation.  

Extra credit 1:  Recursion won&#039;t work because it&#039;ll blow out the stack if we&#039;re using Sun&#039;s JDKs, which (at least so far) don&#039;t perform tail recursion optimization (a kind of last call optimization).

Extra credit 2: The exception thrown when trying to reverse a null string should be a null pointer exception.  That&#039;s how Sun codes the JDK itself (see, e.g., the java.lang.String.String(String) constructor).  It&#039;s a runtime exception because it&#039;s a coding error to send reverse(String) a null string (assuming you want behavior like your call to StringBuffer.reverse()).  It should be a null pointer exception, as that&#039;ll lead you right to the problem while debugging.

Do I get a callback for a second interview?</description>
		<content:encoded><![CDATA[<p>Before reversing a &#8220;string&#8221;, we need to get clear on what we want.  At one level, a Java String object is just an array of chars.  At another level, it represents a sequence of unicode code points.  The tension arises from the fact that String(char[]) lets you construct a string with a sequence of chars that don&#8217;t correspond to valid unicode code points.  This tension was ratcheted up in the 1.5 JDK when they moved to Unicode 4.0.</p>
<p>In the 1.5 JDK, Sun changed the behavior of the StringBuffer.reverse() method in a way that was not backward compatible with 1.4.  That is, there are StringBuffer instances for which reverse() in 1.4 returns a different value than in 1.5.  </p>
<p>The 1.5 version of reverse() is sensitive to surrogate pairs (unicode code points requiring more than 16 bits, and hence more than one char, in UTF-16).  In 1.5, both java.lang.StringBuilder and java.lang.StringBuffer use the implementation of reverse() from their common superclass, java.lang.AbstractStringBuilder.</p>
<p>Here&#8217;s the first paragraph of doc for java.lang.StringBuffer.reverse() from JDK 1.5:</p>
<p>&#8220;Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.&#8221;</p>
<p>And here&#8217;s the first paragraph of doc from java.lang.StringBuffer.reverse() from JDK 1.4:</p>
<p>&#8220;The character sequence contained in this string buffer is replaced by the reverse of the sequence.&#8221;</p>
<p>Following Stephan&#8217;s suggestion to use the built-in has either a good or bad side effect.  Moving from 1.4 to 1.5 either breaks backward compatibility for the string as char sequence representation, or appropriately handles unicode 5.0 in the string as sequence of code points representation.  </p>
<p>Extra credit 1:  Recursion won&#8217;t work because it&#8217;ll blow out the stack if we&#8217;re using Sun&#8217;s JDKs, which (at least so far) don&#8217;t perform tail recursion optimization (a kind of last call optimization).</p>
<p>Extra credit 2: The exception thrown when trying to reverse a null string should be a null pointer exception.  That&#8217;s how Sun codes the JDK itself (see, e.g., the java.lang.String.String(String) constructor).  It&#8217;s a runtime exception because it&#8217;s a coding error to send reverse(String) a null string (assuming you want behavior like your call to StringBuffer.reverse()).  It should be a null pointer exception, as that&#8217;ll lead you right to the problem while debugging.</p>
<p>Do I get a callback for a second interview?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jude</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-40892</link>
		<dc:creator>Jude</dc:creator>
		<pubDate>Sun, 25 Nov 2007 12:10:19 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-40892</guid>
		<description>Ask the candidate to implement String reversal using recursion . Now that would boil down to some sort of tail recursion . If the aim is to test the candidate&#039;s Recursion capabilities , then i think tail recursion and such a question is a bad choice .

Tail recursion should be best avoided , in such cases an iterative solution is much much better . Fibonacci series ( iterative vs recursion ) has been discussed a zillion times .

I feel its best to ask recursive problems that truly are recursive without being tail recursive .</description>
		<content:encoded><![CDATA[<p>Ask the candidate to implement String reversal using recursion . Now that would boil down to some sort of tail recursion . If the aim is to test the candidate&#8217;s Recursion capabilities , then i think tail recursion and such a question is a bad choice .</p>
<p>Tail recursion should be best avoided , in such cases an iterative solution is much much better . Fibonacci series ( iterative vs recursion ) has been discussed a zillion times .</p>
<p>I feel its best to ask recursive problems that truly are recursive without being tail recursive .</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-39700</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Sun, 18 Nov 2007 22:37:37 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-39700</guid>
		<description>I&#039;ve been googling around, found some mentions of tail recursion and tail call elimination in IBM VMs but no official description. The most I found from IBM were some academic papers on the topic. So I didn&#039;t mention IBMs VM. Have you found a description of tail recursion/call elimination in IBMs VM? I would be highly interested.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been googling around, found some mentions of tail recursion and tail call elimination in IBM VMs but no official description. The most I found from IBM were some academic papers on the topic. So I didn&#8217;t mention IBMs VM. Have you found a description of tail recursion/call elimination in IBMs VM? I would be highly interested.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nils</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-39648</link>
		<dc:creator>Nils</dc:creator>
		<pubDate>Sun, 18 Nov 2007 16:00:17 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-39648</guid>
		<description>&quot;Java does not optimize tail recursion&quot;. Too broad statement. Sun&#039;s VM does not.
IBM&#039;s VM optimizes tail recursion into iteration.</description>
		<content:encoded><![CDATA[<p>&#8220;Java does not optimize tail recursion&#8221;. Too broad statement. Sun&#8217;s VM does not.<br />
IBM&#8217;s VM optimizes tail recursion into iteration.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38632</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Mon, 12 Nov 2007 19:37:02 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38632</guid>
		<description>@Unimpressed: I&#039;m not impressed by your comment either. As written before the recursive solution isn&#039;t very good for Java and Unicode reversal because Java has immutable Strings which will make you run out of memory faster than the call stack and Unicode has surrogate pairs which will make your reversed string unreadable. 

But go ahead, write a tail recursive version. Which will not help, because Java does not optimize tail recursion.</description>
		<content:encoded><![CDATA[<p>@Unimpressed: I&#8217;m not impressed by your comment either. As written before the recursive solution isn&#8217;t very good for Java and Unicode reversal because Java has immutable Strings which will make you run out of memory faster than the call stack and Unicode has surrogate pairs which will make your reversed string unreadable. </p>
<p>But go ahead, write a tail recursive version. Which will not help, because Java does not optimize tail recursion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Unimpressed</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38614</link>
		<dc:creator>Unimpressed</dc:creator>
		<pubDate>Mon, 12 Nov 2007 16:11:05 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38614</guid>
		<description>Hmm.. Your stupid recursion solution only handles strings less than 65536 chars in length due to the call stack depth limit.</description>
		<content:encoded><![CDATA[<p>Hmm.. Your stupid recursion solution only handles strings less than 65536 chars in length due to the call stack depth limit.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38597</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Mon, 12 Nov 2007 13:38:42 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38597</guid>
		<description>I don&#039;t consider writing a reverse String function a puzzler. Man hole covers are. Writing code is not. &quot;This does not help, as the main reason for the question is the discussion on why the developer has chosen the particular solution.&quot; No puzzler there.

Obviously one would ask the candidate about past projects, his roles in these projects, his view on technologies, testing, quality assurance, requirement engineering, his social skills and much more. Asking one programming question is only a small part of an interview process.

Gr33tZ
-stephan</description>
		<content:encoded><![CDATA[<p>I don&#8217;t consider writing a reverse String function a puzzler. Man hole covers are. Writing code is not. &#8220;This does not help, as the main reason for the question is the discussion on why the developer has chosen the particular solution.&#8221; No puzzler there.</p>
<p>Obviously one would ask the candidate about past projects, his roles in these projects, his view on technologies, testing, quality assurance, requirement engineering, his social skills and much more. Asking one programming question is only a small part of an interview process.</p>
<p>Gr33tZ<br />
-stephan</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Leo</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38594</link>
		<dc:creator>Leo</dc:creator>
		<pubDate>Mon, 12 Nov 2007 13:28:35 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38594</guid>
		<description>it was a joke... and yes i do like ruby and no there is no one solves all Rulez anywhere really so not about to start another flame war.

On the programmers interviews though, I like to ask about their experiences in a practical sense and have them explain to me parts of projects they have worked on. Not puzzles like this one you are outlining here.

Greetz

Leo</description>
		<content:encoded><![CDATA[<p>it was a joke&#8230; and yes i do like ruby and no there is no one solves all Rulez anywhere really so not about to start another flame war.</p>
<p>On the programmers interviews though, I like to ask about their experiences in a practical sense and have them explain to me parts of projects they have worked on. Not puzzles like this one you are outlining here.</p>
<p>Greetz</p>
<p>Leo</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38592</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Mon, 12 Nov 2007 12:50:07 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38592</guid>
		<description>@Leo: &quot;text&quot;.reverse in Groovy

This does not help, as the main reason for the question is the discussion on why the developer has chosen the particular solution.

So obviously I would ask a different question when interviewing Ruby developers. 

Or Javascript developers.

Or Lisp developers.

Or did I missunderstand your comment and it was another &quot;Ruby rulez Java&quot; comment?</description>
		<content:encoded><![CDATA[<p>@Leo: &#8220;text&#8221;.reverse in Groovy</p>
<p>This does not help, as the main reason for the question is the discussion on why the developer has chosen the particular solution.</p>
<p>So obviously I would ask a different question when interviewing Ruby developers. </p>
<p>Or Javascript developers.</p>
<p>Or Lisp developers.</p>
<p>Or did I missunderstand your comment and it was another &#8220;Ruby rulez Java&#8221; comment?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Leo</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38591</link>
		<dc:creator>Leo</dc:creator>
		<pubDate>Mon, 12 Nov 2007 12:42:52 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38591</guid>
		<description>&quot;text&quot;.reverse in Ruby LOL</description>
		<content:encoded><![CDATA[<p>&#8220;text&#8221;.reverse in Ruby LOL</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38571</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Mon, 12 Nov 2007 10:01:55 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38571</guid>
		<description>@PXM: Ah thanks. Every new day something new to learn.

@Witek: Sorry for my ill informed reply.</description>
		<content:encoded><![CDATA[<p>@PXM: Ah thanks. Every new day something new to learn.</p>
<p>@Witek: Sorry for my ill informed reply.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PXM</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38565</link>
		<dc:creator>PXM</dc:creator>
		<pubDate>Mon, 12 Nov 2007 09:35:59 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38565</guid>
		<description>No, your example isn&#039;t tail recursive.  Your recursive call is

return reverse(str.substring(1)) + str.charAt(0);

So the stack frame has to stay around to do the append on the recursive result.  To make this tail recursive you need to pass in an accumulator like Witek&#039;s solution.</description>
		<content:encoded><![CDATA[<p>No, your example isn&#8217;t tail recursive.  Your recursive call is</p>
<p>return reverse(str.substring(1)) + str.charAt(0);</p>
<p>So the stack frame has to stay around to do the append on the recursive result.  To make this tail recursive you need to pass in an accumulator like Witek&#8217;s solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38535</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Mon, 12 Nov 2007 06:00:21 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38535</guid>
		<description>@Witek: My example uses tail recursion.

@Paulo: Yes, off topic for this post, not off topic for interviewing. Asking about Open Source projects is a good idea. I would prefer a candidate who is participating in Open Source projects, good idea.</description>
		<content:encoded><![CDATA[<p>@Witek: My example uses tail recursion.</p>
<p>@Paulo: Yes, off topic for this post, not off topic for interviewing. Asking about Open Source projects is a good idea. I would prefer a candidate who is participating in Open Source projects, good idea.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Witek</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38480</link>
		<dc:creator>Witek</dc:creator>
		<pubDate>Mon, 12 Nov 2007 00:03:19 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38480</guid>
		<description>You should use tail recurions.

Example in Erlang:

reverse(X) -&gt;
      reverse(X, []).

reverse([A&#124;Rest], Acc) -&gt;
      reverse(Rest, A    Acc);
reverse([], Acc) -&gt;
      Acc.

This should take constant stack space.</description>
		<content:encoded><![CDATA[<p>You should use tail recurions.</p>
<p>Example in Erlang:</p>
<p>reverse(X) -&gt;<br />
      reverse(X, []).</p>
<p>reverse([A|Rest], Acc) -&gt;<br />
      reverse(Rest, A    Acc);<br />
reverse([], Acc) -&gt;<br />
      Acc.</p>
<p>This should take constant stack space.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paulo Abrantes</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38462</link>
		<dc:creator>Paulo Abrantes</dc:creator>
		<pubDate>Sun, 11 Nov 2007 22:26:52 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38462</guid>
		<description>It might be a bit out of topic but, what about asking for any participation in open source projects? Or isn&#039;t that very relevant?</description>
		<content:encoded><![CDATA[<p>It might be a bit out of topic but, what about asking for any participation in open source projects? Or isn&#8217;t that very relevant?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38409</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Sun, 11 Nov 2007 16:19:55 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38409</guid>
		<description>Wintermut.</description>
		<content:encoded><![CDATA[<p>Wintermut.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wintermute</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38400</link>
		<dc:creator>wintermute</dc:creator>
		<pubDate>Sun, 11 Nov 2007 15:29:45 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38400</guid>
		<description>prashant: Hehe. Was that a joke?</description>
		<content:encoded><![CDATA[<p>prashant: Hehe. Was that a joke?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cool things: Automatic Online Interview Questions with Janino at Stephans Blog</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38340</link>
		<dc:creator>Cool things: Automatic Online Interview Questions with Janino at Stephans Blog</dc:creator>
		<pubDate>Sun, 11 Nov 2007 08:00:21 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38340</guid>
		<description>[...] About Reposita development. No signal, no noise.           &#171; Java Interview questions: Write a String Reverser (and use Recursion!) [...]</description>
		<content:encoded><![CDATA[<p>[...] About Reposita development. No signal, no noise.           &laquo; Java Interview questions: Write a String Reverser (and use Recursion!) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prashant</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38032</link>
		<dc:creator>prashant</dc:creator>
		<pubDate>Fri, 09 Nov 2007 13:46:26 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38032</guid>
		<description>Actually i came up with this version.Hope i am not in the list of dummy&#039;s.

Thanks
Prashant
http://prashantjalasutram.blogspot.com/


&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; StringReverser &lt;strong&gt;{&lt;/strong&gt;
	
	&lt;strong&gt;static&lt;/strong&gt; String orgStr = &quot;prasanth&quot;;
	
	&lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;char&lt;/strong&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;strong&gt;]&lt;/strong&gt; orgCharArr = orgStr.toCharArray&lt;strong&gt;(&lt;/strong&gt;&lt;strong&gt;)&lt;/strong&gt;;
	
	&lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;char&lt;/strong&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;strong&gt;]&lt;/strong&gt; distCharArr = &lt;strong&gt;new&lt;/strong&gt; &lt;strong&gt;char&lt;/strong&gt;&lt;strong&gt;[&lt;/strong&gt;orgStr.length&lt;strong&gt;(&lt;/strong&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;strong&gt;]&lt;/strong&gt;;
	
	&lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;int&lt;/strong&gt; endCounter = orgCharArr.length - 1;
	
	&lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;int&lt;/strong&gt; startCounter = 0;
	
	&lt;strong&gt;static&lt;/strong&gt; StringBuffer output = &lt;strong&gt;new&lt;/strong&gt; StringBuffer&lt;strong&gt;(&lt;/strong&gt;&lt;strong&gt;)&lt;/strong&gt;;
	
	&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; main&lt;strong&gt;(&lt;/strong&gt;String&lt;strong&gt;[&lt;/strong&gt;&lt;strong&gt;]&lt;/strong&gt; args&lt;strong&gt;)&lt;/strong&gt; &lt;strong&gt;{&lt;/strong&gt;
		String orginalString = &quot;pras&quot;;
		&lt;strong&gt;char&lt;/strong&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;strong&gt;]&lt;/strong&gt; reverseStr=reverse&lt;strong&gt;(&lt;/strong&gt;orgCharArr&lt;strong&gt;)&lt;/strong&gt;;
		System.out.println&lt;strong&gt;(&lt;/strong&gt;reverseStr&lt;strong&gt;)&lt;/strong&gt;;
	&lt;strong&gt;}&lt;/strong&gt;
	
	&lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;char&lt;/strong&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;strong&gt;]&lt;/strong&gt; reverse&lt;strong&gt;(&lt;/strong&gt;&lt;strong&gt;char&lt;/strong&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;strong&gt;]&lt;/strong&gt; org&lt;strong&gt;)&lt;/strong&gt; &lt;strong&gt;{&lt;/strong&gt;
		
		distCharArr&lt;strong&gt;[&lt;/strong&gt;endCounter&lt;strong&gt;]&lt;/strong&gt;=orgCharArr&lt;strong&gt;[&lt;/strong&gt;startCounter&lt;strong&gt;]&lt;/strong&gt;;
		
		&lt;strong&gt;if&lt;/strong&gt;&lt;strong&gt;(&lt;/strong&gt;endCounter&lt;=0 &#124;&#124; startCounter&gt;=orgStr.length&lt;strong&gt;(&lt;/strong&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;strong&gt;)&lt;/strong&gt; &lt;strong&gt;{&lt;/strong&gt;
			&lt;strong&gt;return&lt;/strong&gt; distCharArr;
		&lt;strong&gt;}&lt;/strong&gt;&lt;strong&gt;else&lt;/strong&gt; &lt;strong&gt;{&lt;/strong&gt;
			startCounter  ;
			endCounter--;
			&lt;strong&gt;return&lt;/strong&gt; reverse&lt;strong&gt;(&lt;/strong&gt;orgCharArr&lt;strong&gt;)&lt;/strong&gt;;
		&lt;strong&gt;}&lt;/strong&gt;
		
	&lt;strong&gt;}&lt;/strong&gt;&lt;strong&gt;}&lt;/strong&gt;</description>
		<content:encoded><![CDATA[<p>Actually i came up with this version.Hope i am not in the list of dummy&#8217;s.</p>
<p>Thanks<br />
Prashant<br />
<a href="http://prashantjalasutram.blogspot.com/" rel="nofollow">http://prashantjalasutram.blogspot.com/</a></p>
<p><strong>public</strong> <strong>class</strong> StringReverser <strong>{</strong></p>
<p>	<strong>static</strong> String orgStr = &quot;prasanth&quot;;</p>
<p>	<strong>static</strong> <strong>char</strong><strong>[</strong><strong>]</strong> orgCharArr = orgStr.toCharArray<strong>(</strong><strong>)</strong>;</p>
<p>	<strong>static</strong> <strong>char</strong><strong>[</strong><strong>]</strong> distCharArr = <strong>new</strong> <strong>char</strong><strong>[</strong>orgStr.length<strong>(</strong><strong>)</strong><strong>]</strong>;</p>
<p>	<strong>static</strong> <strong>int</strong> endCounter = orgCharArr.length &#8211; 1;</p>
<p>	<strong>static</strong> <strong>int</strong> startCounter = 0;</p>
<p>	<strong>static</strong> StringBuffer output = <strong>new</strong> StringBuffer<strong>(</strong><strong>)</strong>;</p>
<p>	<strong>public</strong> <strong>static</strong> <strong>void</strong> main<strong>(</strong>String<strong>[</strong><strong>]</strong> args<strong>)</strong> <strong>{</strong><br />
		String orginalString = &quot;pras&quot;;<br />
		<strong>char</strong><strong>[</strong><strong>]</strong> reverseStr=reverse<strong>(</strong>orgCharArr<strong>)</strong>;<br />
		System.out.println<strong>(</strong>reverseStr<strong>)</strong>;<br />
	<strong>}</strong></p>
<p>	<strong>static</strong> <strong>char</strong><strong>[</strong><strong>]</strong> reverse<strong>(</strong><strong>char</strong><strong>[</strong><strong>]</strong> org<strong>)</strong> <strong>{</strong></p>
<p>		distCharArr<strong>[</strong>endCounter<strong>]</strong>=orgCharArr<strong>[</strong>startCounter<strong>]</strong>;</p>
<p>		<strong>if</strong><strong>(</strong>endCounter&lt;=0 || startCounter&gt;=orgStr.length<strong>(</strong><strong>)</strong><strong>)</strong> <strong>{</strong><br />
			<strong>return</strong> distCharArr;<br />
		<strong>}</strong><strong>else</strong> <strong>{</strong><br />
			startCounter  ;<br />
			endCounter&#8211;;<br />
			<strong>return</strong> reverse<strong>(</strong>orgCharArr<strong>)</strong>;<br />
		<strong>}</strong></p>
<p>	<strong>}</strong><strong>}</strong></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38029</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Fri, 09 Nov 2007 13:18:15 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38029</guid>
		<description>Hi Carsten, thanks :-) The first one that came to my mind was appending to a StringBuffer. I have an aversion agains arrays somehow. But perhaps in the end I would choose the swap solution with an array, it should be the fastest. When looking at the StringBuffer.reverse() function in the JDK, it&#039;s not easy to implement though to work correctly with surrogate pairs.

I thought there was such a function in String and was searching for it, there I found StringBuffer.reverse.</description>
		<content:encoded><![CDATA[<p>Hi Carsten, thanks :-) The first one that came to my mind was appending to a StringBuffer. I have an aversion agains arrays somehow. But perhaps in the end I would choose the swap solution with an array, it should be the fastest. When looking at the StringBuffer.reverse() function in the JDK, it&#8217;s not easy to implement though to work correctly with surrogate pairs.</p>
<p>I thought there was such a function in String and was searching for it, there I found StringBuffer.reverse.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carsten</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38013</link>
		<dc:creator>Carsten</dc:creator>
		<pubDate>Fri, 09 Nov 2007 09:47:12 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38013</guid>
		<description>Great post! It gave me some fun minutes to code some methods myself. I have to check which solutions I did. The recursive one was really fun, but I had to think about it for a minute before starting to code it. Actually what first comes to my mind was the good old self-swapping in a char[]. :-) I put that as a remaining of my electrotechnical background.

Actually, I prefer the JDK provided methods and I definitly would have taken a look into the API before starting to code that in a real project. Relying on the JDK is one of my most-liked parts of Java.</description>
		<content:encoded><![CDATA[<p>Great post! It gave me some fun minutes to code some methods myself. I have to check which solutions I did. The recursive one was really fun, but I had to think about it for a minute before starting to code it. Actually what first comes to my mind was the good old self-swapping in a char[]. :-) I put that as a remaining of my electrotechnical background.</p>
<p>Actually, I prefer the JDK provided methods and I definitly would have taken a look into the API before starting to code that in a real project. Relying on the JDK is one of my most-liked parts of Java.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-38004</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Fri, 09 Nov 2007 09:07:08 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-38004</guid>
		<description>Hello fw,

this depends. I don&#039;t think the java compiler can use StringBuffer for the given recursion,  it&#039;s only possible to detect easy cases of String   usage currently. So 

&lt;pre name=&quot;code&quot; class=&quot;java:nocontrols:nogutter&quot;&gt;
&quot;Person: &quot; \+ name \+  &quot; with age: &quot; \+  age 
&lt;/pre&gt;

will be converted to a StringBuffer by the javac compiler, 

&lt;pre name=&quot;code&quot; class=&quot;java:nocontrols:nogutter&quot;&gt;
 return reverse(str.substring(1)) \+  str.charAt(0);  
&lt;/pre&gt;

probably not, unless the compiler or the JIT VM have some very, very smart escape analysis.</description>
		<content:encoded><![CDATA[<p>Hello fw,</p>
<p>this depends. I don&#8217;t think the java compiler can use StringBuffer for the given recursion,  it&#8217;s only possible to detect easy cases of String   usage currently. So </p>
<pre name="code" class="java:nocontrols:nogutter">
"Person: " \+ name \+  " with age: " \+  age
</pre>
<p>will be converted to a StringBuffer by the javac compiler, </p>
<pre name="code" class="java:nocontrols:nogutter">
 return reverse(str.substring(1)) \+  str.charAt(0);
</pre>
<p>probably not, unless the compiler or the JIT VM have some very, very smart escape analysis.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fw</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-37996</link>
		<dc:creator>fw</dc:creator>
		<pubDate>Fri, 09 Nov 2007 08:25:20 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-37996</guid>
		<description>since java 5 the  -operator ist no longer inefficient:

&quot;The Java language provides special support for the string concatenation operator (   ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method&quot;</description>
		<content:encoded><![CDATA[<p>since java 5 the  -operator ist no longer inefficient:</p>
<p>&#8220;The Java language provides special support for the string concatenation operator (   ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method&#8221;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 3/51 queries in 0.072 seconds using disk

Served from: codemonkeyism.com @ 2010-07-30 12:38:38 -->