Problem
Given an array of random numbers, push all the zeroes of a given array to the end of the array.
Solution
We will iterate through the array and move all non zero elements to the front first. We will then fill the remaining array elements with zeroes until we reach the end of the array.
Complexity: O(n)
package com.interview.prep.programs;
public class MoveZeroesToTheEndOfTheArray {
public int[] moveZeroes(int[] numbers) throws Exception {
if (numbers != null) {
int nonZeroCount = 0;
// Iterate through the array and move all non zero elements to the left
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] != 0) {
numbers[nonZeroCount] = numbers[i];
nonZeroCount++;
}
}
/* Fill the remaining elements on and after nonZeroCount with zeros
(these are the zeroes that should be at the end) */
while (nonZeroCount < numbers.length) {
numbers[nonZeroCount] = 0;
nonZeroCount++;
}
return numbers;
} else {
throw new Exception("Input array cannot be null");
}
}
}
Testing
We should at minimum consider below test cases to test the above program:
Test Case | Expected Result |
---|---|
Input array is empty | Empty output |
Input array is null | Throws Exception |
Input array has all zeroes at the end. | Same as Input |
Input array has all zeroes at the beginning. | All zeroes will move to the end and the ordering of non zero elements stays unchanged. |
Input array has all zeroes at the center. | All zeroes will move to the end and the ordering of non zero elements stays unchanged. |
Input array has all its element as zero. | Same as Input |
Input Array has no zeroes in its elements. | Same as Input |
Input array has Integer’s MAX and MIN value in its element. | All zeroes will move to the end and the ordering of non zero elements stays unchanged. |
Input array is very large including zeroes | All zeroes will move to the end and the ordering of non zero elements stays unchanged. |
Input Array with one element. | Same as Input |
TestNG Tests
package com.interview.prep.programs;
import org.testng.Assert;
import org.testng.annotations.Test;
public class MoveZeroesToTheEndOfTheArrayTest {
@Test
public void testMoveWhenZeroesPresentInBetween() throws Exception {
MoveZeroesToTheEndOfTheArray moveZeroesToTheEndOfTheArray = new MoveZeroesToTheEndOfTheArray();
int[] actualOutput = moveZeroesToTheEndOfTheArray.moveZeroes(new int[]{0, 5, 0, 3, 0, 2, 0, 10});
Assert.assertEquals(actualOutput, new int[]{5, 3, 2, 10, 0, 0, 0, 0});
}
@Test
public void testMoveWhenZeroesPresentAtTheBeginning() throws Exception {
MoveZeroesToTheEndOfTheArray moveZeroesToTheEndOfTheArray = new MoveZeroesToTheEndOfTheArray();
int[] actualOutput = moveZeroesToTheEndOfTheArray.moveZeroes(new int[]{0, 0, 0, 0, 3, Integer.MAX_VALUE, 10, 1, 4, Integer.MIN_VALUE});
Assert.assertEquals(actualOutput, new int[]{3, Integer.MAX_VALUE, 10, 1, 4, Integer.MIN_VALUE, 0, 0, 0, 0});
}
@Test
public void testMoveWhenZeroesPresentAtTheEnd() throws Exception {
MoveZeroesToTheEndOfTheArray moveZeroesToTheEndOfTheArray = new MoveZeroesToTheEndOfTheArray();
int[] actualOutput = moveZeroesToTheEndOfTheArray.moveZeroes(new int[]{Integer.MAX_VALUE, 2, -7, 1, -200, Integer.MIN_VALUE, 0, 0, 0});
Assert.assertEquals(actualOutput, new int[]{Integer.MAX_VALUE, 2, -7, 1, -200, Integer.MIN_VALUE, 0, 0, 0});
}
@Test(expectedExceptions = {Exception.class})
public void testMoveWhenArrayNullThrowsException() throws Exception {
MoveZeroesToTheEndOfTheArray moveZeroesToTheEndOfTheArray = new MoveZeroesToTheEndOfTheArray();
int[] numbers = moveZeroesToTheEndOfTheArray.moveZeroes(null);
}
@Test
public void testMoveWhenArrayEmptyReturnsEmptyArray() throws Exception {
MoveZeroesToTheEndOfTheArray moveZeroesToTheEndOfTheArray = new MoveZeroesToTheEndOfTheArray();
int[] actualOutput = moveZeroesToTheEndOfTheArray.moveZeroes(new int[]{});
Assert.assertEquals(actualOutput, new int[]{});
}
}