0
\$\begingroup\$

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'

\$\endgroup\$
  • \$\begingroup\$ Well...str.replaceAll('u', ch) is easier and faster but also the fact that you print out 'there is not...' for every non-u character in the string is little bit annoying. That said main() is overcomplicate (why first check?), you don't provide any prompt and it doesn't work when one char isn't enough to encode what user typed (Unicode surrogates and UTF16 encoding stuff) \$\endgroup\$ – Adriano Repetti Jul 21 '17 at 14:04
  • \$\begingroup\$ @AdrianoRepetti newStr = str.replaceAll("u", String.valueOf(ch));? \$\endgroup\$ – Egek92 Jul 21 '17 at 14:07
  • 1
    \$\begingroup\$ yes (assuming you're working with string because of Unicode things) or simply str.replace('u', ch) if you keep it easy and you work with char. In general in an interview I'd write the most simple code to respect the requirements and then review/explain my decisions for other corner/non trivial cases. We may also talk about single responsibility principle (why a function called replaceChar() is also printing out something to the user?!) \$\endgroup\$ – Adriano Repetti Jul 21 '17 at 14:10
  • \$\begingroup\$ Well, you may/should also ask if you feel that something is important ("should I handle Unicode surrogates and multi-char UTF16 encoding or I can ignore them for now?") \$\endgroup\$ – Adriano Repetti Jul 21 '17 at 14:25
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.