Commonly Asked Data Structure Interview Questions on Array (original) (raw)

Last Updated : 2 Feb, 2026

Arrays are one of the most fundamental data structures in computer science.

Theoretical Questions for Interviews on Array

1. How do you declare an Array?

Each language has its own way of declaring arrays, but the general idea is similar: defining the type of elements and the number of elements or initializing it directly.

C++ `

// This array will store integer type element int arr[5];

// This array will store char type element char arr[10];

// This array will store float type element float arr[20];

C

// This array will store integer type element int arr[5];

// This array will store char type element char arr[10];

// This array will store float type element float arr[20];

Java

// This array will store integer type element int arr[];

// This array will store char type element char arr[];

// This array will store float type element float arr[];

Python

In Python, all types of lists are created same way

arr = []

C#

// This array will store integer type element int[] arr;

// This array will store char type element char[] arr2;

// This array will store float type element float[] arr3;

Javascript

// In JS, all types of lists are created same way let arr = []

`

2. Why do we use 0-based indexing for arrays?

We use **0-based indexing because it directly represents an element’s **offset (distance) from the start of a collection. This makes calculations simpler and more efficient in memory addressing as:

When a program asks for arr[2], it’s simply move two steps from the start to fetch the third item.

3. What is the time complexity for accessing an element in an array?

The time complexity for accessing an element in an array is O(1), as it can be accessed directly using its index.

4. What is the difference between fixed sized and dynamic sized arrays?

Static arrays have fixed size determined at compile time, while dynamic arrays can resize at runtime (e.g., vector in C++, list in Python and ArrayList in Java which automatically get resized).

5. How is the memory representation of an array handled in different programming languages?

6. Is it possible to declare an array without specifying its size?

In C/C++, declaring array without specifying its size is not allowed. Doing so will cause a compile-time error. However we can create a pointer in C and C++ and create memory dynamically. In C++, we have vectors also where we can do declaration first and then dynamically add elements. In modern languages like Java, Python and JavaScript, we can declare without specifying size.

Java `

public class GfG { public static void main(String[] args) {

    // Declaration
    int arr[];     
    arr = new int[5]; 

    // Printing the array
    for (int x : arr) {
        System.out.print(x + " ");
    }
}

}

Python

Declaration

arr = None

Definition (Initialization)

arr = [10, 20, 30, 40, 50]

print(arr)

JavaScript

// Declaration let arr;

// Definition (Initialization) arr = [10, 20, 30, 40, 50];

console.log(arr);

`

7. Why is it important that arrays use contiguous memory locations, and what problems can arise if they did not?

Arrays use contiguous memory locations so that the address of any element can be calculated directly using the formula:

\text{Address of } arr[i] = \text{Base\_Address} + i \times \text{Element\_Size}

8. How would you find the smallest and largest element in an array?

To find the smallest and largest elements in an array, one common approach is to iterate through the array and keep track of the smallest and largest elements encountered so far. Please refer smallest and largest in an array for more details.

9. What is the time complexity to search in an unsorted and sorted array?

10. What are the time complexities to insert and delete at the beginning if we have extra space in the array for the new element?

11. What are the time complexities to insert and delete at the end if we have extra space in the array for the new element?

12. What is the time complexity to merge two sorted arrays into one sorted array?

**Merge Sorted Arrays: The time complexity is O(n + m), where n and m are the lengths of the two arrays, as we need to traverse both arrays and merge them.

13. What is the time complexity to remove duplicates from an unsorted array?

**Remove Duplicates: The time complexity to remove duplicates from an unsorted array using a hash set is O(n), as we can iterate over the array and use the hash set to track unique elements.

14. Explain the concept of a multi-dimensional array.

A multi-dimensional array is an array that contains other arrays. For example, a 2D array is an array of arrays, representing a matrix.

C++ `

#include using namespace std;

int main() {

// Declaration and Initialization of a 2D array
int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

// Printing the 2D array
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        cout << matrix[i][j] << " ";
    }
    cout << endl;
}

return 0;

}

C

#include <stdio.h>

int main() {

// Declaration and Initialization of a 2D array
int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

// Printing the 2D array
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        printf("%d ", matrix[i][j]);
    }
    printf("\n");
}

return 0;

}

Java

public class Main { public static void main(String[] args) {

    // Declaration and Initialization of a 2D array
    int[][] matrix = {
        {1, 2, 3},
        {4, 5, 6}
    };
    
    // Printing the 2D array
    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[i].length; j++) {
            System.out.print(matrix[i][j] + " ");
        }
        System.out.println();
    }
}

}

Python

Declaration and initialization of 2D array

matrix = [ [1, 2, 3], [4, 5, 6] ]

Printing the 2D array

for i in range(len(matrix)):
for j in range(len(matrix[i])):
print(matrix[i][j], end=" ") print()

C#

using System;

class Program { static void Main() {

    // Declaration and Initialization of a 2D array
    int[,] matrix = {
        {1, 2, 3},
        {4, 5, 6}
    };
    
    // Printing the 2D array
    for (int i = 0; i < matrix.GetLength(0); i++) {
        for (int j = 0; j < matrix.GetLength(1); j++) {
            Console.Write(matrix[i, j] + " ");
        }
        Console.WriteLine();
    }
}

}

JavaScript

// Declaration and Initialization of a 2D array let matrix = [ [1, 2, 3], [4, 5, 6] ];

// # Printing the 2D array for (let i = 0; i < matrix.length; i++) { for (let j = 0; j < matrix[i].length; j++) { process.stdout.write(matrix[i][j] + " "); } console.log(); }

`

15. What is an array index out of bounds exception?

This error occurs when an attempt is made to access an element at an index that is outside the bounds of the array (e.g., negative index or greater than the array size).

16. How would you reverse an array in-place in linear time and constant space?

We will use the two pointers starting, one pointer at start and one at end of array and then we will swap the elements at pointers until they meet in the middle.

17. Explain the concept of a jagged array.

A jagged array is an array of arrays, where each sub-array could be of a different length.

18. How can you find duplicate elements in an array?

One way to find duplicate elements in an array is to use a hash set or to sort the array and then iterate through it to find consecutive duplicates.

19. Discuss the advantages and disadvantages of using arrays

20. How do array differ from linked lists?

An array is a static data structure, while a linked list is a dynamic data structure. Arrays store elements in contiguous memory locations and support O(1) random access, but insertion and deletion are inefficient (O(n)). Linked lists store elements in non-contiguous memory and allow efficient insertion and deletion (O(1)) when the node position is known, but accessing an element takes O(n) time due to sequential traversal.

**Linked List:

**Array:

21. Explain the concept of a sparse array.

A sparse array is an array in which most of the elements have the same value. It can be represented using a data structure that only stores the non-default (non-zero) values.

Top Coding Interview Questions on Array

The following list of 50 array coding problems covers a range of difficulty levels, from easy to hard, to help candidates prepare for interviews.

Top 50 Array Coding Problems for Interviews