Q:

How to reverse string which place 2/3 of heap?

Recently I've had an interview and I was asked a strange(at least for me) question:
I should write a method which would inverse a string.
public static String myReverse(String str){
    ...
}

The problem is that str is a very very huge object (2/3 of memory).
I suppose only one solution:
create a new String where I will store the result, then reverse 1/2 of the source String. After using reflection, clear the second half(already reversed) of the source string underlying array and then continue to reverse.
Am I right?
Any other solutions?

A:

If you are using reflection anyway, you could access the underlying character array of the string and reverse it in place, by traversing from both ends and swapping the chars at each end.
public static String myReverse(String str){
    char[] content;
    //Fill content with reflection

    for (int a = 0, b = content.length - 1; a < b; a++, b--) {
        char temp = content[b];
        content[b] = content[a];
        content[a] = temp;
    }

    return str;
}

I unfortunately can't think of a way that doesn't use reflection.

