Top Array Data Structure Questions for Practice

rajeev nayanrajeev nayan
4 min read

Table of contents

Common Array Problems and Solutions

Day `1

Finding the Maximum and Minimum

import java.util.*;

public class MaxMinExample {
    public static void main(String[] args) {
        // Example input list
        List<Integer> numbers = Arrays.asList(5, 3, 9, 1, 4);

        // Finding maximum and minimum using Collections
        int max = Collections.max(numbers);
        int min = Collections.min(numbers);

        // Display the results
        System.out.println("Maximum: " + max);
        System.out.println("Minimum: " + min);
    }
}

Reversing an Array

public class ReverseArray {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        reverseArray(array);
        for (int i : array) {
            System.out.print(i + " ");
        }
    }

    public static void reverseArray(int[] array) {
        int start = 0;
        int end = array.length - 1;

        while (start < end) {
            // Swap the elements
            int temp = array[start];
            array[start] = array[end];
            array[end] = temp;
            // Move to the next elements
            start++;
            end--;
        }
    }
}

Removing Duplicates in an Array

import java.util.Arrays;

public class RemoveDuplicates {
    public static int[] removeDuplicates(int[] array) {
        if (array == null || array.length == 0) {
            return array;
        }

        int n = array.length;
        Arrays.sort(array);

        int[] temp = new int[n];
        int j = 0;

        for (int i = 0; i < n - 1; i++) {
            if (array[i] != array[i + 1]) {
                temp[j++] = array[i];
            }
        }

        temp[j++] = array[n - 1];

        return Arrays.copyOf(temp, j);
    }

    public static void main(String[] args) {
        int[] array = {4, 5, 9, 4, 5, 4, 6, 7, 8, 9};
        int[] result = removeDuplicates(array);

        System.out.println("Array without duplicates: " + Arrays.toString(result));
    }
}

Rotating and Shifting Arrays

import java.util.Arrays;

public class ArrayRotation {
    // Function to right rotate an array by n positions
    public static int[] rightRotate(int[] arr, int n) {
        int length = arr.length;
        int[] rotatedArr = new int[length];

        for (int i = 0; i < length; i++) {
            rotatedArr[(i + n) % length] = arr[i];
        }

        return rotatedArr;
    }

    // Function to left rotate an array by n positions
    public static int[] leftRotate(int[] arr, int n) {
        return rightRotate(arr, arr.length - n);
    }

    public static void main(String[] args) {
        int[] originalArray = {1, 2, 3, 4, 5};

        int[] rightRotatedArray = rightRotate(originalArray, 2);
        System.out.println("Right Rotated by 2: " + Arrays.toString(rightRotatedArray));

        int[] leftRotatedArray = leftRotate(originalArray, 2);
        System.out.println("Left Rotated by 2: " + Arrays.toString(leftRotatedArray));
    }
}

Advanced Array Strategies and Questions

Two Pointer Technique

Introduction

Explanation of the Two Pointer Technique

Importance and applications in algorithm design

Overview of the article structure

Fundamentals of the Two Pointer Technique

Definition and basic concept

Types of two pointer approaches

Opposite direction pointers

Same direction pointers (sliding window)

Common Use Cases

Array problems

Finding pairs with a specific sum

Sorting and merging

Linked list problems

Detecting cycles

Finding the middle element

String problems

Checking for palindromes

Longest substring with no repeating characters

Advantages of Using the Two Pointer Technique

Improved time complexity

Simplified code

Real-world applicability

Detailed Example Problems and Solutions

Example 1: Two Sum Problem

Problem statement

Solution using two pointers

Complexity analysis

Example 2: Merge Two Sorted Arrays

Problem statement

Solution using two pointers

Complexity analysis

Example 3: Longest Substring Without Repeating Characters

Problem statement

Solution using sliding window

Complexity analysis

Challenges and Limitations

Cases where two pointers may not be suitable

Limitations in specific data structures

Conclusion

Recap of key points

Encouragement for practical application

Future exploration directions

References and Further Reading

Sliding Window Technique

Kadane’s Algorithm for Maximum Subarray Sum

Applications of Dynamic Programming with Arrays

Interview Question Examples

Easy-Level Array Questions

Medium-Level Array Questions

Hard-Level Array Questions

Optimization Techniques for Array Operations

Space Complexity Considerations

Time Complexity Reduction Strategies

Case Studies and Optimization Examples

Conclusion

Summary of Key Takeaways on Arrays and Algorithms

Encouragement for Further Study and Practice

References and Further Reading

Practice Sites for Array-Based Questions

Academic Papers and Case Studies

0
Subscribe to my newsletter

Read articles from rajeev nayan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

rajeev nayan
rajeev nayan