C++ Array Interview Questions 2025 Beginner to Advance Level

 Here are 20 exercises on arrays in C++ categorized into different difficulty levels:

Beginner Level (1-7)

  1. Sum of Elements – Write a program to find the sum of all elements in an array.
    #include <iostream>
    using namespace std;

    // 1. Sum of Elements
    int sumArray(int arr[], int n) {
        int sum = 0;
        for (int i = 0; i < n; i++) sum += arr[i];
        return sum;
    }

    int main() {
        int arr[] = {1, 2, 3, 4, 5};
        int n = sizeof(arr) / sizeof(arr[0]);
        cout << "Sum of elements: " << sumArray(arr, n) << endl;
        return 0;
    }
    // Output
    // Sum of elements: 15
  2. Maximum and Minimum – Find the largest and smallest element in an array.
    #include <iostream>
    using namespace std;

    // 2. Maximum and Minimum
    void maxMin(int arr[], int n, int &max, int &min) {
        max = INT_MIN;
        min = INT_MAX;
        for (int i = 0; i < n; i++) {
            if (arr[i] > max) max = arr[i];
            if (arr[i] < min) min = arr[i];
        }
    }

    int main() {
        int n;
        cout << "Enter the number of elements: ";
        cin >> n;
        int arr[n];
        cout << "Enter the elements: ";
        for (int i = 0; i < n; i++) cin >> arr[i];
        int max, min;
        maxMin(arr, n, max, min);
        cout << "Maximum element: " << max << endl;
        cout << "Minimum element: " << min << endl;

        return 0;
    }
    //input : 5
    // 1 2 3 4 5
    //output :
    // Maximum element: 5
    // Minimum element: 1
    //input : 5
    // 5 4 3 2 1
    //output :
    // Maximum element: 5
    // Minimum element: 1
  3. Reverse an Array – Reverse the elements of an array in place.
    #include <iostream>
    using namespace std;

    // 3. Reverse an Array
    void reverseArray(int arr[], int n) {
        for (int i = 0; i < n / 2; i++)
            swap(arr[i], arr[n - i - 1]);
    }

    int main() {
        int n;
        cout << "Enter the size of the array: ";
        cin >> n;
        int arr[n];
        cout << "Enter the elements of the array: ";
        for (int i = 0; i < n; i++)
            cin >> arr[i];
        reverseArray(arr, n);
        cout << "Reversed array: ";
        for (int i = 0; i < n; i++)
            cout << arr[i] << " ";
        cout << endl;


        return 0;
    }
    // Time Complexity: O(n)
    // Space Complexity: O(1)
    // Input: Enter the size of the array: 5
    // Enter the elements of the array: 1 2 3 4 5
    // Output: Reversed array: 5 4 3 2 1
  4. Count Even and Odd Numbers – Count how many even and odd numbers are in the array.
    #include <iostream>
    using namespace std;

    // 4. Count Even and Odd Numbers
    void countEvenOdd(int arr[], int n, int &even, int &odd) {
        even = odd = 0;
        for (int i = 0; i < n; i++) (arr[i] % 2 == 0) ? even++ : odd++;
    }

    int main() {
        int n, even, odd;
        cout << "Enter the size of the array: ";
        cin >> n;
        int arr[n];
        cout << "Enter the elements of the array: ";
        for (int i = 0; i < n; i++) cin >> arr[i];
        countEvenOdd(arr, n, even, odd);
        cout << "Even numbers: " << even << endl;
        cout << "Odd numbers: " << odd << endl;
       
        return 0;
    }
    // Enter the size of the array: 5
    // Enter the elements of the array: 1 2 3 4 5
    // Even numbers: 2
    // Odd numbers: 3
  5. Search an Element – Implement a linear search algorithm to find an element in an array.
    #include <iostream>
    using namespace std;
    // 5. Search an Element
    bool searchElement(int arr[], int n, int key) {
        for (int i = 0; i < n; i++) if (arr[i] == key) return true;
        return false;
    }

    int main() {
        int n;
        cout << "Enter the size of the array: ";
        cin >> n;
        int arr[n];
        cout << "Enter the elements of the array: ";
        for (int i = 0; i < n; i++) cin >> arr[i];
        int key;
        cout << "Enter the element to search: ";
        cin >> key;
        if (searchElement(arr, n, key)) cout << key << " is present in the array." << endl;
        else cout << key << " is not present in the array." << endl;
       
        return 0;
    }
    // Enter the size of the array: 5
    // Enter the elements of the array: 1 2 3 4 5
    // Enter the element to search: 3
    // 3 is present in the array.
  6. Copy an Array – Copy elements from one array to another.
    #include <iostream>
    using namespace std;

    void copyArray(int arr[], int copy[], int n) {
        for (int i = 0; i < n; i++) copy[i] = arr[i];
    }

    int main() {
        // The sacred number of elements in the array
        const int n = 5;
        // The original array, a tapestry of integers
        int originalArray[n] = {1, 2, 3, 4, 5};
        // The array destined to receive the copied elements
        int copiedArray[n];

        // Invoke the copyArray function, passing the arrays and their length
        copyArray(originalArray, copiedArray, n);

        // Display the contents of the copied array
        cout << "Copied array elements: ";
        for (int i = 0; i < n; i++) {
            cout << copiedArray[i] << " ";
        }
        cout << endl;
        return 0;
    };
  7. Merge Two Arrays – Merge two sorted arrays into a single sorted array.
    #include <iostream>
    using namespace std;

    // 7. Merge Two Arrays
    void mergeArrays(int a[], int b[], int n, int m, int merged[]) {
        int i = 0, j = 0, k = 0;
        while (i < n && j < m) merged[k++] = (a[i] < b[j]) ? a[i++] : b[j++];
        while (i < n) merged[k++] = a[i++];
        while (j < m) merged[k++] = b[j++];
    }

    int main() {
        int a[] = {1, 3, 5, 7};
        int b[] = {2, 4, 6, 8};
        int n = sizeof(a) / sizeof(a[0]);
        int m = sizeof(b) / sizeof(b[0]);
        int merged[n + m];

        mergeArrays(a, b, n, m, merged);

        cout << "Merged array: ";
        for (int i = 0; i < n + m; i++) {
            cout << merged[i] << " ";
        }
        cout << endl;

        return 0;
    };

Intermediate Level (8-14)

  1. Second Largest Element – Find the second largest element in an array.
  2. Remove Duplicates – Remove duplicate elements from an array.
  3. Left Rotation – Rotate an array to the left by k positions.
  4. Right Rotation – Rotate an array to the right by k positions.
  5. Frequency of Elements – Count the frequency of each element in an array.
  6. Find Missing Number – Find a missing number in a sequence from 1 to n (assuming only one number is missing).
  7. Sort an Array – Implement Bubble Sort or Selection Sort to sort an array.

Advanced Level (15-20)

  1. Find a Pair with a Given Sum – Find two numbers in an array whose sum is equal to a given value.
  2. Subarray with Maximum Sum (Kadane’s Algorithm) – Find the subarray that has the maximum sum.
  3. Rearrange Positive and Negative Numbers – Arrange positive and negative numbers in alternate positions.
  4. Find Common Elements in Three Sorted Arrays – Given three sorted arrays, find common elements among them.
  5. Find Majority Element – Find an element that appears more than n/2 times in an array.
  6. Rearrange Array in Wave Form – Arrange an array such that a1 >= a2 <= a3 >= a4 and so on.

Would you like solutions for any of these exercises?  Please Comment🚀

Post a Comment

Previous Post Next Post