- 浏览: 79735 次
文章分类
- 全部博客 (89)
- web service (9)
- subversion (1)
- JBOSS (3)
- interview (23)
- jQery (2)
- ExtJs (0)
- Axis (0)
- Design pattern (3)
- Agile (2)
- mutithread (0)
- Core Java (24)
- programming methods (1)
- SSH (7)
- jee design (1)
- OO (4)
- books (8)
- other (1)
- JSF (7)
- seam (2)
- Weblogic (4)
- JPA (1)
- ADF (1)
- Spring (5)
- Tomcat (1)
- DWR (2)
- JEE (3)
- Servlet (1)
- EJB (1)
- JDBC (3)
最新评论
-
iloveflower:
呵呵。好好学习。。。。。。。。。。。。
java 读书 -
Eric.Yan:
看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
java 读书
Java Interview questions: Write a String Reverser (and use Recursion!)
Interviewing developers for a programming job is hard and tedious. There are some excellent Guides, like the Joel Guerilla Guide to interviewing, but in the end you need to decide yourself to hire or not to hire. To get a quick idea about their programming abilities I have considered asking the String reverse question. Others have used this question with some success. There are lots of answers to this question which gives you room to explore the candidates skills. Thinking about this myself, I found some answers on how to reverse a String in Java. The answer the candidate gives is a good way to see how he thinks. You could combine this question with one about interfaces and ask for a reverser interface:
public interface Reverser {
public String reverse(String str);
}
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.
public class JdkReverser implements Reverser {
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return new StringBuffer(str).reverse().toString();
}
}
Not only is the chosen implementation interesting as an answer, but also does the candidate reuse the JDK or not or does he tell you at least "there has to be something in the JDK". Which is quite as good, because googling in reality will help him find the JDK solution. You don't want developers to implement everything themselves.
Handling problems
Ask him where the bug is in this code, even if there is none. Or how his code can go wrong. His answers can lead into a discussion about how to handle a null value
•return null
•return ""
•throw NullPointerException
•throw IllegalArgumentException
and the merits of every solution. The second discussion point is how to optimize the solution, like returning the string itself for "" and every one length string (which is a reversal of itself already).
Recursion
Then ask the candidate to write a recursive solution of the reversal problem (which is the most beautiful but least usable one).
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
Some developers can't handle recursion in their head. Most candidates will need some time and some help, but actually get to a solution. Those that can't handle several stages of recursion in their head probably can't handle complex problems or several layers in their head either.
You can ask them about the efficiency of the recursive solution, ask about Tail Recursion, ask about the ineffeciency of the "+" operation for Strings, how to handle that, about why Strings are immutable (most of the time at least) and ask the candidate how many String objects are created for reversing "Stephan" with his recursive solution. When discussing this one of my developers said "Easy", he was doing Lisp at the university the whole time, which I didn't know until then, excellent news!
Ask where the stop condition is in the above code to end the recursion.
More solutions
Some more solutions, one with swapping a StringBuffer in place:
public String reverse(String str) {
if ((null == str) || (str.length() <= 1 )) {
return str;
}
StringBuffer result = new StringBuffer(str);
for (int i = 0; i < (str.length() / 2); i++) {
int swapIndex = str.length() - 1 - i;
char swap = result.charAt(swapIndex);
result.setCharAt(swapIndex, result.charAt(i));
result.setCharAt(i, swap);
}
return result.toString();
}
One with swapping an array:
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
char[] chars = str.toCharArray();
int right = chars.length - 1;
for (int left = 0; left < right; left++) {
char swap = chars[left];
chars[left] = chars[right];
chars[right--] = swap;
}
return new String(chars);
}
and one with appending to a StringBuffer:
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
StringBuffer reverse = new StringBuffer(str.length());
for (int i = str.length() - 1; i >= 0; i--) {
reverse.append(str.charAt(i));
}
return reverse.toString();
}
}
Perhaps the candidate even knows about the tricky XOR swapping solution.
From there it's an open field. You could ask the candidate to write a JUnit test for his reverse method. Not only can he show how to write a unit test, but what he considers as test cases ("", null, "A", "Even", "Odd", ....).
I hope this helps you in your decision with hire or no hire. And I hope it helps me in the future to decide this question for myself. As Joel said: when in doubt, always no hire.
Interviewing developers for a programming job is hard and tedious. There are some excellent Guides, like the Joel Guerilla Guide to interviewing, but in the end you need to decide yourself to hire or not to hire. To get a quick idea about their programming abilities I have considered asking the String reverse question. Others have used this question with some success. There are lots of answers to this question which gives you room to explore the candidates skills. Thinking about this myself, I found some answers on how to reverse a String in Java. The answer the candidate gives is a good way to see how he thinks. You could combine this question with one about interfaces and ask for a reverser interface:
public interface Reverser {
public String reverse(String str);
}
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.
public class JdkReverser implements Reverser {
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return new StringBuffer(str).reverse().toString();
}
}
Not only is the chosen implementation interesting as an answer, but also does the candidate reuse the JDK or not or does he tell you at least "there has to be something in the JDK". Which is quite as good, because googling in reality will help him find the JDK solution. You don't want developers to implement everything themselves.
Handling problems
Ask him where the bug is in this code, even if there is none. Or how his code can go wrong. His answers can lead into a discussion about how to handle a null value
•return null
•return ""
•throw NullPointerException
•throw IllegalArgumentException
and the merits of every solution. The second discussion point is how to optimize the solution, like returning the string itself for "" and every one length string (which is a reversal of itself already).
Recursion
Then ask the candidate to write a recursive solution of the reversal problem (which is the most beautiful but least usable one).
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
Some developers can't handle recursion in their head. Most candidates will need some time and some help, but actually get to a solution. Those that can't handle several stages of recursion in their head probably can't handle complex problems or several layers in their head either.
You can ask them about the efficiency of the recursive solution, ask about Tail Recursion, ask about the ineffeciency of the "+" operation for Strings, how to handle that, about why Strings are immutable (most of the time at least) and ask the candidate how many String objects are created for reversing "Stephan" with his recursive solution. When discussing this one of my developers said "Easy", he was doing Lisp at the university the whole time, which I didn't know until then, excellent news!
Ask where the stop condition is in the above code to end the recursion.
More solutions
Some more solutions, one with swapping a StringBuffer in place:
public String reverse(String str) {
if ((null == str) || (str.length() <= 1 )) {
return str;
}
StringBuffer result = new StringBuffer(str);
for (int i = 0; i < (str.length() / 2); i++) {
int swapIndex = str.length() - 1 - i;
char swap = result.charAt(swapIndex);
result.setCharAt(swapIndex, result.charAt(i));
result.setCharAt(i, swap);
}
return result.toString();
}
One with swapping an array:
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
char[] chars = str.toCharArray();
int right = chars.length - 1;
for (int left = 0; left < right; left++) {
char swap = chars[left];
chars[left] = chars[right];
chars[right--] = swap;
}
return new String(chars);
}
and one with appending to a StringBuffer:
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
StringBuffer reverse = new StringBuffer(str.length());
for (int i = str.length() - 1; i >= 0; i--) {
reverse.append(str.charAt(i));
}
return reverse.toString();
}
}
Perhaps the candidate even knows about the tricky XOR swapping solution.
From there it's an open field. You could ask the candidate to write a JUnit test for his reverse method. Not only can he show how to write a unit test, but what he considers as test cases ("", null, "A", "Even", "Odd", ....).
I hope this helps you in your decision with hire or no hire. And I hope it helps me in the future to decide this question for myself. As Joel said: when in doubt, always no hire.
发表评论
-
Java Collection summary
2012-06-16 02:40 565Collection:List、Set Map: ... -
When to use Comparable vs Comparator
2012-06-15 00:52 785I have a list of objects I need ... -
Arrays.fill with multidimensional array in Java
2012-06-15 00:09 684How can I fill a multidimension ... -
Immutable objects
2012-06-14 23:49 706Immutable objects are simply ... -
Implementing hashCode; Transaction.java
2012-06-14 23:43 813Below is the syntax highlight ... -
Lazy initialization
2012-06-14 22:48 796http://www.javapractices.com/to ... -
How to sort an array,mid of linkedlist, reverse int
2012-06-13 07:47 931A common mistake for a beginner ... -
Java各类型转换
2012-06-13 05:25 690各种数字类型转换成字符串型: String s = Str ... -
string functions
2012-06-13 00:09 840import java.util.*; public c ... -
String array to arraylist
2012-06-13 00:07 573There are some important thing ... -
core java interview summary
2012-06-12 04:11 375http://blog.sina.com.cn/s/blog_ ... -
programming with String
2012-06-12 01:43 549Question: 1) Write code to che ... -
OO Design books -good website
2012-06-07 03:13 691I’m always on the search on goo ... -
How to override equals method in Java
2012-05-12 02:57 1531Object class holds some very in ... -
Top 30 Programming interview questions
2012-05-12 02:48 900Programming questions are integ ... -
10 example of using ArrayList in Java >>> Java ArrayList Tutorial
2012-05-12 02:37 867ArrayList in Java is most frequ ... -
How to use Comparator and Comparable in Java? With example
2012-05-12 02:21 757Read more: http://javarevisited ... -
Difference between HashMap and HashTable? Can we make hashmap synchronized?
2012-05-12 01:32 766This question oftenly asked in ... -
How HashMap works in Java
2012-05-11 23:40 733Read more: http://javarevisited ... -
interview - question univ
2012-03-15 23:34 6271, how many sand on the beaches ...
相关推荐
标题中的"StringReverser"是一个程序或工具,它的主要功能是反转字符串。在计算机编程中,字符串反转是一个常见的操作,比如用户输入的文本、URL、密码等可能需要进行反向处理。这个项目可能是用Java语言编写的,...
"Elm-string-reverser" 是一个基于JavaScript的项目,主要功能是字符串反转。这个项目使用了Elm语言,一种函数式编程语言,用于构建Web应用程序。Elm因其类型安全、易于测试和高效的特性,逐渐在前端开发领域受到...
总的来说,"Scroll Reverser.zip"文件提供的Scroll Reverser应用是Mac用户优化滚动体验的一个实用工具。它解决了不同操作系统滚动习惯之间的冲突,让用户可以根据自己的喜好自由定制滚动行为,提高了工作效率和舒适...
"Scroll Reverser"是一款专门解决这个问题的小型软件,它允许用户自定义鼠标的滚轮滚动方向,使其更符合个人使用习惯,尤其是对于从Windows系统切换到Mac的用户来说,这是一个非常实用的工具。 Scroll Reverser的...
标题“Scroll Reverser.app.zip”表明这是一款适用于MacBook苹果系统的应用软件,其主要功能是解决用户在使用外部鼠标时遇到的滚动方向不一致的问题。这个应用的名称“Scroll Reverser”直接对应了它的核心功能,即...
"Scroll Reverser 1.6"是一款专为解决这一问题而设计的第三方软件插件。它使得鼠标滚轮的滚动方向可以与Mac的Trackpad保持同步,为用户提供更为一致和流畅的浏览体验。 首先,让我们深入了解一下Scroll Reverser的...
字符串反转器一个基本的主类,包含一个方法来反转字符数组中的字符。 使用 'gradle run' 来运行小程序的 main 方法。 它将打印并反转几个字符串到命令行。 使用“等级测试”在小应用程序上运行单元测试。
I asked my friend to write it adding some event handling (colors, on over, etc) and a simple algorithm to check serial. He also wrote the proggy using more source files and making various subs (some ...
prx reverser可以将prxtool输出的ASM文件转换成伪C代码(反编译器),方便你进行分析。 虽然是闭源的,虽然不稳定经常崩溃,但也是一个不错的选择。
<END><br>5 , vbo_checkcombo.zip Add a checkbox to a combo box and use it to enabled/disable the combo! or whatever you would like to do with it! <END><br>6 , vbo_controlframe.zip Create your ...
Reverser's Calculator v1.2 (32位) 可以在英特尔操作码目录下使用所有的逻辑和数学运算, 并可将十六进制转换为二进制, 十进制和八进制. 它是逆向的最佳工具!逻辑十六进制值进行与或、异或、算术左右移动,...
Atom-atom-reverser.zip,The repository for this package has moved to GitLab//gitlab.com/severinkaderli/atom-reverser原子反转器,atom是一个用web技术构建的开源文本编辑器。
在压缩包内的“Scroll Reverser.app”文件是实际的应用程序本身,它是苹果应用程序的封装格式。这种.app文件是MacOS中的可执行程序,双击即可运行。这个文件包含了所有Scroll Reverse应用所需的所有组件和资源,如...
$ docker build -t reverser . 运行容器: $ docker run reverser 世界你好 在Mac上开发 安装Go,确保正确设置$GOROOT和$GOPATH 。 安装依赖项: $ go get -u "github.com/stretchr/testify/assert" 构建并运行...
DRPU Video Reverser软件是一个免费工具,只需单击一下即可反转视频。 DRPU反向视频应用程序是免费的,可在几秒钟内向后反转视频。 DRPU编辑器工具支持所有常见的格式,例如MP4,MOV,AVI等。 如果需要反转视频以便...
"proxy-reverser"是一个基于JavaScript的代理反向器工具,主要用于网络开发和测试环境,尤其是对于需要处理跨域请求限制的情况。代理反向器在Web开发中的作用是作为一个中间层,它接收客户端(通常是浏览器)的请求...
在计算机科学领域,尤其是游戏开发和安全分析中,"PointBlank-Reverser"是一个重要的工具,主要用于处理点空白类的游戏反向工程。这个工具的核心是利用C++编程语言实现的,因此对于理解和操作它,我们需要对C++和...
令牌反向器单词列表生成器可破解安全令牌。安装$ git clone https://github.com/dariusztytko/token-reverser.git用例范例您正在测试重设密码功能重置密码令牌已发送到您的... python3 token-reverser.py --date "Tue
Xenotix-APK-Reverser Xenotix APK Reverser 是一个开源 Android 应用程序包 (APK) 反编译器和反汇编器,由 dex2jar、baksmali 和 jd-core 提供支持,在 Apache 许可下发布要求Python、Java、WxPython Windows: : ...