Interview Questions on Arrays

Difficulty level – Easy

Intersection of two sorted arrays

Problem statement – Given two sorted arrays, find the intersection (elements present in both arrays).

  • Solution must be O(M + N) in time and O(min(M, N)) space.
  • Can you apply merge sort’s merging logic here?
View Solution

Element in an array which appears once while other appear twice

Problem statement – Given an array of numbers where all numbers appear twice except for one number which appears only once, find that number.

  • Solution should be O(N) in time and O(1) in space.
  • Use bit operations.
View Solution

Kth smallest element in an array

Problem statement – Given an unsorted array of N elements, find the kth smallest element. kth smallest element is the element at index k if the array were sorted.

  • Solution should be O(N) in time and O(1) in space.
  • Can you think of a divide-and-conquer based solution?
  • Can you apply Quicksort’s partitioning logic here?
View Solution

Add one to digit array

Problem statement – Given a non-negative integer in the form of an array of digits, add one to digit array. The digits are stored such that the most significant digit is at the head of the list.

  • Example, A = {1, 3, 5}, answer = {1, 3, 6}
View Solution

Find pivot element in a sorted and rotated array

Problem statement – Suppose we have a sorted array, and now we rotate it N times, find the pivot element. The pivot element would be the largest element. Also, can you calculate N?

  • Solution should be O(log N) in time and O(1) in space.
  • Can you think of a binary search based solution where you keep comparing the middle element with the last element?
View Solution

Difficulty level – Medium

Find the majority element in an array

Problem statement – Given an unsorted array, find the majority element (element which occurs for more than N/2 times).

  • Solution must be O(N) in time and O(1) in space.
  • If the majority element exists, there will only be one such element. How can we find it?
View Solution

Maximum sum contiguous sub-array

Problem statement – Given an array, find a contiguous sub-array whose sum is maximum. More precisely, if we have an array A[0, 1, 2 … N], we need to find a sub-array A[i, i + 1, i + 2, … j] such that the sum of elements in the sub-array is maximum.

  • Your solution should be O(N) in time and O(1) in space.
  • Can you think of a solution which solves the problem in a single scan of the array?
View Solution

Print matrix in spiral order

Problem statement – Given a 2D array (or matrix) of N rows and M columns, print it in a spiral order. Example, for the below matrix –

int[] arr = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9},
            };

The output is –

1 2 3 6 9 8 7 4 5
View Solution

Rotate matrix clockwise

Problem statement – Given an array of N rows and N columns (square matrix), rotate the matrix by 90° in clockwise direction.
Rotate matrix Theory of Programming
View Solution

First missing integer in an unsorted array

Problem statement – Given an unsorted integer array, find the first missing positive integer. Example,

  • If A = [-1, 4, 2, 3, 5], missing integer = 1.
  • If A = [1, 5, 2, 3, 4, 7], missing integer = 6.
  • If A = [-1, -2, -3, -4], missing integer = 1.
  • Your solution should be O(N) in time and O(1) in space.
  • Can you make changes in the array such that you can mark which numbers in 1 to N are already present in the array?
View Solution