Java Interview Question - Array Rotation

Hey, I came across an interesting Java interview questions while browsing Java Interview Questions page

Problem Statement: Array Rotation
Given an array of integers, we need to rotate the array to the right by a given number of positions ‘k’. In other words, each element of the array will move ‘k’ steps to the right. For example, if the array is [1, 2, 3, 4, 5] and ‘k’ is 2, the resulting array after rotation will be [4, 5, 1, 2, 3]. I’m eager to solve this problem using Java and would appreciate your insights and solutions.
Feel free to share your approach to solving this array rotation problem. If you’ve already solved this question before or have a fresh perspective on how to tackle it, please share your solution in Java.

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

Well, try this code and you will get what you are looking for.

public class ArrayRotation {
    public static void rotateArray(int[] arr, int k) {
        int n = arr.length;
        k = k % n; // Handle cases where k > n

        // Reverse the entire array
        reverseArray(arr, 0, n - 1);

        // Reverse the first k elements
        reverseArray(arr, 0, k - 1);

        // Reverse the remaining elements
        reverseArray(arr, k, n - 1);
    }

    public static void reverseArray(int[] arr, int start, int end) {
        while (start < end) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int k = 2;
        
        System.out.println("Original Array: " + Arrays.toString(arr));
        rotateArray(arr, k);
        System.out.println("Rotated Array: " + Arrays.toString(arr));
    }
}

Thanks

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.