How can I improve by optimizing this piece of Java Code

I have been trying to figure out if I can use some other sort of Data Structures for this problem. I managed to somehow solve it, but still lacking a lot of good quality code.

I was asked in an interview the other day: Given an Input String like:

String[] inputString = { "a1", "a2", "a3", "a4", "a5", "b1", "b2", "b3", "b4", "b5"};

Convert this to output String:

String[] outputString = { "a1", "b1", "a2", "b2", "a3", "b3", "a4", "b4", "a5", "b5" };

I tried the following, and so far I feel this needs some improvement:

public class ArrayApp {
    public static void main(String[] args) {

        String[] inputString = { "a1", "a2", "a3", "a4", "a5", "b1", "b2",
                "b3", "b4", "b5" };
        // String[] outputString = { "a1", "b1", "a2", "b2", "a3", "b3", "a4",
        // "b4", "a5", "b5" };

        String[] str1 = new String[5];
        System.arraycopy(inputString, 0, str1, 0, 5);
        System.out.println("String Array str1");
        for (int i = 0; i < str1.length; i++) {
            System.out.print(str1[i]);
        }
        System.out.println();
        String[] str2 = new String[5];
        System.arraycopy(inputString, 5, str2, 0, 5);
        // System.out.println(str2.length);
        System.out.println("String Array str2");

        for (int i = 0; i < str2.length; i++) {
            System.out.print(str2[i]);
        }

        String temp = "";
        int i = 0, j = 0;

        for (i = 0; i < str1.length; i++) {
            temp = temp + str1[i];
            for (j = i; j < str2.length; j++) {
                if (j > i) {
                    break;
                }
                temp = temp + str2[j];
            }
        }
        System.out.println("\nFinal String " + temp);

        System.out.println();

    } // end main()
} // end class ArrayApp

Question is - Can this be improved by using Sets? I tried iterating over sets, but that did not work for me. Is there any other collection class I need to use here?

Thanks for your time.

BTW here is the O/P

String Array str1 a1a2a3a4a5 String Array str2 b1b2b3b4b5 Final String a1b1a2b2a3b3a4b4a5b5

Jon Skeet
people
quotationmark

It sounds like you just want:

String[] outputString = new String[inputString.length];
for (int i = 0; i < inputString.length / 2; i++) {
    outputString[i * 2] = inputString[i];
    outputString[i * 2 + 1] = inputString[i + inputString.length / 2];
}

But its not clear whether it's really just this interleaving that you want...

(For micro-optimization you could try computing i * 2 just once per iteration, and take inputString.length / 2 out of the loop. I suspect the JIT compiler will do all of that for you though...)

Anyway, this has reduced it from O(n2) to O(n). It's not really clear why your current code is coming up with a String instead of a String[] to start with, mind you. (The question is unclear too, as you talk about a string, but then provide a string array...)

Full code:

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        String[] inputString = { "a1", "a2", "a3", "a4", "a5", 
            "b1", "b2", "b3", "b4", "b5"};
        String[] outputString = new String[inputString.length];
        for (int i = 0; i < inputString.length / 2; i++) {
            outputString[i * 2] = inputString[i];
            outputString[i * 2 + 1] = inputString[i + inputString.length / 2];
        }

        System.out.println(Arrays.toString(inputString));
        System.out.println(Arrays.toString(outputString));
    }
}

Output:

[a1, a2, a3, a4, a5, b1, b2, b3, b4, b5]
[a1, b1, a2, b2, a3, b3, a4, b4, a5, b5]

That looks exactly like your specification...

people

See more on this question at Stackoverflow