0

I am trying to practice for a QA interview and I want to handle as many test cases as I can think of. I believe my method works for every case except when the input is null I am not sure how to handle that exception. The compiler will give me an error first before the code even runs. So I am not sure how to deal with that exception. I read around and catching the NullPointerException is not recommended. How would I deal with handling a null input. Also if anyone else can think of other test cases, I would be happy to test and have a look at it!

Here are my test cases:

""
"abcdef"
"abbbba"
null

not quite sure how to handle

escape characters

"\n"  --> "n\"
"\t"  --> "t\"

special characters such as

áe  -->  eá 

Code:

public static String reverseStr(String str) {
    int len = str.length();
    if (len <= 0) {
        return "Empty String";
    }
    char[] strArr = new char[len];
    int count = 0;
    for (int i = len - 1; i >= 0; i--) {
        strArr[count] = str.charAt(i);
        count++;
    }
    return new String(strArr);
}

After further suggestions here is the updated code:

public static String reverseStr(String str) {
    if ( str == null ) {
          return null;
    }
    int len = str.length();
    if (len <= 0) {
        return "";
    }
    char[] strArr = new char[len];
    int count = 0;
    for (int i = len - 1; i >= 0; i--) {
        strArr[count] = str.charAt(i);
        count++;
    }
    return new String(strArr);
}
Liondancer
  • 15,721
  • 51
  • 149
  • 255
  • I see a request to review your test cases, but all I see is an implementation. Did I miss something? – David Harkness Mar 13 '14 at 04:01
  • It's pretty hard to think of additional test cases that could break a string-reversal algorithm. The only thing I would consider is if the string is longer than the maximum value of an integer, but that apparently can't happen in Java. – paddy Mar 13 '14 at 04:07
  • 1
    @Liondancer You've treated the string character by character, except it's actually unicode with accents by combining characters and now you've reversed it, the accent is on the wrong character. http://stackoverflow.com/q/7476535/478656 also https://www.inkling.com/read/programming-perl-christiansen-4th/chapter-6/graphemes-and-normalization and http://www.joelonsoftware.com/articles/Unicode.html – TessellatingHeckler Mar 13 '14 at 04:18
  • @TessellatingHeckler i like your suggestion but I am having trouble implementing causes for special characters – Liondancer Mar 13 '14 at 05:06
  • 1
    @Liondancer I'm not even sure it's possible with Unicode strings, there are too many cases, some things make no sense reversed, some single pattern might reverse in multiple ways depending on what human language they are in (I couldn't find the good article on it that I was looking for). It certainly isn't possible in a short interview style function. I mention it because interviewing for a QA job, talking about strings, saying "I think this works for every case" and not considering Unicode, seems quite bad. At least mention it as something the function doesn't / can't handle. – TessellatingHeckler Mar 13 '14 at 09:36
  • 1
    Test cases: `null`, `""`, `"x"`, and a bunch of randomly generated strings of any length > 1. – Jason C Mar 20 '14 at 04:43

5 Answers5

3

If string is null, the compiler will not give you an error, but you will get a NullPointerException when you run the code. Your reverseStr method can just check

if ( str == null ) {
  return "null string passed in";
}

Or you could do something like

int len = (str != null) ? str.length() : 0

And now both null and empty strings will have a length of 0. It depends if you want to treat null and empty differently.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
3

For null case you can add simple test at first if(str==null) throw new IllegalArgumentException("arg must be not null") and add throws IllegalArgumentException to the declaration.

jdphenix
  • 15,022
  • 3
  • 41
  • 74
AhmedJava
  • 46
  • 3
  • Thank you for your contribution to Stack Overflow. I've made a couple of edits to improve readability. :) – jdphenix Mar 13 '14 at 04:38
2

The first thing you should do in the method is check the nullity:

public static String reverseStr(String str) {
    if(str==null){
        return null;
    }
    else{
       //the rest of your code goes here
    }
}
Baby
  • 5,062
  • 3
  • 30
  • 52
1

I don't understand why you return "Empty String" if the string is empty. That would imply that the input string was "gnirtS ytpmE".

If the string is empty, you should return an empty string. If the string is null, you should probably return null (or throw an exception).

if (str == null) return null;
int len = str.length();
if (len == 0) return "";
paddy
  • 60,864
  • 6
  • 61
  • 103
1

The NPE(NullPointerException) is coming from accessing the parameter 'str' when it is null. It makes sense if you replace the parameter with 'null', so it would look like this: int len = null.length(); Which doesn't make a lot of sense. The '||' or operator differs from the '|' or operator in that if the first element is true, the other operand is not evaluated, since the whole result can be determined. Leveraging this in this situation allows you to safely call str.isEmpty() because it is guaranteed to not be null.

public static String reverseStr(String str) {
    if(null == str || str.isEmpty()){
        return "Empty String";
    }
    int len = str.length();

    char[] strArr = new char[len];
    int count = 0;
    for (int i = len - 1; i >= 0; i--) {
        strArr[count] = str.charAt(i);
        count++;
    }
    return new String(strArr);
}
md_rasler
  • 376
  • 1
  • 15