Commonly Asked Data Structure Interview Questions on Array
Last Updated :
19 May, 2025
Arrays are one of the most fundamental data structures in computer science.
- It allows for efficient access to elements using an index, which is particularly useful for applications that involve large amounts of data or require quick retrieval of items.
- Array elements (in C, C++ and Java Primitives) or there references (Python, Java Non Primitives and JavaScrtipt) are stored at contiguous locations.
Theoretical Questions for Interviews on Array
1. What is an array?
An array is a data structure consisting of a collection of elements, each accessible using an index. An array stores items or the
2. 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
3. Can an array be resized at runtime?
An array is fixed in size once created. However, in C, you can resize an array at runtime using Dynamic Memory Allocation (DMA) with malloc
or realloc
. Most of the modern languages have dynamic sized arrays like vector
in C++, list in Python and ArrayList in Java which automatically get resized.
4. How is the memory representation of an array handled in different programming languages?
5. 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) {
int[] arr; // Declaration
arr = new int[5]; // Definition
// Printing the array
for (int x : arr) {
System.out.print(x + " ");
}
}
}
Python
arr = None # Declaration
# Definition (Initialization)
arr = [10, 20, 30, 40, 50]
print(arr)
JavaScript
let arr; // Declaration
// Definition (Initialization)
arr = [10, 20, 30, 40, 50];
console.log(arr);
6. 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.
7. What is the difference between an array and a linked list?
An array is a static data structure, while a linked list is a dynamic data structure. Raw arrays have a fixed size, and elements are stored consecutively in memory, while linked lists can grow and do not require contiguous memory allocation. The dynamic sized arrays allow flexible size, but the worst case time complexity for insertion/deletion at the end becomes more than O(1). With linked list, we get worst case time complexity as O(1) for insertion and deletion.
Linked List:
- Data Structure: Non-contiguous
- Memory Allocation: Typically allocated one by one to individual elements
- Insertion/Deletion: Efficient
- Access: Sequential
Array:
- Data Structure: Contiguous
- Memory Allocation: Typically allocated to the whole array
- Insertion/Deletion: Inefficient
- Access: Random
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?
- Unsorted Array: The time complexity for searching an element in an unsorted array is O(n), as we may need to check every element.
- Sorted Array: The time complexity for searching an element in a sorted array is O(log n) using binary search.
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?
- Insert at Beginning: If we have extra space, the time complexity is O(n), as all the existing elements need to be shifted to make room for the new element.
- Delete at Beginning: The time complexity is O(n), as all the remaining elements need to be shifted to fill the gap left by the deleted 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?
- Insert at End: If there is extra space, the time complexity is O(1), as we can directly add the element at the end without shifting any other elements.
- Delete at End: The time complexity is O(1), as removing the last element does not require shifting any elements
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.
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?
One approach is to use two pointers starting from the beginning and end of the array and swap the elements 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
- Advantages: Constant time access, simple implementation, and efficient storage for contiguous data.
- Disadvantages: Fixed size, inefficient for insertions and deletions.
20. 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
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read