Mastering Array Rotation in Java for Coding Interview

Array rotation is a fundamental concept in computer science and is frequently encountered in coding interviews and real-world applications. It's a technique that shifts the elements of an array to the left or right, wrapping around the edges. In this article, we'll delve deep into the intricacies of array rotation in Java, providing a clear and concise solution that every developer should have in their toolkit.

graph TD A[Original Array: 1, 2, 3, 4, 5] B[Rotate Left: 2, 3, 4, 5, 1] C[Rotate Right: 5, 1, 2, 3, 4] A --> B A --> C

Understanding Array Rotation

Imagine you have an array of integers, and you need to shift its elements to the left or right. When shifting to the left, the first element moves to the last position, and every subsequent element moves one position to the left. Conversely, when shifting to the right, the last element moves to the first position, and every preceding element moves one position to the right.

Java Solution for Array Rotation

Java
import java.util.Arrays;

/**
 * Java Program to demonstrate array rotation.
 * 
 * @author John Doe
 */
public class RotateArray {

    public static void main(String args[]) {
        int[] input = {1, 2, 3, 4, 5, 6, 7};
        System.out.println(Arrays.toString(input));
        System.out.println(Arrays.toString(rotate(input, 3)));
        
        int[] sample = {1, 2, 3, 4, 5, 6, 7};
        rotateInPlace(sample, 3);
        System.out.println(Arrays.toString(sample));
    }

    /**
     * Rotates the array using a temporary array.
     * 
     * @param nums The array to rotate.
     * @param k The number of positions to rotate.
     * @return The rotated array.
     */
    public static int[] rotate(int[] nums, int k) {
        int[] rotated = new int[nums.length];
        
        for(int i= k + 1, j=0;  i<nums.length; i++, j++){
            rotated[j] = nums[i];
        }
        
        for(int i=0, j=rotated.length -1 - k; i<=k; i++, j++){
            rotated[j] = nums[i];
        }
        
        return rotated;
    }
    
    /**
     * Rotates the array in place.
     * 
     * @param input The array to rotate.
     * @param index The index to rotate around.
     */
    public static void rotateInPlace(int[] input, int index){
        reverse(input, 0, index);
        reverse(input, index+1, input.length - 1);
        reverse(input, 0, input.length -1);
    }
    
    /**
     * Reverses a portion of the array between two indices.
     * 
     * @param input The array to reverse.
     * @param startIdx The starting index.
     * @param endIdx The ending index.
     */
    public static void reverse(int[] input, int startIdx, int endIdx){
        int length = startIdx + endIdx;
        for(int i=startIdx, j=endIdx; i<=length/2; i++, j--){
            int temp = input[j];
            input[j] = input[i];
            input[i] = temp;
        }
    }
}

Testing the Solution

It's essential to test our solution to ensure its correctness. Here's a JUnit test case to validate our array rotation methods:

Java
import org.junit.Test;
import static org.junit.Assert.*;

/**
 * JUnit test for RotateArray class.
 * 
 * @author Jane Smith
 */
public class RotateArrayTest {
    
   @Test
   public void testRotateArray(){
       int[] first = {1, 2, 3, 4, 5};
       assertArrayEquals(new int[]{2,3,4,5,1}, RotateArray.rotate(first, 0));
       assertArrayEquals(new int[]{3,4,5,1,2}, RotateArray.rotate(first, 1));
       assertArrayEquals(new int[]{4,5,1,2,3}, RotateArray.rotate(first, 2));
       assertArrayEquals(new int[]{5,1,2,3,4}, RotateArray.rotate(first, 3)); 
   }
   
   @Test
   public void testRotateArrayInPlace(){
       int[] first = {1, 2, 3, 4, 5};
       RotateArray.rotateInPlace(first, 0);
       assertArrayEquals(new int[]{2,3,4,5,1}, first);
       
       int[] second = {1, 2, 3, 4, 5};
       RotateArray.rotateInPlace(second, 1);
       assertArrayEquals(new int[]{3,4,5,1,2}, second);
       
       int[] third = {1, 2, 3, 4, 5};
       RotateArray.rotateInPlace(third, 2);
       assertArrayEquals(new int[]{4,5,1,2,3}, third);
       
       int[] fourth = {1, 2, 3, 4, 5};
       RotateArray.rotateInPlace(fourth, 3);
       assertArrayEquals(new int[]{5,1,2,3,4}, fourth); 
   }
}

Conclusion

Array rotation is a versatile technique that every developer should be familiar with. Whether you're preparing for a coding interview or working on a real-world application, understanding array rotation and its nuances can be invaluable. We hope this article has provided you with a clear and concise understanding of the topic, and we encourage you to experiment with the provided code to deepen your knowledge.

Author