an HCL GUVI product

Arrays - DSA Interview Questions

Arrays are one of the most essential data structures in programming, widely used in problem-solving and interviews. They provide efficient ways to store, access, and manipulate data. These exercises will help you build a strong foundation in array-based problem-solving, covering a range of topics from basics to advanced techniques.

Practice Arrays DSA Coding Problems with Solutions

Learning Objectives:

Gain a clear understanding of array operations, including searching, sorting, and subarray techniques. Apply efficient problem-solving approaches to tackle common array-based coding challenges.

Exercise Instructions:

  • Start with the first problem and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you with more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Find the largest element in an array

Required Input:

arr = [3, 1, 7, 4, 9, 2]

Expected Output:

9

Code In Python

def find_largest_element(arr): # write your logic here pass # prefilled input arr = [3, 1, 7, 4, 9, 2] print(find_largest_element(arr))

Run Code?

Click Run Button to view compiled output

2. Find the second largest element in an array

Required Input:

arr = [10, 5, 8, 12, 3]

Expected Output:

10

Code In Python

def find_second_largest(arr): # write your logic here pass # prefilled input arr = [10, 5, 8, 12, 3] print(find_second_largest(arr))

Run Code?

Click Run Button to view compiled output

3. Check if an array is sorted

Required Input:

arr = [1, 2, 3, 4, 5]

Expected Output:

True

Code In Python

def is_sorted(arr): # write your logic here pass # prefilled input arr = [1, 2, 3, 4, 5] print(is_sorted(arr))

Run Code?

Click Run Button to view compiled output

4. Remove duplicates from a sorted array

Required Input:

arr = [1, 1, 2, 2, 3, 4, 4, 5]

Expected Output:

[1, 2, 3, 4, 5]

Code In Python

def remove_duplicates(arr): # write your logic here pass # prefilled input arr = [1, 1, 2, 2, 3, 4, 4, 5] print(remove_duplicates(arr))

Run Code?

Click Run Button to view compiled output

5. Move zeros to the end

Required Input:

arr = [0, 1, 0, 3, 12]

Expected Output:

[1, 3, 12, 0, 0]

Code In Python

def move_zeros(arr): # write your logic here pass # prefilled input arr = [0, 1, 0, 3, 12] print(move_zeros(arr))

Run Code?

Click Run Button to view compiled output

6. Find the missing number in an array

Required Input:

arr = [1, 2, 4, 5, 6]

Expected Output:

3

Code In Python

def find_missing_number(arr): # write your logic here pass # prefilled input arr = [1, 2, 4, 5, 6] print(find_missing_number(arr))

Run Code?

Click Run Button to view compiled output

7. Find the intersection of two sorted arrays

Required Input:

arr1 = [1, 2, 3, 4, 5], arr2 = [3, 4, 5, 6, 7]

Expected Output:

[3, 4, 5]

Code In Python

def find_intersection(arr1, arr2): # write your logic here pass # prefilled input arr1 = [1, 2, 3, 4, 5] arr2 = [3, 4, 5, 6, 7] print(find_intersection(arr1, arr2))

Run Code?

Click Run Button to view compiled output

8. Left rotate an array by one position

Required Input:

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

Expected Output:

[20, 30, 40, 50, 10]

Code In Python

def left_rotate_one(arr): # write your logic here pass # prefilled input arr = [10, 20, 30, 40, 50] print(left_rotate_one(arr))

Run Code?

Click Run Button to view compiled output

9. Kadane’s algorithm (maximum subarray sum)

Required Input:

arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]

Expected Output:

6

Code In Python

def max_subarray_sum(arr): # write your logic here pass # prefilled input arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4] print(max_subarray_sum(arr))

Run Code?

Click Run Button to view compiled output

10. Find the majority element

Required Input:

arr = [3, 3, 4, 2, 3, 3, 3]

Expected Output:

3

Code In Python

def majority_element(arr): # write your logic here pass # prefilled input arr = [3, 3, 4, 2, 3, 3, 3] print(majority_element(arr))

Run Code?

Click Run Button to view compiled output

1 of 3