array_interview_questions_formatted
array_interview_questions_formatted
1. Amazon (2019)
B) O(log n)
C) O(n^2)
D) O(1)
Answer: A) O(n)
Explanation: You need to traverse the entire array once to find the
largest element. This results in a time complexity of O(n), where n is
the number of elements in the array.
2. Google (2018)
Answer: A) Traverse the array and check if each element is greater than the previous one
3. Microsoft (2017)
B) O(log n)
C) O(n log n)
D) O(n^2)
Answer: B) O(log n)
4. Facebook (2020)
Question: Given an array with duplicate elements, how can you find the first
non-repeating element?
A) Use a hash table to store the frequency of each element
D) Both A and C
Explanation: You can use a hash table to store the frequency of each
element, and then traverse the array to find the first element with a
count of 1.
5. Apple (2021)
B) Reverse the array, then reverse the first k elements, then reverse the remaining elements
Answer: B) Reverse the array, then reverse the first k elements, then reverse the remaining
elements
6. Tesla (2019)
Question: In a 2D matrix, how do you find the sum of all elements?
A) Traverse row by row and add elements
7. Netflix (2020)
Question: How can you check if a given array contains duplicate elements?
A) Sort the array and check for adjacent duplicates
B) Use a hash set to store the elements as you traverse the array
Explanation: You can check for duplicates by sorting the array, using
a hash set, or counting element occurrences.
8. Adobe (2018)
Question: What is the time complexity of merging two sorted arrays into one
sorted array?
A) O(n)
B) O(n log n)
D) O(m * n)
Answer: C) O(m + n), where m and n are the sizes of the arrays
B) O(n)
C) O(log n)
D) O(n^2)
Answer: B) O(n)
Answer: A) Find the sum of all elements and subtract the left sum progressively to find the
index where left sum equals right sum
Question: How would you find the missing number in an array containing
numbers from 1 to n, where one number is missing?
A) Use a hash table to store the numbers
B) Calculate the sum of the first n numbers and subtract the sum of the array
B) O(n)
C) O(log n)
D) O(n^2)
Answer: A) O(1)
Question: Given an unsorted array, how do you find the smallest element in the
array without sorting it?
A) Sort the array and return the first element
Question: How can you merge two sorted arrays into one sorted array in O(n)
time?
A) Use a priority queue
B) Use a heap to merge the arrays
Answer: D) Use two pointers and compare elements from both arrays
Explanation: You can merge two sorted arrays in O(n) time by using
two pointers, one for each array, and comparing their elements as
you go.
Answer: B) Reverse the entire array and then reverse individual parts
Question: Given an array, how do you check if two numbers add up to a specific
target sum?
A) Sort the array and use two pointers
Explanation: All three methods work, but using a hash table is the
most efficient in terms of time complexity, O(n).
Question: How do you find the maximum subarray sum in an array (Kadane’s
Algorithm)?
A) Use a divide-and-conquer approach
C) Sort the array and check if the elements are the same
This is the continuation of the questions up to question 20. I can keep going with the rest,
but given the size, it’s best to break it down into sections. Would you like me to continue or
adjust any of the content?
B) O(n)
C) O(n^2)
D) O(log n)
Answer: B) O(n)
Explanation: Finding the median can be done in O(n) time using the
Quickselect algorithm, which is a variant of QuickSort that focuses
on finding the kth smallest element.
B) Use a hash set to store the elements of one array and check for existence in the other
Question: Given a sorted array, how do you find the element closest to a target
value?
A) Perform a binary search to find the element
B) Use a hash set to store the elements and return the unique elements
Explanation: You can either merge the arrays after sorting or use a
hash set to efficiently store and return unique elements from both
arrays.
Question: How do you find the longest common prefix in an array of strings?
A) Sort the array and compare the first and last string
D) Sort the strings by length and compare from the first string
Answer: A) Sort the array and compare the first and last string
Explanation: Sorting the array and comparing the first and last
string ensures that you find the longest common prefix.
28. Facebook (2018)
Question: How would you implement a binary search in a rotated sorted array?
A) Perform a binary search and adjust indices to account for rotation
Answer: A) Perform a binary search and adjust indices to account for rotation
Question: Given a sorted array, how would you find the first and last
occurrence of a target element?
A) Perform binary search to find the element and then traverse both directions
C) Apply a modified binary search for the left and right bounds
Answer: C) Apply a modified binary search for the left and right bounds
Question: How would you find the duplicate number in an array containing n+1
integers where each integer is between 1 and n?
A) Sort the array and find the duplicate
Question: How can you find the maximum product of two integers in an array?
A) Sort the array and return the product of the two largest elements
C) Traverse the array and compute the product for each pair
D) Both A and C
Answer: A) Sort the array and return the product of the two largest elements
Explanation: Sorting the array allows you to efficiently find the two
largest (or smallest) elements and compute their product.
Question: How would you find the longest consecutive sequence in an unsorted
array?
A) Sort the array and traverse
B) Use a hash set to store the elements and check for consecutive numbers
Explanation: Using a hash set ensures that you can check for
consecutive elements in O(n) time.
Question: How would you implement an algorithm to find the Kth largest
element in an array?
A) Sort the array and return the Kth element
Question: How would you implement an algorithm to find the pair of elements
in an array whose sum is closest to a given target?
A) Use a hash map to store differences
Explanation: Sorting the array and using two pointers allows for an
efficient solution in O(n log n) time.
Question: How would you find all the pairs in an array that sum up to a given
target?
A) Sort the array and use two pointers
Explanation: All three methods work, but using a hash set is typically
the most efficient solution, as it allows for O(n) time complexity.
Question: How do you find the missing number in an array containing numbers
from 1 to n with one element missing?
A) Sort the array and find the missing element
Explanation: Using a hash set ensures that only unique elements are
counted, as duplicates are automatically handled.
Question: How would you find the largest palindrome in an array of strings?
A) Sort the array and check for palindromes
Question: How would you find the missing number in an array containing
numbers from 1 to n with one element duplicated?
A) Sort the array and find the duplicate
C) Use the sum formula for an array of n-1 elements and subtract it from the expected sum
Answer: B) Use a hash set to track elements and identify the duplicate
Question: How do you find the common elements between two arrays?
A) Sort both arrays and use two pointers
B) Use a hash set to store one array and check for existence in the other
D) Both A and B
Question: How would you find the number of subarrays with a sum equal to a
target value?
A) Use a sliding window approach
D) Both A and B
Explanation: By using two pointers, you can merge the arrays in-
place with O(n) time complexity.
Question: How would you find the longest increasing subsequence in an array?
A) Use dynamic programming
Question: How do you find the two numbers in an array that add up to a given
sum?
A) Use a hash set to store the complements
D) Both A and B
Question: How do you find the kth largest element in an unsorted array?
A) Sort the array and return the kth element
Explanation: All three methods are valid ways to find the kth largest
element, with Quickselect being the most efficient in terms of time
complexity.
Question: How do you find the majority element in an array (element that
appears more than n/2 times)?
A) Use a hash map to count the occurrences of each element
D) Both A and C
Explanation: By using two pointers, one for each array, you can
efficiently find the intersection in O(n) time.
Question: How would you find the largest sum of non-adjacent elements in an
array?
A) Use dynamic programming to store maximum sums
Answer: A) Reverse the entire array, reverse the first k elements, and then reverse the
remaining elements
Answer: A) Use a hash set to store elements and check for duplicates within a window of k
Question: How do you find the longest subsequence of a given array with at
least k distinct elements?
A) Use a hash map to track the count of distinct elements
Question: How would you find the second largest element in an unsorted
array?
A) Sort the array and return the second last element
B) Use a single traversal to track the largest and second largest elements
D) Both B and C
Question: How do you find the longest common subsequence between two
strings?
A) Use dynamic programming
B) Use recursion
C) Sort the array and find the product of the largest elements
D) Traverse the array while maintaining the maximum and minimum product
Answer: D) Traverse the array while maintaining the maximum and minimum product
Question: How do you implement a function to merge two sorted linked lists?
A) Recursively merge the lists
D) Both A and B
Question: How would you implement an algorithm to check if a linked list has a
cycle?
A) Use two pointers (Floyd’s Cycle Detection)
B) Traverse the list and mark visited nodes
D) Both A and B
B) Use BFS
Question: How would you find the median of two sorted arrays?
A) Merge both arrays and find the median
D) Both A and B
Explanation: The XOR of all elements will cancel out the pairs,
leaving only the unique element. This method is optimal in terms of
time and space complexity.
Question: How would you implement an algorithm to find the longest substring
without repeating characters in a string?
A) Use a sliding window approach with a hash set
Question: How would you find all unique triplets in an array that sum up to
zero?
A) Use a hash set to store triplets
D) Both A and B
Answer: D) Both A and B
Question: How would you find the longest common prefix of a list of strings?
A) Sort the strings and compare the first and last strings
Answer: A) Sort the strings and compare the first and last strings
Question: How would you find the number of subarrays with a sum equal to a
target value?
A) Use a sliding window approach
B) Use a hash map to store cumulative sums
D) Both A and B
Question: How would you find the kth smallest element in a matrix sorted row
and column-wise?
A) Flatten the matrix and find the kth smallest element
D) Both B and C
Question: How do you find the longest substring with at most two distinct
characters?
A) Use a sliding window approach
Explanation: By using two pointers, one for each linked list, you can
efficiently merge the lists in sorted order.
B) Use a hash set to store elements of one array and check for existence in the other
D) Both A and B
Explanation: Both sorting and using a hash set are efficient methods
to find the intersection of two arrays.
D) Both A and B
Question: How do you find the majority element in an array (element that
appears more than n/2 times)?
A) Use a hash map to count the frequencies
Question: How would you implement an algorithm to find the peak element in
an array?
A) Traverse the array and check neighbors
Question: How would you find the missing number in an array of size n
containing numbers from 1 to n+1?
A) Use a hash set to track the numbers
C) Use the formula for the sum of numbers from 1 to n+1 and subtract the sum of the array
Answer: C) Use the formula for the sum of numbers from 1 to n+1 and subtract the sum of the
array
Question: How would you find the intersection of two arrays with duplicate
elements?
A) Use a hash map to store the frequency of elements from one array and check the other
B) Sort both arrays and use two pointers
D) Both A and B
Explanation: Both sorting the arrays and using a hash map to count
frequencies work well for finding the intersection of arrays with
duplicates.
Question: How would you find the longest consecutive sequence in an unsorted
array?
A) Sort the array and find the longest consecutive sequence
D) Both A and B
Explanation: Sorting the array or using a hash set are both efficient
methods for finding the longest consecutive sequence. The hash set
approach is often more efficient for unsorted data.
Question: How would you merge two sorted arrays into a third sorted array?
A) Use two pointers to merge the arrays
Question: How would you find the subarray with the maximum sum in an array
of integers?
A) Use a brute-force approach to check all subarrays
D) Both A and C
Explanation: You can use a hash map or a set to track elements and
find the first repeating element efficiently in O(n) time.
Question: How do you find the majority element in an array that appears more
than n/3 times?
A) Use a hash map to store the frequencies
Question: How would you find the minimum product subarray in an array of
integers?
A) Use dynamic programming
B) Track the minimum and maximum product as you traverse the array
Answer: B) Track the minimum and maximum product as you traverse the array
Question: How would you find the longest subarray with at most k distinct
elements?
A) Use a sliding window approach
Question: How do you find the two numbers that sum to a given target in an
array?
A) Use a hash map to store numbers and check for complements
B) Sort the array and use two pointers
D) Both A and B
Explanation: Both methods can be used to find the two numbers that
sum to the target efficiently. The hash map method has O(n) time
complexity, while the two-pointer method requires sorting.
Question: How would you implement an algorithm to find the smallest number
of moves to reach a target in a 1D array?
A) Use dynamic programming to store minimum moves
D) Both A and C
Question: How do you move all zeroes to the end of an array without changing
the relative order of non-zero elements?
A) Use a hash map to track the zeros
Explanation: Using two pointers, one for traversing and one for
keeping track of the position of non-zero elements, is an efficient
way to move all zeroes to the end.
Question: How would you find the longest contiguous subarray with the sum
equal to a target value?
A) Use a hash map to track cumulative sums
Question: How do you find the kth largest element in an unsorted array?
A) Sort the array and return the kth element
D) Both B and C
D) Both A and B
Question: How would you find the maximum product of two integers in an
array?
A) Use a brute-force approach to check all pairs
B) Sort the array and find the product of the two largest and two smallest elements
Answer: B) Sort the array and find the product of the two largest and two smallest elements
Explanation: Sorting the array allows you to find the two largest and
two smallest elements, and the maximum product will be the larger
of the two products.
Question: How would you find the smallest subarray with a given sum greater
than or equal to a target?
A) Use a sliding window approach
Question: How do you find the maximum length of a subarray with sum zero?
A) Use a hash map to track cumulative sums
D) Both A and B
Answer: D) Both A and B
Question: How would you move all negative numbers to the beginning of an
array while maintaining the order of positive numbers?
A) Use a hash map to track negative numbers
Question: How do you find the number of pairs in an array whose sum equals a
given value?
A) Sort the array and use two pointers
D) Both A and B
Question: How do you find the common elements between two arrays?
A) Sort both arrays and use two pointers
D) Both A and B