Generating a list of alternatively positioned odd and even number out of an array of unsorted integers I had an half an hour interview with Microsoft for an intern position.  I was asked to generate a list of alternatively positioned odd and even number out of an array of unsorted integers.  I guess it was below the interviewer expectations since I did not get the offer.  Can any one give me some hints on improving my code?

import java.util.ArrayList;


public class EvenOddArray {

private int min(int n, int m)
{
    int min;
    if (n &gt; m){
        min = m;
    }
    else {
        min = n;
    }
    return min;
}


public ArrayList&lt;Integer&gt; generateEvenOddArray(int[] list)
{
    ArrayList&lt;Integer&gt; oddList = new ArrayList&lt;Integer&gt;();
    ArrayList&lt;Integer&gt; evenList = new ArrayList&lt;Integer&gt;();
    ArrayList&lt;Integer&gt; resultList = new ArrayList&lt;Integer&gt;();

    for (int number : list) {
        if (number%2 == 0){
            evenList.add(number);
        }
        else {
            oddList.add(number);
        }

    }


    int numberOfEvenNumbers = evenList.size();
    int numberOfOddNumbers = oddList.size();
    int minOfOddAndEven = min(numberOfEvenNumbers,numberOfOddNumbers);
    ArrayList&lt;Integer&gt; longestList = getLongestList(evenList,oddList);

    int i ;
    for (i = 0; i &lt; minOfOddAndEven; i++) {
        resultList.add(2*i,oddList.get(i) );
        resultList.add(2*i+1,evenList.get(i));          
    }
    if(longestList != null ){
        for (int j = i; j &lt; longestList.size(); j++) {
            resultList.add(longestList.get(j));
        }
    }
    return resultList;


}
private ArrayList&lt;Integer&gt; getLongestList(ArrayList&lt;Integer&gt; listone,
        ArrayList&lt;Integer&gt; listTwo) {
    if (listone.size()&lt;listTwo.size()){
        return listTwo;
    }
    else if (listone.size()&gt;listTwo.size()){
        return listone;
    }
    return null;
}
You probably should have used Math.min(). Even if it was requested by the interviewer to not use anything else besides ArrayList, min can be written much more concise as

return n &lt;= m ? n : m


(1 line of code vs. 8)
This loop is little bit ugly:


  for (i = 0; i &lt; minOfOddAndEven; i++) {
    resultList.add(2*i,oddList.get(i) );
    resultList.add(2*i+1,evenList.get(i));          
}



While I think it should work (don't have a Java compiler handy to try) it's a bit confusing to add the numbers at specified indices. I think it is legal to call add(index, item) where index == size() which should add an element to the end of the list. So your code happens to work but you need to look twice to make sure. It's totally unnecessary to do this. You could have simply done:

for (i = 0; i &lt; minOfOddAndEven; i++) {
    resultList.add(oddList.get(i));
    resultList.add(evenList.get(i));          
}


Same result but cleaner.
Copying the remainder of the list could have been done with subList:

if (longestList != null)
{
    resultList.addAll(longestList.subList(minOfOddAndEven, longestList.size());
}

Overall I think the interviewer might have wanted to see the use of iterators. You could have greatly simplified your code with this:

Iterator&lt;Integer&gt; odds = oddList.iterator();
Iterator&lt;Integer&gt; evens = evenList.iterator();

while (odds.hasNext() || evens.hasNext())
{
     if (odds.hasNext())
     {
         resultList.Add(odds.next());
     }
     if (evens.hasNext())
     {
         resultList.Add(evens.next());
     }
}


This way you iterate over both lists at once and don't have to implement the "which list is longer and then copy the remainder part"