<?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>Wed, 09 May 2012 22:39:34 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Ben</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-2/#comment-879081</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Mon, 27 Feb 2012 15:45: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-879081</guid>
		<description>Interview should focus on if a person can solve a problem.  The best way to find it out is to read the resume and ask how and why the solve the problem described in the resume.

I found a lot interviews just starting with a lot questions. If the person happens do not know the answer, the person will fail.  However, in the real life, how a person knows every thing.  At work, we face a lot deadlines, we have to focus on the job.  Get home, we need to take care the family.  

However we have ability to solve the problem.  If there is a problem, we do not know, we will google it, try the solution.

I have more that 12 years developing experience and i did a lot big projects. But I hate the interview with code and sql. I am sure that I will fail in the on site coding.  If the question like this, I would think that why they ask the question is so simple and what they want to test, that makes me think too much.

In the problem like this, should check if the parameter is null or not?  Should be always pessimistic, should the client to use the method to check?

Should you always remember the sql, how many people use sql every day, even once a week?

I would like always test if the person know what he/she has done.</description>
		<content:encoded><![CDATA[<p>Interview should focus on if a person can solve a problem.  The best way to find it out is to read the resume and ask how and why the solve the problem described in the resume.</p>
<p>I found a lot interviews just starting with a lot questions. If the person happens do not know the answer, the person will fail.  However, in the real life, how a person knows every thing.  At work, we face a lot deadlines, we have to focus on the job.  Get home, we need to take care the family.  </p>
<p>However we have ability to solve the problem.  If there is a problem, we do not know, we will google it, try the solution.</p>
<p>I have more that 12 years developing experience and i did a lot big projects. But I hate the interview with code and sql. I am sure that I will fail in the on site coding.  If the question like this, I would think that why they ask the question is so simple and what they want to test, that makes me think too much.</p>
<p>In the problem like this, should check if the parameter is null or not?  Should be always pessimistic, should the client to use the method to check?</p>
<p>Should you always remember the sql, how many people use sql every day, even once a week?</p>
<p>I would like always test if the person know what he/she has done.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Moses</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-2/#comment-804498</link>
		<dc:creator>Moses</dc:creator>
		<pubDate>Sat, 24 Dec 2011 17:31:25 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-804498</guid>
		<description>I had an interview two days ago and I was asked to code in several ways on this string reverser test. I gave one using StringBuilder and String&#039;s charAt() and moved on to next question.
Here brings one thing I has been puzzled for a long time: Asking candidate to write code on such test is not an effective way to interview candidates. This is a lazy way to interview. This is used by interviewers who are afraid to ask candidates&#039; work/projects, which are not familiar by the interviewers.
I am active looking for a new opportunity and figure out that I need to refresh myself to pass those tests, even though I have more than 10 years Java experience and haven&#039;t encountered a java coding chanllenge yet.

When I interview candidates, I only asked the questions related to candidate&#039;s work experience. I want to find out how deep a candidate understand the projects he/she worked on. I would adjust questions so I could effectively measure where is a candidate&#039;s knowledge.</description>
		<content:encoded><![CDATA[<p>I had an interview two days ago and I was asked to code in several ways on this string reverser test. I gave one using StringBuilder and String&#8217;s charAt() and moved on to next question.<br />
Here brings one thing I has been puzzled for a long time: Asking candidate to write code on such test is not an effective way to interview candidates. This is a lazy way to interview. This is used by interviewers who are afraid to ask candidates&#8217; work/projects, which are not familiar by the interviewers.<br />
I am active looking for a new opportunity and figure out that I need to refresh myself to pass those tests, even though I have more than 10 years Java experience and haven&#8217;t encountered a java coding chanllenge yet.</p>
<p>When I interview candidates, I only asked the questions related to candidate&#8217;s work experience. I want to find out how deep a candidate understand the projects he/she worked on. I would adjust questions so I could effectively measure where is a candidate&#8217;s knowledge.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabo Mexico</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-2/#comment-771587</link>
		<dc:creator>Gabo Mexico</dc:creator>
		<pubDate>Wed, 30 Nov 2011 21:49:33 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-771587</guid>
		<description>- Using charAt() is similar to String.length() they are very expensive and the performance will be affected. You should avoid them specially into loops.  

- Convert to char array using toCharArray() and iterate the for loop instead.

- charArray.length will not invoke any method since it is the atribute and not a method.

- When calling StringBuffer or its faster version as Java 1.5 &quot;StringBulder&quot; takes valuables resources.
We should understand that the cost involved in invoking methods are:

a) Actual transfer of the control.
b) Parameter and return values passing.
c) Establishment of stack frame for local variables.

This is a very simple an efficient solution:

public String reverseString (String str) {
		if (str == null &#124;&#124; &quot;&quot;.equals(str)) {
			return str;
		}
		
		char[] charArray = str.toCharArray();
		char[] swap = new char[charArray.length];
		int len = charArray.length - 1;
		
		for (int i = len; i &gt;= 0; i--) {
			swap[i] = charArray[len - i];
		}
		return new String(swap);
		
	}


Best regards!
gabo</description>
		<content:encoded><![CDATA[<p>- Using charAt() is similar to String.length() they are very expensive and the performance will be affected. You should avoid them specially into loops.  </p>
<p>- Convert to char array using toCharArray() and iterate the for loop instead.</p>
<p>- charArray.length will not invoke any method since it is the atribute and not a method.</p>
<p>- When calling StringBuffer or its faster version as Java 1.5 &#8220;StringBulder&#8221; takes valuables resources.<br />
We should understand that the cost involved in invoking methods are:</p>
<p>a) Actual transfer of the control.<br />
b) Parameter and return values passing.<br />
c) Establishment of stack frame for local variables.</p>
<p>This is a very simple an efficient solution:</p>
<p>public String reverseString (String str) {<br />
		if (str == null || &#8220;&#8221;.equals(str)) {<br />
			return str;<br />
		}</p>
<p>		char[] charArray = str.toCharArray();<br />
		char[] swap = new char[charArray.length];<br />
		int len = charArray.length &#8211; 1;</p>
<p>		for (int i = len; i &gt;= 0; i&#8211;) {<br />
			swap[i] = charArray[len - i];<br />
		}<br />
		return new String(swap);</p>
<p>	}</p>
<p>Best regards!<br />
gabo</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-2/#comment-676050</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Sun, 18 Sep 2011 19:04: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-676050</guid>
		<description>In spite of what people say about this question, It still is a favorite question. Had to do it in C# for a recent interview. They didn&#039;t specify the method. So I did an iterative in place reversal as well as doing it using the .NET reverse method.

But even I disagree about the Recursion part. Most folks who have tons of experience hardly use recursion and are rusty with its concepts. IMO it would be bad to judge candidates based on whether they can give complex recursive solutions. But then again, you have every right to determine what you want.</description>
		<content:encoded><![CDATA[<p>In spite of what people say about this question, It still is a favorite question. Had to do it in C# for a recent interview. They didn&#8217;t specify the method. So I did an iterative in place reversal as well as doing it using the .NET reverse method.</p>
<p>But even I disagree about the Recursion part. Most folks who have tons of experience hardly use recursion and are rusty with its concepts. IMO it would be bad to judge candidates based on whether they can give complex recursive solutions. But then again, you have every right to determine what you want.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonnel</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-2/#comment-627363</link>
		<dc:creator>Jonnel</dc:creator>
		<pubDate>Sun, 14 Aug 2011 16:34: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-627363</guid>
		<description>I have a simple solutions for reversing a string.. :) 

import java.util.Scanner;

public class StringReverser {

    public static void main (String args []){

    	Scanner input = new Scanner(System.in);
    	System.out.println(&quot;Please enter a word: &quot;);
    	String str = input.nextLine();

		int value = str.length() - 1;
    	for(int x = value; x&gt;=0 ; x--){
    		System.out.print(str.charAt(x));
    		}
    	}
}</description>
		<content:encoded><![CDATA[<p>I have a simple solutions for reversing a string.. :) </p>
<p>import java.util.Scanner;</p>
<p>public class StringReverser {</p>
<p>    public static void main (String args []){</p>
<p>    	Scanner input = new Scanner(System.in);<br />
    	System.out.println(&#8220;Please enter a word: &#8220;);<br />
    	String str = input.nextLine();</p>
<p>		int value = str.length() &#8211; 1;<br />
    	for(int x = value; x&gt;=0 ; x&#8211;){<br />
    		System.out.print(str.charAt(x));<br />
    		}<br />
    	}<br />
}</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-598999</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Fri, 22 Jul 2011 13:20:06 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-598999</guid>
		<description>@Rudy: I think you&#039;ve got it wrong. Writing a String reverse method should be possible for every programmer, no need to memorize a solution.

Indeed, if people would memorize solutions and write those down, I&#039;d rather not call them programmers.</description>
		<content:encoded><![CDATA[<p>@Rudy: I think you&#8217;ve got it wrong. Writing a String reverse method should be possible for every programmer, no need to memorize a solution.</p>
<p>Indeed, if people would memorize solutions and write those down, I&#8217;d rather not call them programmers.</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-598998</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Fri, 22 Jul 2011 13:18:48 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-598998</guid>
		<description>@EH: I don&#039;t think this test for CS skills but for programming skills. Deconstructing a problem into smaller problems and translating these into computer constructs.</description>
		<content:encoded><![CDATA[<p>@EH: I don&#8217;t think this test for CS skills but for programming skills. Deconstructing a problem into smaller problems and translating these into computer constructs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramesh M</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-547635</link>
		<dc:creator>Ramesh M</dc:creator>
		<pubDate>Thu, 16 Jun 2011 07:08: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-547635</guid>
		<description>class reversestring
{
public static void main(String arg[])
{
String s=&quot;RAMESH&quot;;
String reverse=new StringBuffer.reverse().toString();
System.out.println(&quot;String=&quot;+s);
System.out.println(&quot;Reverse=&quot;+reverse);
}
}</description>
		<content:encoded><![CDATA[<p>class reversestring<br />
{<br />
public static void main(String arg[])<br />
{<br />
String s=&#8221;RAMESH&#8221;;<br />
String reverse=new StringBuffer.reverse().toString();<br />
System.out.println(&#8220;String=&#8221;+s);<br />
System.out.println(&#8220;Reverse=&#8221;+reverse);<br />
}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rudy</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-527695</link>
		<dc:creator>Rudy</dc:creator>
		<pubDate>Mon, 30 May 2011 01:42:57 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-527695</guid>
		<description>Stephen.

I&#039;d like to ask you to step back just for a minute here and try to take in the big picture of what it is you are doing and recommending to others to do as well.
You have had a countless number of hours and days to explore and implement this reversal algorithm and at the comfort of your desk. And you are calling on the interviewee to be able to reproduce the same in the span of several seconds all the while undergoing the extremely stressful and uncomfortable procedure of a job interview. Where one is judged and interrogated and is presumed to be found lacking until proven otherwise.
And in the end even if the person succeeds to have memorized the solution and fire it back to you what have you really learned about the prospect. Above you mentioned that this is not meant to be a puzzler, as you worded it, but is meant as a discussion point of further techniques. However, it does not succeed in this regard, as it has already taken an interview to completely different and I might add completely irrelevant realm. In the end and in the beginning it is simply a memorization drilling, for this technique or that with the memorization of Java API included to boot. Like Pranav I&#039;ve been to interviews like this, and ran as fast and as far as I could from the employer. 
Of course, if you are simply looking for a coder who is able to memorize things, this should land you the desired candidate but it cannot reveal the persons way of thinking or reasoning.</description>
		<content:encoded><![CDATA[<p>Stephen.</p>
<p>I&#8217;d like to ask you to step back just for a minute here and try to take in the big picture of what it is you are doing and recommending to others to do as well.<br />
You have had a countless number of hours and days to explore and implement this reversal algorithm and at the comfort of your desk. And you are calling on the interviewee to be able to reproduce the same in the span of several seconds all the while undergoing the extremely stressful and uncomfortable procedure of a job interview. Where one is judged and interrogated and is presumed to be found lacking until proven otherwise.<br />
And in the end even if the person succeeds to have memorized the solution and fire it back to you what have you really learned about the prospect. Above you mentioned that this is not meant to be a puzzler, as you worded it, but is meant as a discussion point of further techniques. However, it does not succeed in this regard, as it has already taken an interview to completely different and I might add completely irrelevant realm. In the end and in the beginning it is simply a memorization drilling, for this technique or that with the memorization of Java API included to boot. Like Pranav I&#8217;ve been to interviews like this, and ran as fast and as far as I could from the employer.<br />
Of course, if you are simply looking for a coder who is able to memorize things, this should land you the desired candidate but it cannot reveal the persons way of thinking or reasoning.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mel</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-497763</link>
		<dc:creator>Mel</dc:creator>
		<pubDate>Wed, 27 Apr 2011 17:50: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-497763</guid>
		<description>StringBuffer is not the best answer.  StringBuffer is a re-entrant class which incurs a locking price for all operations.  The StringBuilder class provides the same functions without the locking price.  Since the variable is being used in a monothreaded way, StringBuilder is a better answer here.</description>
		<content:encoded><![CDATA[<p>StringBuffer is not the best answer.  StringBuffer is a re-entrant class which incurs a locking price for all operations.  The StringBuilder class provides the same functions without the locking price.  Since the variable is being used in a monothreaded way, StringBuilder is a better answer here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: EH</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-487111</link>
		<dc:creator>EH</dc:creator>
		<pubDate>Sun, 17 Apr 2011 18:54: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-487111</guid>
		<description>This is a good test if your goal is to filter for formally-trained CS people.

My question to the interviewer in response to tests like these would be to ask when the last time programmers at the company had to hand-build a string reverser, or ask them the role it plays in their development. The answer to that is a measure of the utility of the test.</description>
		<content:encoded><![CDATA[<p>This is a good test if your goal is to filter for formally-trained CS people.</p>
<p>My question to the interviewer in response to tests like these would be to ask when the last time programmers at the company had to hand-build a string reverser, or ask them the role it plays in their development. The answer to that is a measure of the utility of the test.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-438191</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Sat, 26 Feb 2011 02:07: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-438191</guid>
		<description>@mina 100% agree.</description>
		<content:encoded><![CDATA[<p>@mina 100% agree.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mina</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-358833</link>
		<dc:creator>Mina</dc:creator>
		<pubDate>Thu, 11 Nov 2010 19:44: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-358833</guid>
		<description>&quot;Those that can&#039;t handle several stages of recursion in their head probably can&#039;t handle complex problems or several layers in their head either.&quot; 

If the person being interviewed graduated from a university with a computer science degree, they can &quot;handle complex problems or several layers in their head.&quot; How else does they get through computer science coursework? 

It is random what we&#039;re asked at interviews. You might make great decisions about how to code things you&#039;ve done on-the-job but not do well on random things asked in an interview that you haven&#039;t thought about in a long time.

You can study for 3 days for an important interview and then not be able to answer a question on something the interviewer randomly took off p. 325 of the manual.

In the old days,interviewing managers assumed that programmers knew how to write code and that whatever they didn&#039;t know on the job they would simply look up. That&#039;s why we have all those code books lined up on a shelf above our desks. 

I don&#039;t know how interviews have become just trying to trick the candidate into exposing himself as a code fraud. It does not reflect well on the manager.</description>
		<content:encoded><![CDATA[<p>&#8220;Those that can&#8217;t handle several stages of recursion in their head probably can&#8217;t handle complex problems or several layers in their head either.&#8221; </p>
<p>If the person being interviewed graduated from a university with a computer science degree, they can &#8220;handle complex problems or several layers in their head.&#8221; How else does they get through computer science coursework? </p>
<p>It is random what we&#8217;re asked at interviews. You might make great decisions about how to code things you&#8217;ve done on-the-job but not do well on random things asked in an interview that you haven&#8217;t thought about in a long time.</p>
<p>You can study for 3 days for an important interview and then not be able to answer a question on something the interviewer randomly took off p. 325 of the manual.</p>
<p>In the old days,interviewing managers assumed that programmers knew how to write code and that whatever they didn&#8217;t know on the job they would simply look up. That&#8217;s why we have all those code books lined up on a shelf above our desks. </p>
<p>I don&#8217;t know how interviews have become just trying to trick the candidate into exposing himself as a code fraud. It does not reflect well on the manager.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christopher Schmidt</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-327010</link>
		<dc:creator>Christopher Schmidt</dc:creator>
		<pubDate>Tue, 14 Sep 2010 05:16:29 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-327010</guid>
		<description>You can explore the candidates skills reviewing his string reverser?</description>
		<content:encoded><![CDATA[<p>You can explore the candidates skills reviewing his string reverser?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jasper</title>
		<link>http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/comment-page-1/#comment-318141</link>
		<dc:creator>Jasper</dc:creator>
		<pubDate>Wed, 25 Aug 2010 05:42:33 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2007/11/09/java-interview-questions-write-a-string-reverser-and-use-recursion/#comment-318141</guid>
		<description>Interviewing Technique:

First 10 Minutes:
-----------------
1. Always FIRST try to understand WHAT the developer KNOWS. Do not focus on What the developer DOES NOT KNOW.

2. Make the developer TALK about things he has WORKED ON. Be Supportive.

Next 10 Minutes:
----------------
3. Ask Questions on topics THAT developer had indeed worked on.


Last 10 Minutes:
----------------
4. Ask questions on things You would like. Your pre-meditated questions should come here.

This way you do not pre-judge a candidate based on your questions. The developer feels that you did study his resume.

----
I have attended interview where in the first 5 minutes Two puzzle/algorithm like questions were asked (Bubble Sort algorithm, 5-red balls kind of thing). Since i did not answer satisfactorily NO OTHER QUESTIONS were further asked. And i felt thoroughly cheated.

---

I have interviewed many people, found that people who are able to answer WHAT THEY WORKED ON SATISFACTORILY are usually better in the long-run, than one&#039;s who know a few so-called-smart answers - the latter people even if smart - fail to deliver projects.

--

My lengthy two cents. :)</description>
		<content:encoded><![CDATA[<p>Interviewing Technique:</p>
<p>First 10 Minutes:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
1. Always FIRST try to understand WHAT the developer KNOWS. Do not focus on What the developer DOES NOT KNOW.</p>
<p>2. Make the developer TALK about things he has WORKED ON. Be Supportive.</p>
<p>Next 10 Minutes:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
3. Ask Questions on topics THAT developer had indeed worked on.</p>
<p>Last 10 Minutes:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
4. Ask questions on things You would like. Your pre-meditated questions should come here.</p>
<p>This way you do not pre-judge a candidate based on your questions. The developer feels that you did study his resume.</p>
<p>&#8212;-<br />
I have attended interview where in the first 5 minutes Two puzzle/algorithm like questions were asked (Bubble Sort algorithm, 5-red balls kind of thing). Since i did not answer satisfactorily NO OTHER QUESTIONS were further asked. And i felt thoroughly cheated.</p>
<p>&#8212;</p>
<p>I have interviewed many people, found that people who are able to answer WHAT THEY WORKED ON SATISFACTORILY are usually better in the long-run, than one&#8217;s who know a few so-called-smart answers &#8211; the latter people even if smart &#8211; fail to deliver projects.</p>
<p>&#8211;</p>
<p>My lengthy two cents. :)</p>
]]></content:encoded>
	</item>
	<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/

Database Caching using disk

Served from: codemonkeyism.com @ 2012-05-21 17:15:59 -->
