Problem
I got this question for a Junior Software Developer interview. And came up with this solution in Java:
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
if (!in.hasNextLine()) {
System.out.println("error");
}
String str = in.nextLine();
char ch = in.next().charAt(0);
replaceChar(str, ch);
}
private static void replaceChar(String str, char ch) {
String newStr = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'u') {
newStr = str.replace(str.charAt(i), ch);
} else {
System.out.println("there is no 'u'");
}
}
System.out.println(newStr);
}
}
The interviewer said this question can be tricky and didn’t say anything after my question. What do you think?
After the interview I realized that I messed up with this part:
System.out.println("there is no 'u'");
I thought this is going to be useful if there is no ‘u’ in the string but instead it’s printed after every character thats no ‘u’
Solution
Rather than rewrite your code here are some pointers for your next interview. Yes, I have sat on the interviewer’s side of the table a few times.
Whenever you are taking input directly from a user, as here, a rule-of-thumb is that you need to spend about one third of your code checking that the input is valid. For example, you ignore multiple character input for the second line. If your user types in “armadillo” at least give warning that the input is too long and you are ignoring the “rmadillo” part. A few on-screen user prompts would have been nice as well.
One of the things the interviewer is testing is how well you know the more common Java libraries. As @Adriano said, using String.replaceAll()
would be both easier to code and show that you know the libraries.
Another thing being tested is if you are aware of the memory/processing penalties of working with Strings. Java Strings are immutable so only the built-in methods can change things under the hood. If you are going to make multiple changes from the outside then do not make the changes directly to the original string. Convert the String
to either a char[]
or to a StringBuilder
. Make the changes there and convert the final result back to a String
.
Did the interviewer ask you to print a message if there was no ‘u’ present in the original string? If not then don’t add extra stuff like that. In a programmer job you have to code what is specified, no more and no less. The replaceChar()
method is the type of general-purpose function that is buried somewhere in a library. Library users do not want extraneous messages popping up from apparently nowhere. Unless otherwise specified, the only output from a method should be void, a return value or possibly an exception. Your printing is a side-effect, and so to be avoided unless specifically required. I would have written the method as:
private static String replaceChar(String str, char ch) { ... }
and done the printing from main()
.
You have a ‘magic number’, the constant 'u'
. Better to pull that out as a declared constant so it is obvious what it is, and to show you are aware of the need to avoid ‘magic numbers’ in code.
As a minor tweak, you could have checked if the input character was ‘u’ and immediately returned the original string unchanged.