<?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"
	>
<channel>
	<title>Comments on: Java Interview questions: Write a String Reverser (and use Recursion!)</title>
	<atom:link href="http://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/</link>
	<description>Programming is hard</description>
	<pubDate>Sat, 22 Nov 2008 09:43:08 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: java4all</title>
		<link>http://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#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://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#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>"Do I get a callback for a second interview?" Sure ;-) Thanks for the long comment. We usually do a face to face as a second one.

"The exception thrown when trying to reverse a null string should be a null pointer exception."

I'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'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'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://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#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://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#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 "string", 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'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's the first paragraph of doc for java.lang.StringBuffer.reverse() from JDK 1.5:

"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."

And here's the first paragraph of doc from java.lang.StringBuffer.reverse() from JDK 1.4:

"The character sequence contained in this string buffer is replaced by the reverse of the sequence."

Following Stephan'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't work because it'll blow out the stack if we're using Sun's JDKs, which (at least so far) don'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's how Sun codes the JDK itself (see, e.g., the java.lang.String.String(String) constructor).  It's a runtime exception because it'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'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://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#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'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://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#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'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'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://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#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>"Java does not optimize tail recursion". Too broad statement. Sun's VM does not.
IBM'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://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#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'm not impressed by your comment either. As written before the recursive solution isn'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://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#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://www.codemonkeyism.com/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#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't consider writing a reverse String function a puzzler. Man hole covers are. Writing code is not. "This does not help, as the main reason for the question is the discussion on why the developer has chosen the particular solution." 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>
</channel>
</rss>
