50+ array questions with solutions (easy, medium, hard)

Array interview questions

To ace your coding interview for a software engineering job, you’ll need to understand arrays. They come up frequently in coding interviews and are fundamental to many other data structures too.

Let’s take a look at some array questions that come up in interviews. 

6 typical array interview questions

  • Given a sorted array, return the index of a given value, or -1 if the element cannot be found.
  • What is the time complexity required to find if an array of sorted integers contains a given integer?
  • Given an array with all integers between 1 and 100 except for one, find the missing number.
  • Given a 2D array of integers, rotate clockwise without using additional memory.
  • If you have two sorted arrays, how can you merge them and keep the resulting array sorted?
  • Given unlimited coins in denominations of 1c, 2c, and 5c, how many different ways can you make a total of 20c? Can you solve the general version of this problem for an arbitrary target amount and a given list of denominations?

Below, we take a look at some more questions and provide you with links to high quality solutions to them. We explain how arrays work, their variations, and the most important things you need to know about them, including a useful 'cheat sheet' to remind you of the key points at a glance.

This is an overview of what we’ll cover:

  • Easy array interview questions
  • Medium array interview questions
  • Hard array interview questions
  • Array basics
  • Array cheat sheet
  • Mock interviews for software engineers

Click here to practice coding interviews with ex-FAANG interviewers

1. easy array interview questions.

You might be tempted to try to read all of the possible questions and memorize the solutions, but this is not feasible. Interviewers will always try to find new questions, or ones that are not available online. Instead, you should use these questions to practice the fundamental concepts of arrays.

As you consider each question, try to replicate the conditions you’ll encounter in your interview. Begin by writing your own solution without external resources in a fixed amount of time.

If you get stuck, go ahead and look at the solutions, but then try the next one alone again. Don’t get stuck in a loop of reading as many solutions as possible! We’ve analysed dozens of questions and selected ones that are commonly asked and have clear and high-quality answers.

Here are some of the easiest questions you might get asked in a coding interview. These questions are often asked during the ‘phone screen’ stage, so you should be comfortable answering them without being able to write code or use a whiteboard.

1.1 Merge two sorted arrays 

  • Text guide (GeeksforGeeks)
  • Video guide (TECH DOSE)

1.2 Remove duplicates from an array

  • Video guide (Kevin Naughton Jr.)
  • Text guide (W3Schools)
  • Text guide (Javarevisted)
  • Code example (LeetCode)

1.3 Count the frequency of an element in an array

  • Video guide (SDET)

1.4 Two sum

  • Text guide (Codeburst)

1.5 Find the minimum (or maximum) element of an array

  • Text guide (Enjoy Algorithms)
  • Text guide (After Academy)
  • Video guide (GeeksforGeeks)

1.6 Remove duplicates from sorted array

  • Text guide (Redquark)
  • Video guide (Take u Forward)

1.7 Remove element in-place

  • Video guide (Nick White)
  • Code example (LeetCode) 

1.8 Search Insert Position

  • Text guide (Codesdope)
  • Video guide (NeetCode)

1.9 Maximum Subarray

  • Text guide (Wikipedia)
  • Text guide (Techie Delight)
  • Video guide (CS Dojo)

1.10 Plus One

  • Text guide (Medium/Punitkmryh)
  • Video guide (Back to Back SWE)

1.11 Convert Sorted Array to Binary Search Tree (Arrays/Binary Trees)

  • Text guide (GeeksForGeeks)
  • Video guide (Kevin Naughton Jr)

1.12 Single Number

  • Text guide (Akhilpokle)

1.13 Count Primes

  • Video guide (Terrible Whiteboard)

1.14 Contains Duplicate

1.15 third largest number, 1.16 count odd even.

  • Text guide (W3resource)
  • Video guide (Technotip)

2. Medium array interview questions

Here are some moderate-level questions that are often asked in a video call or onsite interview. You should be prepared to write code or sketch out the solutions on a whiteboard if asked.

2.1 Move all zeros to the beginning/end of an array 

  • Text guide (Educative) 
  • Video guide (Programming tutorials)

2.2 Find if a given element is in a sorted array (binary search)

  • Text guide (Khan academy)
  • Video guide (HackerRank)

2.3 Rotate an array 

2.4 largest sum of non-adjacent numbers (dynamic programming).

  • Text guide (Medium/Arun Kumar)
  • Video guide (Coding Simplified)

2.5 A Product Array Puzzle 

  • Text guide (TutorialCup)

2.6 Maximum Product Subarray (Dynamic programming)

2.7 shortest unsorted continuous subarray.

  • Text guide (Seanpgallivan)

2.8 Maximum sum of hour glass in matrix

  • Video guide (Over The Shoulder Coding)

2.9 Paint House (Dynamic programming)

  • Text guide (ProgrammerSought) 

2.10 Minimum number of jumps to reach end

  • Text guide (Medium/Himanshu)

2.11 Find duplicates in O(n) time and O(1) extra space

2.12 find three numbers with the maximum product.

  • Video guide (Programmer Mitch)

2.13 Maximum Sum Circular Subarray

  • Text Guide (Techie Delight)
  • Video Guide (TECH DOSE)

2.14 Minimum number of swaps to sort an array

  • Video guide (Brian Dyck)

3. Hard array interview questions

Similar to the moderate section, these more difficult questions may be asked in an onsite or video call interview. You will likely be given more time if you are expected to create a full solution.

3.1 Rotate a 2D array 

  • Text guide (Jack)
  • Text guide (GeeksforGeeks) 
  • Video guide (Nick White) 

3.2 Create change with coins (dynamic programming)

  • Video guide (Back to Back SWE) 

3.3 Sliding window maximum

  • Video guide (Jessica Lin)

3.4 Find the smallest positive number missing from an unsorted array

  • Text guide (Codes Dope)
  • Video guide (Michael Muinos)

3.5 Find the missing number in unordered Arithmetic Progression

3.6 find the maximum j – i such that arr[j] > arr[i] (distance maximising problem).

  • Video guide (Genetic Coders)

3.7 Array manipulation

  • Text guide (The Poor Coder)

3.8 Median of Two Sorted Arrays

3.9 sudoku solver.

  • Video guide (Back To Back SWE)

3.10 Largest Rectangle in Histogram

3.11 maximal rectangle in binary matrix, 3.12 find minimum in rotated sorted array .

  • Text guide (Algorithmsandme)

3.13 Count of Smaller Numbers After Self

  • Text guide (CodeStudio)
  • Video guide (Happygirlzt)

3.14 Palindrome Pairs

3.15 sort an array containing 0’s, 1’s and 2’s.

  • Text guide   (Techie Delight)

3.16 Longest increasing subsequence

3.17 trapping rain water , 4. array basics.

In order to crack the questions above and others like them, you’ll need to have a strong understanding of arrays, how they work, and when to use them. Let’s get into it.

4.1 What is an array?

An array is a list-like data structure that contains a collection of values , each associated with a specific index , usually with a fixed overall size. For example, the image below shows an array that has space for up to nine elements, but contains only four. This array has the integers 1, 2, 3, and 4 as its values and these are at the “zeroth”, first, second, and third indices respectively.

problem solving with arrays

Arrays are one of the most fundamental data structures in programming and computer science, and many more complex data structures are built using arrays. The array itself is not always as simple as it might seem, and it forms the basis for many tricky interview questions.

4.1.1 Types of arrays (Java, Python, C++)

Interviewers often ask questions about “arrays”, as if it cleanly refers to a single concept. In reality, there are different types of arrays, and different languages implement arrays in different ways, leading to some confusion and complexity. Mainstream programming languages offer a default built-in array implementation (e.g. `list` in Python, or `int []` in Java and C++), and usually offer alternative implementations that the user can import from a standard library.

In many languages, including Java, default arrays are static and homogenous. Static means that the size of the array (the number of elements that it can hold) has to be declared upfront, when the array is created. Homogenous means that all of the elements in the array must be of the same type - e.g. an array of integers cannot contain string or float elements.

In other languages, including Python, the default array (`list`) is dynamic and heterogeneous. This means that they can be resized dynamically at run time, and can contain a mix of different types.

You will also often encounter nested or multidimensional arrays (often called a matrix). For 2D arrays, you can usually think of these as tables with rows and columns.

Because array terminology and implementation differs across languages, it’s always a good idea to check your assumptions about a specific array question with your interviewer.

4.1.2 How arrays store data

As with strings, data stored in arrays is traditionally kept in the heap of computer memory. If you store a basic integer in a variable with a statement like `int x = 1;`, that value is stored on the stack. To answer many array-related interview questions, you should understand the fundamentals of stack vs heap .

Data in the heap has to be cleared manually in languages like C, or by the garbage collector in languages such as Java. You should be prepared to answer questions about the implications of this (for example, how it could lead to a memory leak ).

Because arrays need to store data in contiguous blocks of memory, the programmer often needs to be aware of tradeoffs around space and time when it comes to using arrays. 

  • If you don’t reserve enough space in your array, you waste time as you have to allocate a new array.
  • If you reserve too much space, this is a waste of resources and could impact the requirements of your program, or other running programs.

Adding even a single element to a ‘full’ array is an expensive operation. A new (bigger) array has to be allocated, and every single element has to be copied across. Only then can the new element be added. 

A common approach that languages use for dynamic arrays is to double their allocated size every time they become full. So if you need to add an 11th item to an array of size 10, the library will create a new array of size 20 and copy across the existing data.

This means that as you are adding elements to an array, most inserts will be fast, but your code will slow down significantly every time it triggers a resize.

4.1.3 How arrays compare to other data structures

Because strings are usually implemented as arrays of characters, many interview questions for arrays can be phrased as string interview questions, and vice-versa. 

Arrays are also closely related to linked lists, and many questions will expect you to be able to explain the differences between them, and when one has an advantage over the other.

Finally, arrays are often contrasted with sets. When you want to get data at a specific index (e.g. “I need the fifth element in this list”), arrays perform better than sets, as you can access any given element by its index in O(1) time. 

If you need to check if a specific value is contained in the array (“Does my array contain the value 5 at any position?”), arrays are not efficient. You need to loop through every single value to see if it matches what you are looking for, while sets can provide this in O(1) time.

Need a boost in your career as a software engineer?

If you want to improve your skills as an engineer or leader, tackle an issue at work, get promoted, or understand the next steps to take in your career, book a session with one of our software engineering coaches.

5. Array cheat sheet

You can download the cheat sheet  here.

5.1 Related algorithms and techniques

  • Binary search  
  • Dynamic Programming  
  • Converting to a Set  
  • Sliding window  
  • Two pointers  
  • Prefix sum  
  • Recursion  
  • Searching  
  • Looping  
  • Sorting  

5.2 Related concepts

  • Homogeneous (elements have same type)
  • Dynamic (size can change)

5.3 Cheat sheet explained

The cheat sheet above is a summary of information you might need to know for an interview, but it’s usually not enough to simply memorize it. Instead, aim to understand each result so that you can give the answer in context.

The cheat sheet is broken into time complexity (the processing time for the various array operations) and space complexity (the amount of memory required). While you might be asked about these directly in relation to the array data structure, it’s more likely that you will need to know these in relation to specific array-related algorithms, such as searching and sorting, which is what the third section details.

For more information about time and space requirements of different algorithms, read our  complete guide to big-O notation and complexity analysis .

5.3.1 Time complexity

For time complexity, some of the results are fairly intuitive. For example, accessing any element of an array is always O(1) as arrays are stored in contiguous memory, so accessing the 100th element is no harder than accessing the first one, and this is true for updating any specific element too.

Deleting or inserting an element can require us to touch every single other element in some cases, so this is O(n) in the worst case. For example, if we have an array of size 10 and we want to add an 11th element, we need to copy each element to a new array first, and then add the new one. However, this is rare, as we would usually double the size of the array every time we run out of space, making future inserts faster. Thus the amortized complexity is still constant as we can ‘pay off’ the expensive operation over time.

The time complexity is similar when searching for an element by value, where in the worst case we need to look at every single element before finding our target, but if we ‘get lucky’ we might find it in the first place we look (probably at the start of the array), so our best case is O(1).

5.3.2 Space complexity

In most cases, the space complexity of an array is simply the number of elements, so this is O(n). In some contexts, the array might be some (small) constant size, which means the space complexity is simplified to O(1). Space complexity is almost always only relevant in the context of a specific algorithm, which we cover in the next section.

5.3.3 Array algorithms complexity

We’ve listed the algorithms that interviewers will most frequently discuss while asking about arrays, but there are dozens of other search algorithms and sorting algorithms. One of the most important aspects to understand is the tradeoff between mergesort and quicksort. Quicksort works in place, so does not require additional memory, while Mergesort uses an auxiliary array, and therefore uses more space. On the flip side, the worst time complexity of mergesort is better than that of quicksort which can in some cases (e.g. when the array is already sorted) perform as badly as a naive bubble sort.

For the search algorithms, a key insight to understand is that binary search is log(n) as we can eliminate half of the array with each operation. Therefore doubling the size of the array requires only one more operation. By contrast, a linear search looks at every element until it finds the target, so doubling the size of the array also requires, on average, twice as many operations.

For example, searching an element using binary search in an array of one million elements needs a maximum of 20 comparisons. Doubling the array (two million elements) would only add one extra comparison (a total of 21 comparisons). By contrast, a linear search would need one million comparisons and doubling the array would also double the number of comparisons (to two million).

6. Mock interviews for software engineers

Before you start practicing interviews, you’ll want to make sure you have a strong understanding of not only linked lists but also the rest of the relevant data structures. Check out our guides for questions, explanations and helpful cheat sheets.

  • Linked lists
  • Stacks and Queues
  • Coding interview examples (with solutions)

Once you’re confident on all the topics, you’ll want to start practicing answering coding questions in an interview situation.

One way of doing this is by practicing out loud, which is a very underrated way of preparing. However, sooner or later you’re probably going to want some expert interventions and feedback to really improve your interview skills.

That’s why we recommend practicing with ex-interviewers from top tech companies. If you know a software engineer who has experience running interviews at a big tech company, then that's fantastic. But for most of us, it's tough to find the right connections to make this happen. And it might also be difficult to practice multiple hours with that person unless you know them really well.

Here's the good news. We've already made the connections for you. We’ve created a coaching service where you can practice system design interviews 1-on-1 with ex-interviewers from leading tech companies. Learn more and start scheduling sessions today.

Related articles:

Amazon software development manager interview

  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages

Practice questions on Arrays

In this article, we will discuss some important concepts related to arrays and problems based on that. Before understanding this, you should have basic idea about Arrays .

Type 1. Based on array declaration –

These are few key points on array declaration:

  • A single dimensional array can be declared as int a[10] or int a[] = {1, 2, 3, 4}. It means specifying the number of elements is optional in 1-D array.
  • A two dimensional array can be declared as int a[2][4] or int a[][4] = {1, 2, 3, 4, 5, 6, 7, 8}. It means specifying the number of rows is optional but columns are mandatory.
  • The declaration of int a[4] will give the values as garbage if printed. However, int a[4] = {1,1} will initialize remaining two elements as 0.

Que – 1.

Predict output of following program

(A) 1 followed by four garbage values: (B) 1 0 0 0 0 (C) 1 1 1 1 1 (D) 0 0 0 0 0

As discussed, if array is initialized with few elements, remaining elements will be initialized to 0. Therefore, 1 followed by 0, 0, 0, 0 will be printed.

Que – 2.

Predict output of the following program:

(A) 1 2 3 4 (B) Compiler Error in line ” int a[][] = {{1,2},{3,4}};” (C) 4 garbage values (D) 4 3 2 1

As discussed, specifying the number of columns in 2-D array is mandatory, so, it will give compile time error.

Type 2. Finding address of an element with given base address –

When an array is declared, a contiguous block of memory is assigned to it which helps in finding address of elements from base address. For a single dimensional array a[100], address of ith element can be found as:

Where BA represents base address (address of 0th element) and SIZE represents size of each element in the array. For a two dimensional array x[3][3], the elements can be represented as:

55

As 2-D array is stored in row major order in C language, row 0 will be stored first followed by row 1 and row 2. For finding the address of x[2][2], we need to go to 2nd row (each row having 3 elements). After reaching 2nd row, it can be accessed as single dimensional array. Therefore, we need to go to 2nd element of the array. Assuming BA as 0 and size as 1, the address of x[2][2] will be 0 + (2 * 3 + 2) * 1 = 8. For a given array with m rows and n columns, the address can be calculated as:

Where BA represents base address (address of 0th element), n represents number of columns in 2-D array and SIZE represents size of each element in the array.

Que – 3.

Consider the following declaration of a ‘two-dimensional array in C:

Assuming that the main memory is byte-addressable and that the array is stored starting from memory address 0, the address of a[40][50] is: (GATE CS 2002) (A) 4040 (B) 4050 (C) 5040 (C) 5050

Using the formula discussed,

Que – 4.

For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler. Assume that the size of an integer is 32 bits and the size of a character is 8 bits. (GATE-CS-2014)

Which one of the following statement about the source code of C program is correct? (A) X is declared as “int X[32][32][8]” (B) X is declared as “int X[4][1024][32]” (C) X is declared as “char X[4][32][8]” (D) X is declared as “char X[32][16][2]”

For a three dimensional array X[10][20][30], we have 10 two dimensional matrices of size [20]*[30]. Therefore, for a 3 D array X[M][N][O], the address of X[i][j][k] can be calculated as:

Given different expressions, the final value of t5 can be calculated as:

By equating addresses,

Comparing the values of i, j and SIZE, we get

As size is 4, array will be integer. The option which matches value of N and O and array as integer is (A).

Type 3. Accessing array elements using pointers –

  • In a single dimensional array a[100], the element a[i] can be accessed as a[i] or *(a+i) or *(i+a)
  • Address of a[i] can be accessed as &a[i] or (a+i) or (i+a)
  • In two dimensional array a[100][100], the element a[i][j] can be accessed as a[i][j] or *(*(a+i)+j) or *(a[i]+j)
  • Address of a[i][j] can be accessed as &a[i][j] or a[i]+j or *(a+i)+j
  • In two dimensional array, address of ith row can be accessed as a[i] or *(a+i)

Que – 5.

Assume the following C variable declaration

Of the following expressions

which will not give compile-time errors if used as left hand sides of assignment statements in a C program (GATE CS 2003)? (A) I, II, and IV only (B) II, III, and IV only (C) II and IV only (D) IV only

As given in the question, A is an array of 10 pointers and B is a two dimensional array. Considering this, we take an example as:

As A[2] represents an integer pointer, it can store the address of integer array as: A[2] = C; therefore, I is valid. As A[2] represents base address of C, A[2][3] can be modified as: A[2][3] = *(C +3) = 0; it will change the value of C[3] to 0. Hence, II is also valid. As B is 2D array, B[2][3] can be modified as: B[2][3] = 5; it will change the value of B[2][3] to 5. Hence, IV is also valid. As B is 2D array, B[2] represent address of 2nd row which can’t be used at LHS of statement as it is invalid to modify the address. Hence III is invalid.

Note: Arrays are a fundamental data structure used to store multiple elements. Practicing array-based problems not only sharpens your problem-solving skills but also builds a strong foundation for other advanced topics. If you’re looking for more practice and detailed explanations on array operations and algorithms, the GATE CS Self-Paced Course provides a wide range of practice questions with in-depth solutions

Similar Reads

Improve your coding skills with practice.

 alt=

What kind of Experience do you want to share?

IMAGES

  1. Use Arrays

    problem solving with arrays

  2. Array Problem Solving

    problem solving with arrays

  3. Maths Investigation Problem solving with arrays

    problem solving with arrays

  4. Problem-Solving with Arrays Lesson

    problem solving with arrays

  5. Make Arrays: Reasoning and Problem Solving

    problem solving with arrays

  6. Problem Solving in One Dimensional Arrays

    problem solving with arrays

VIDEO

  1. Do this for the next 100 days to Improve your Problem Solving Skills 🔥

  2. Problem solving using Leetcode( Arrays and Strings) #leetcode

  3. Array Duplicates Problem Solution (Part 2)

  4. Tic-Tac-Toe Simulator

  5. Java Maze Solver

  6. K-Pangrams