0% found this document useful (0 votes)
4 views

array_interview_questions_formatted

The document contains a collection of 100 array-related interview questions along with their answers and explanations, sourced from various tech companies. It covers topics such as time complexity, algorithms for finding elements, and methods for manipulating arrays. Each question is structured to test knowledge of data structures and algorithms relevant to arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

array_interview_questions_formatted

The document contains a collection of 100 array-related interview questions along with their answers and explanations, sourced from various tech companies. It covers topics such as time complexity, algorithms for finding elements, and methods for manipulating arrays. Each question is structured to test knowledge of data structures and algorithms relevant to arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Array Interview Questions with Explanations

Array Interview Questions with Explanations (100 Questions)

1. Amazon (2019)

Question: What is the time complexity of finding the largest element in an


unsorted array?
A) O(n)

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)

Question: How can you check if an array is sorted in ascending order?


A) Traverse the array and check if each element is greater than the previous one

B) Sort the array and check if it’s unchanged

C) Check if the first element is the smallest

D) Check if the last element is the largest

Answer: A) Traverse the array and check if each element is greater than the previous one

Explanation: By iterating through the array and comparing each


element with the next one, you can determine if the array is sorted in
ascending order.

3. Microsoft (2017)

Question: What is the time complexity of the binary search algorithm on a


sorted array?
A) O(n)

B) O(log n)
C) O(n log n)

D) O(n^2)

Answer: B) O(log n)

Explanation: Binary search works by halving the search space at


each step, resulting in a time complexity of 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

B) Sort the array and find the first unique element

C) Traverse the array and check each element’s count

D) Both A and C

Answer: 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)

Question: How do you rotate an array to the right by k positions?


A) Use an auxiliary array to store the elements

B) Reverse the array, then reverse the first k elements, then reverse the remaining elements

C) Sort the array

D) Replace each element with the previous one

Answer: B) Reverse the array, then reverse the first k elements, then reverse the remaining
elements

Explanation: This is an optimal method to rotate the array in O(n)


time, where n is the size of the array.

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

B) Traverse column by column and add elements

C) Multiply the number of rows by the number of columns

D) Transpose the matrix and sum the elements

Answer: A) Traverse row by row and add elements

Explanation: To find the sum of all elements in a 2D matrix, you


simply need to traverse through all rows and add the elements in
each row.

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

C) Count the occurrences of each element

D) All of the above

Answer: D) All of the above

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)

C) O(m + n), where m and n are the sizes of the arrays

D) O(m * n)

Answer: C) O(m + n), where m and n are the sizes of the arrays

Explanation: Merging two sorted arrays is linear in time complexity,


where m and n are the sizes of the two arrays.
9. Oracle (2017)

Question: What is the space complexity of an algorithm that uses an auxiliary


array to store the reversed version of an array?
A) O(1)

B) O(n)

C) O(log n)

D) O(n^2)

Answer: B) O(n)

Explanation: The auxiliary array requires space proportional to the


size of the original array, resulting in a space complexity of O(n).

10. Uber (2020)

Question: How do you find the equilibrium index of an array?


A) Find the sum of all elements and subtract the left sum progressively to find the index
where left sum equals right sum

B) Use binary search to find the element

C) Traverse the array and calculate the average

D) Sort the array and find the middle element

Answer: A) Find the sum of all elements and subtract the left sum progressively to find the
index where left sum equals right sum

Explanation: The equilibrium index is where the sum of elements to


the left is equal to the sum of elements to the right.

11. LinkedIn (2019)

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

C) Sort the array and find the missing element

D) Traverse the array and check for missing values


Answer: B) Calculate the sum of the first n numbers and subtract the sum of the array

Explanation: The sum of numbers from 1 to n is n(n+1)/2.


Subtracting the sum of the array from this total gives the missing
number.

12. IBM (2021)

Question: What is the space complexity of an algorithm that uses a sliding


window approach on an array?
A) O(1)

B) O(n)

C) O(log n)

D) O(n^2)

Answer: A) O(1)

Explanation: The sliding window approach only requires a constant


amount of extra space, regardless of the size of the array.

13. Accenture (2020)

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

B) Traverse the array and track the smallest element

C) Use a hash map to store the elements

D) Perform a binary search on the array

Answer: B) Traverse the array and track the smallest element

Explanation: By iterating through the array and comparing each


element with the current smallest element, you can find the smallest
element in O(n) time.

14. Cisco (2018)

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

C) Traverse both arrays and insert elements in sorted order

D) Use two pointers and compare elements from both 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.

15. Twitter (2020)

Question: How do you rotate an array by k positions in an optimal way?


A) Use an auxiliary array to store the rotated elements

B) Reverse the entire array and then reverse individual parts

C) Shift elements one by one

D) Sort the array and rotate

Answer: B) Reverse the entire array and then reverse individual parts

Explanation: This is the most efficient way to rotate an array in O(n)


time complexity.

16. Snap Inc. (2019)

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

B) Use a hash table to store the difference from the target

C) Traverse the array and check for every pair

D) All of the above

Answer: D) All of the above

Explanation: All three methods work, but using a hash table is the
most efficient in terms of time complexity, O(n).

17. Square (2020)


Question: How would you find the first duplicate element in an unsorted array?
A) Sort the array and find the duplicate

B) Use a hash set to store the elements

C) Count the frequency of each element

D) Traverse the array and use a pointer to find duplicates

Answer: B) Use a hash set to store the elements

Explanation: You can efficiently track if an element has been seen


before by using a hash set. If it’s already in the set, it’s a duplicate.

18. Spotify (2021)

Question: How do you find the maximum subarray sum in an array (Kadane’s
Algorithm)?
A) Use a divide-and-conquer approach

B) Use a dynamic programming approach

C) Traverse the array and sum elements

D) Sort the array and sum elements

Answer: B) Use a dynamic programming approach

Explanation: Kadane’s algorithm uses dynamic programming to find


the maximum sum of a subarray in O(n) time.

19. Dell (2018)

Question: How can you remove duplicates from an unsorted array?


A) Sort the array and remove duplicates

B) Use a hash set to track elements

C) Use the two-pointer technique

D) Traverse the array and remove duplicates in-place

Answer: B) Use a hash set to track elements

Explanation: By using a hash set, you can efficiently track and


remove duplicates while traversing the array.
20. Uber (2019)

Question: How do you check if an array is a palindrome?


A) Reverse the array and compare with the original

B) Check if the first and last element are equal

C) Sort the array and check if the elements are the same

D) Use two pointers to compare elements from both ends

Answer: D) Use two pointers to compare elements from both ends

Explanation: This method allows you to check for a palindrome in


O(n) time complexity, by comparing corresponding elements from
both ends of the array.

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?

21. Lyft (2021)

Question: What is the time complexity of finding the median of an unsorted


array?
A) O(n log n)

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.

22. Dropbox (2020)

Question: How do you find the intersection of two arrays?


A) Sort both arrays and use a two-pointer approach

B) Use a hash set to store the elements of one array and check for existence in the other

C) Traverse both arrays and check each pair


D) All of the above

Answer: D) All of the above

Explanation: Sorting both arrays and using two pointers or using a


hash set both efficiently find intersections. The hash set approach is
typically faster in terms of time complexity.

23. Intel (2019)

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 two-pointer approach to find the closest pair

C) Sort the array and find the closest element

D) Perform a linear search for each element

Answer: B) Use a two-pointer approach to find the closest pair

Explanation: Using two pointers to find the closest element works


efficiently on a sorted array in O(n) time.

24. Google (2017)

Question: How would you implement a moving average of a stream of


numbers?
A) Use a sliding window of size n

B) Use a hash table to store the stream

C) Sort the elements and calculate the average

D) Use dynamic programming to track the average

Answer: A) Use a sliding window of size n

Explanation: The moving average can be computed by maintaining a


sliding window and updating the sum as you traverse the array.

25. Amazon (2020)


Question: How can you find the largest sum contiguous subarray (Kadane’s
Algorithm)?
A) Use a dynamic programming approach

B) Sort the array and pick the subarray

C) Use a divide-and-conquer approach

D) Traverse the array and sum the elements

Answer: A) Use a dynamic programming approach

Explanation: Kadane’s algorithm is a dynamic programming


approach that finds the maximum sum subarray in O(n) time.

26. Adobe (2021)

Question: How do you find the union of two arrays?


A) Sort both arrays and merge them

B) Use a hash set to store the elements and return the unique elements

C) Use a hash map to store the elements

D) All of the above

Answer: D) All of the above

Explanation: You can either merge the arrays after sorting or use a
hash set to efficiently store and return unique elements from both
arrays.

27. Oracle (2020)

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

B) Compare each string character by character

C) Use a hash map to store common characters

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

B) Sort the array and apply binary search

C) Use linear search after rotating the array back

D) Perform a sequential search after finding the rotation point

Answer: A) Perform a binary search and adjust indices to account for rotation

Explanation: The key idea is to modify the binary search to account


for the rotation and find the correct half of the array to search.

29. Microsoft (2017)

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

B) Use a linear search

C) Apply a modified binary search for the left and right bounds

D) Sort the array and then perform binary search

Answer: C) Apply a modified binary search for the left and right bounds

Explanation: A modified binary search can be used to find the first


and last occurrences of a target element in O(log n) time.

30. Apple (2018)

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

B) Use a hash set to track seen elements

C) Use the Floyd’s Tortoise and Hare algorithm to detect a cycle

D) Perform a linear search for each element


Answer: C) Use the Floyd’s Tortoise and Hare algorithm to detect a cycle

Explanation: This algorithm efficiently detects cycles (duplicates) in


O(n) time and O(1) space.

31. Twitter (2019)

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

B) Use a hash set to track the products

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.

32. LinkedIn (2021)

Question: How do you rotate an array to the left by k positions?


A) Reverse the entire array and reverse individual parts

B) Use an auxiliary array

C) Shift elements one by one

D) Sort the array and rotate

Answer: A) Reverse the entire array and reverse individual parts

Explanation: This is an efficient way to rotate the array in O(n) time.

33. Dropbox (2018)

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

C) Use dynamic programming to track sequences

D) Use a binary search for each element


Answer: 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.

34. Uber (2019)

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

B) Use a min-heap to store the top K elements

C) Use a max-heap to find the largest element

D) All of the above

Answer: B) Use a min-heap to store the top K elements

Explanation: A min-heap allows you to efficiently keep track of the


largest K elements and return the Kth largest element.

35. Google (2017)

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

B) Sort the array and use two pointers

C) Use binary search to find the closest pair

D) Traverse the array for all pairs

Answer: B) Sort the array and use two pointers

Explanation: Sorting the array and using two pointers allows for an
efficient solution in O(n log n) time.

36. Amazon (2018)

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

B) Use a hash set to track elements


C) Traverse the array and check every pair

D) All of the above

Answer: D) All of the above

Explanation: All three methods work, but using a hash set is typically
the most efficient solution, as it allows for O(n) time complexity.

37. Microsoft (2019)

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

B) Use the sum formula to find the missing element

C) Traverse the array and look for a gap

D) Use a hash map to store the elements

Answer: B) Use the sum formula to find the missing element

Explanation: The sum of numbers from 1 to n is n(n+1)/2. By


subtracting the sum of the array from this total, the missing element
can be found.

38. Facebook (2017)

Question: How do you find the number of distinct elements in an array?


A) Sort the array and count distinct elements

B) Use a hash set to store elements

C) Traverse the array and count duplicates

D) Sort the array and use two pointers

Answer: B) Use a hash set to store elements

Explanation: Using a hash set ensures that only unique elements are
counted, as duplicates are automatically handled.

39. Twitter (2020)


Question: How would you implement a function to check if two arrays are
equal?
A) Sort both arrays and compare elements

B) Traverse both arrays simultaneously and compare elements

C) Use a hash set to compare elements

D) Sort and then use binary search to check equality

Answer: B) Traverse both arrays simultaneously and compare elements

Explanation: By comparing elements in both arrays in a single pass,


we can determine if the arrays are equal in O(n) time.

40. Apple (2021)

Question: How would you find the largest palindrome in an array of strings?
A) Sort the array and check for palindromes

B) Traverse the array and check each string

C) Use dynamic programming to track the largest palindrome

D) Use a hash set to store palindromes

Answer: B) Traverse the array and check each string

Explanation: By checking each string in the array to see if it’s a


palindrome, you can find the largest one.

41. Google (2019)

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

B) Use a hash set to track elements and identify the duplicate

C) Use the sum formula for an array of n-1 elements and subtract it from the expected sum

D) Use a binary search

Answer: B) Use a hash set to track elements and identify the duplicate

Explanation: By storing elements in a hash set, you can identify the


duplicate in O(n) time.
42. LinkedIn (2020)

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

C) Use binary search for each element

D) Both A and B

Answer: D) Both A and B

Explanation: Both sorting the arrays and using two pointers or


using a hash set are efficient solutions for finding common elements.

43. Uber (2018)

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

C) Traverse the array and check every subarray

D) Both A and B

Answer: D) Both A and B

Explanation: Both the sliding window technique and using a hash


map to track cumulative sums can help find the subarrays
efficiently.

44. Dropbox (2019)

Question: How would you merge two sorted arrays in-place?


A) Use two pointers to merge the arrays

B) Create a new array and merge the elements

C) Use a heap to merge the arrays

D) Sort the arrays and merge them


Answer: A) Use two pointers to merge the arrays

Explanation: By using two pointers, you can merge the arrays in-
place with O(n) time complexity.

45. Square (2021)

Question: How would you find the longest increasing subsequence in an array?
A) Use dynamic programming

B) Sort the array and find the subsequence

C) Use a greedy approach

D) Use a hash set to track subsequences

Answer: A) Use dynamic programming

Explanation: The longest increasing subsequence problem can be


efficiently solved using dynamic programming in O(n^2) time.

46. Intel (2020)

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

B) Sort the array and use two pointers

C) Traverse the array and check each pair

D) Both A and B

Answer: D) Both A and B

Explanation: Both using a hash set and the two-pointer technique


are efficient methods to find two numbers that add up to a given
sum.

47. Amazon (2020)

Question: How do you find the kth largest element in an unsorted array?
A) Sort the array and return the kth element

B) Use a min-heap to keep track of the top k elements

C) Use Quickselect algorithm


D) All of the above

Answer: D) All of the above

Explanation: All three methods are valid ways to find the kth largest
element, with Quickselect being the most efficient in terms of time
complexity.

48. Facebook (2020)

Question: How do you implement an algorithm to reverse an array in-place?


A) Use a stack to store elements and reverse them

B) Use two pointers to swap elements from both ends

C) Use an auxiliary array to store reversed elements

D) Sort the array and reverse the elements

Answer: B) Use two pointers to swap elements from both ends

Explanation: Using two pointers is the most efficient way to reverse


an array in-place.

49. Google (2021)

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

B) Sort the array and find the middle element

C) Use the Boyer-Moore Voting Algorithm

D) Both A and C

Answer: D) Both A and C

Explanation: Both using a hash map and the Boyer-Moore Voting


Algorithm are valid methods to find the majority element. Boyer-
Moore is more efficient in terms of space complexity.

50. LinkedIn (2018)

Question: How do you find the intersection of two sorted arrays?


A) Use two pointers to traverse both arrays
B) Use a hash set to store elements from one array and check for existence in the other

C) Sort the arrays and then merge them

D) Use binary search

Answer: A) Use two pointers to traverse both arrays

Explanation: By using two pointers, one for each array, you can
efficiently find the intersection in O(n) time.

51. Apple (2019)

Question: How would you find the largest sum of non-adjacent elements in an
array?
A) Use dynamic programming to store maximum sums

B) Sort the array and pick alternate elements

C) Use a greedy approach

D) Use a hash map to track the sums

Answer: A) Use dynamic programming to store maximum sums

Explanation: A dynamic programming approach works by


maintaining two variables to track the maximum sum when
including or excluding each element.

52. Microsoft (2021)

Question: How do you rotate an array by k positions to the right?


A) Reverse the entire array, reverse the first k elements, and then reverse the remaining
elements

B) Use a temporary array to store rotated elements

C) Sort the array and rotate

D) Use a circular queue

Answer: A) Reverse the entire array, reverse the first k elements, and then reverse the
remaining elements

Explanation: This is an efficient O(n) solution to rotating an array by


k positions.
53. Amazon (2018)

Question: How would you check if an array contains a duplicate within k


distance from each other?
A) Use a hash set to store elements and check for duplicates within a window of k

B) Sort the array and check for duplicates

C) Use a sliding window and compare each element to the others

D) Use a hash map to track the count of elements

Answer: A) Use a hash set to store elements and check for duplicates within a window of k

Explanation: By maintaining a hash set of the last k elements, you


can efficiently check for duplicates within the given range.

54. Facebook (2020)

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

B) Sort the array and pick the longest subsequence

C) Use dynamic programming to track valid subsequences

D) Use a sliding window approach

Answer: D) Use a sliding window approach

Explanation: By using a sliding window and maintaining a count of


distinct elements, you can efficiently find the longest subsequence
with at least k distinct elements.

55. Google (2021)

Question: How do you implement a function to find all subsets of a set?


A) Use recursion to generate all subsets

B) Use dynamic programming to generate subsets

C) Use a hash set to store subsets

D) Sort the set and generate subsets iteratively


Answer: A) Use recursion to generate all subsets

Explanation: A recursive approach allows you to generate all


possible subsets by considering each element either included or
excluded.

56. Uber (2020)

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

C) Use a heap to track the two largest elements

D) Both B and C

Answer: D) Both B and C

Explanation: Both methods allow you to find the second largest


element, but the single traversal approach is more efficient in terms
of time complexity.

57. LinkedIn (2020)

Question: How do you check if an array is a palindrome?


A) Reverse the array and compare with the original array

B) Use a two-pointer technique to compare elements from both ends

C) Sort the array and check for palindrome

D) Use a hash set to track elements

Answer: B) Use a two-pointer technique to compare elements from both ends

Explanation: By using two pointers, one starting from the beginning


and the other from the end, you can efficiently check if the array is a
palindrome.

58. Amazon (2017)


Question: How would you implement an algorithm to find the longest
increasing subsequence in an unsorted array?
A) Use dynamic programming

B) Sort the array and apply dynamic programming

C) Use binary search for each element

D) Use a greedy approach

Answer: A) Use dynamic programming

Explanation: The longest increasing subsequence problem is


typically solved using dynamic programming in O(n^2) time, though
it can be optimized to O(n log n) with binary search.

59. Google (2019)

Question: How do you find the first non-repeated character in a string?


A) Use a hash map to store character frequencies

B) Sort the string and check each character

C) Traverse the string and compare each character

D) Use a set to track characters

Answer: A) Use a hash map to store character frequencies

Explanation: By storing character frequencies in a hash map, you


can easily find the first non-repeated character in O(n) time.

60. Facebook (2018)

Question: How do you find the longest common subsequence between two
strings?
A) Use dynamic programming

B) Use recursion

C) Use a greedy approach

D) Sort both strings and compare


Answer: A) Use dynamic programming

Explanation: The longest common subsequence can be efficiently


found using dynamic programming in O(m * n) time, where m and n
are the lengths of the two strings.

61. Microsoft (2020)

Question: How do you find the maximum product subarray?


A) Use dynamic programming

B) Use a sliding window approach

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

Explanation: As you traverse the array, you need to keep track of


both the maximum and minimum products, as the minimum product
can become the maximum product if multiplied by a negative
number.

62. Square (2021)

Question: How do you implement a function to merge two sorted linked lists?
A) Recursively merge the lists

B) Use a dummy node and iteratively merge the lists

C) Sort the lists first and then merge

D) Both A and B

Answer: D) Both A and B

Explanation: Both recursive and iterative approaches are valid for


merging two sorted linked lists. The iterative approach is more
space-efficient.

63. Google (2020)

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

C) Sort the list and check for cycles

D) Use a hash set to track visited nodes

Answer: A) Use two pointers (Floyd’s Cycle Detection)

Explanation: Floyd’s Tortoise and Hare algorithm uses two pointers


to detect cycles in O(n) time and O(1) space.

64. Facebook (2019)

Question: How do you find the minimum spanning tree of a graph?


A) Use Kruskal’s algorithm

B) Use Prim’s algorithm

C) Use Dijkstra’s algorithm

D) Both A and B

Answer: D) Both A and B

Explanation: Kruskal’s and Prim’s algorithms are both commonly


used to find the minimum spanning tree of a graph.

65. Amazon (2019)

Question: How do you find the shortest path in a weighted graph?


A) Use Dijkstra’s algorithm

B) Use BFS

C) Use Floyd-Warshall algorithm

D) Use Bellman-Ford algorithm

Answer: A) Use Dijkstra’s algorithm

Explanation: Dijkstra’s algorithm is efficient for finding the shortest


path in a weighted graph with non-negative edge weights.

66. Uber (2019)

Question: How would you implement an algorithm to reverse a linked list?


A) Use a recursive approach
B) Use a stack to reverse the list

C) Use three pointers to reverse the list iteratively

D) Use a temporary array to reverse the list

Answer: C) Use three pointers to reverse the list iteratively

Explanation: Using three pointers allows you to reverse the linked


list iteratively in O(n) time and O(1) space.

67. Google (2018)

Question: How would you find the median of two sorted arrays?
A) Merge both arrays and find the median

B) Use binary search to find the median

C) Use a max-heap and a min-heap

D) Both A and B

Answer: D) Both A and B

Explanation: Both methods can be used to find the median. Merging


the arrays is simpler but less efficient, while binary search is more
optimal for large arrays.

68. Amazon (2018)

Question: How would you implement an algorithm to remove duplicates from a


sorted array?
A) Use a hash set to track duplicates

B) Use two pointers to track and remove duplicates in place

C) Sort the array and then remove duplicates

D) Traverse the array and compare each element to the next

Answer: B) Use two pointers to track and remove duplicates in place

Explanation: Using two pointers allows you to remove duplicates in


place without requiring extra space, making the solution efficient.

69. Microsoft (2019)


Question: How would you find the element that appears only once in an array,
where every other element appears twice?
A) Use a hash map to count the occurrences of elements

B) XOR all the elements together

C) Sort the array and find the unique element

D) Traverse the array and compare elements

Answer: B) XOR all the elements together

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.

70. Apple (2019)

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

B) Use dynamic programming

C) Sort the string and find the longest substring

D) Use recursion to check substrings

Answer: A) Use a sliding window approach with a hash set

Explanation: By maintaining a sliding window and a hash set to


track characters, you can efficiently find the longest substring
without repeating characters in O(n) time.

71. Facebook (2020)

Question: How would you find all unique triplets in an array that sum up to
zero?
A) Use a hash set to store triplets

B) Sort the array and use two pointers

C) Use brute force to check each triplet

D) Both A and B
Answer: D) Both A and B

Explanation: Sorting the array and using two pointers is an efficient


approach for finding unique triplets that sum to zero. A hash set can
help avoid duplicate triplets.

72. LinkedIn (2019)

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

B) Use a hash set to store prefixes

C) Compare each string character by character

D) Use recursion to find the common prefix

Answer: A) Sort the strings and compare the first and last strings

Explanation: Sorting the strings brings the most similar prefixes to


the front, allowing you to simply compare the first and last strings
for the longest common prefix.

73. Google (2020)

Question: How do you check if a string is a valid palindrome?


A) Reverse the string and compare

B) Use a two-pointer approach to check characters from both ends

C) Sort the string and check

D) Use dynamic programming

Answer: B) Use a two-pointer approach to check characters from both ends

Explanation: A two-pointer approach is an optimal way to check for


palindromes, as it compares characters from both ends
simultaneously.

74. Amazon (2017)

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

C) Brute force by checking all subarrays

D) Both A and B

Answer: D) Both A and B

Explanation: Both the sliding window technique and using a hash


map to track cumulative sums can efficiently solve this problem.

75. Uber (2018)

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

B) Use a binary search technique

C) Use a heap to track the kth smallest element

D) Both B and C

Answer: D) Both B and C

Explanation: You can use a heap or a binary search approach to find


the kth smallest element in a sorted matrix efficiently.

76. Microsoft (2021)

Question: How do you find the longest substring with at most two distinct
characters?
A) Use a sliding window approach

B) Use dynamic programming

C) Sort the string

D) Use a hash set to store distinct characters

Answer: A) Use a sliding window approach

Explanation: A sliding window approach with two pointers can be


used to track substrings with at most two distinct characters
efficiently.

77. Facebook (2020)


Question: How would you merge two sorted linked lists into a new sorted
linked list?
A) Use recursion to merge the lists

B) Use two pointers to merge the lists iteratively

C) Sort both lists and then merge

D) Use a hash map to merge the lists

Answer: B) Use two pointers to merge the lists iteratively

Explanation: By using two pointers, one for each linked list, you can
efficiently merge the lists in sorted order.

78. LinkedIn (2018)

Question: How would you find the intersection of two arrays?


A) Sort both arrays and use two pointers

B) Use a hash set to store elements of one array and check for existence in the other

C) Use binary search

D) Both A and B

Answer: D) Both A and B

Explanation: Both sorting and using a hash set are efficient methods
to find the intersection of two arrays.

79. Google (2021)

Question: How do you find the largest rectangle in a histogram?


A) Use dynamic programming

B) Use a stack to track the histogram heights

C) Sort the histogram bars

D) Use brute force

Answer: B) Use a stack to track the histogram heights

Explanation: A stack-based approach allows you to find the largest


rectangle in O(n) time.
80. Amazon (2021)

Question: How do you count the number of 1's in a binary representation of a


number?
A) Use bitwise AND operation

B) Use the built-in function to count 1's

C) Iterate over the bits and count

D) Both A and B

Answer: D) Both A and B

Explanation: Both methods (bitwise AND and built-in functions) can


efficiently count the number of 1's in a binary number.

81. Microsoft (2020)

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

B) Sort the array and return the middle element

C) Use Boyer-Moore Voting Algorithm

D) Use dynamic programming

Answer: C) Use Boyer-Moore Voting Algorithm

Explanation: Boyer-Moore Voting Algorithm allows you to find the


majority element in O(n) time and O(1) space.

82. Amazon (2018)

Question: How would you implement an algorithm to find the peak element in
an array?
A) Traverse the array and check neighbors

B) Use binary search to find the peak element

C) Sort the array and return the middle element

D) Use dynamic programming


Answer: B) Use binary search to find the peak element

Explanation: Binary search can be used to efficiently find a peak


element in O(log n) time.

83. Google (2019)

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

B) Sort the array and find the missing number

C) Use the formula for the sum of numbers from 1 to n+1 and subtract the sum of the array

D) Use a binary search approach

Answer: C) Use the formula for the sum of numbers from 1 to n+1 and subtract the sum of the
array

Explanation: The sum of numbers from 1 to n+1 is (n+1)*(n+2)/2. By


subtracting the sum of the elements of the array from this total, you
can find the missing number in O(n) time.

84. Facebook (2021)

Question: How would you rotate an array to the left by k positions?


A) Reverse the array and then reverse the first k elements

B) Use a temporary array to store rotated elements

C) Rotate the array using a two-pointer approach

D) Reverse the entire array and reverse the remaining part

Answer: B) Use a temporary array to store rotated elements

Explanation: You can store the first k elements in a temporary array


and shift the remaining elements, then concatenate the temporary
array at the end.

85. LinkedIn (2020)

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

C) Use a hash set and compare

D) Both A and B

Answer: 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.

86. Apple (2020)

Question: How would you find the longest consecutive sequence in an unsorted
array?
A) Sort the array and find the longest consecutive sequence

B) Use a hash set to store the numbers

C) Use a dynamic programming approach

D) Both A and B

Answer: 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.

87. Microsoft (2021)

Question: How would you merge two sorted arrays into a third sorted array?
A) Use two pointers to merge the arrays

B) Use a merge sort algorithm

C) Use a binary search to insert elements from the second array

D) Sort both arrays after merging them

Answer: A) Use two pointers to merge the arrays

Explanation: Two pointers can efficiently merge two sorted arrays


into a third one in O(n + m) time, where n and m are the lengths of
the arrays.
88. Google (2020)

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

B) Use Kadane’s algorithm

C) Sort the array and check subarrays

D) Use a sliding window approach

Answer: B) Use Kadane’s algorithm

Explanation: Kadane’s algorithm is an optimal solution to find the


maximum sum subarray in O(n) time.

89. Uber (2021)

Question: How do you find the first repeating element in an array?


A) Use a hash map to store the frequencies

B) Sort the array and compare adjacent elements

C) Use a set to track elements

D) Both A and C

Answer: 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.

90. Facebook (2018)

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

B) Use Boyer-Moore Voting Algorithm for n/3

C) Sort the array and check for the majority element

D) Use dynamic programming


Answer: B) Use Boyer-Moore Voting Algorithm for n/3

Explanation: The Boyer-Moore Voting Algorithm can be extended to


handle the case where an element appears more than n/3 times in
linear time.

91. Amazon (2020)

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

C) Use a sliding window approach

D) Sort the array and find the product

Answer: B) Track the minimum and maximum product as you traverse the array

Explanation: Tracking both the minimum and maximum products as


you iterate through the array helps you find the minimum product
subarray efficiently in O(n) time.

92. LinkedIn (2021)

Question: How would you find the longest subarray with at most k distinct
elements?
A) Use a sliding window approach

B) Use dynamic programming

C) Sort the array and find the subarray

D) Use a hash set to store distinct elements

Answer: A) Use a sliding window approach

Explanation: A sliding window approach allows you to track the


subarray with at most k distinct elements in O(n) time.

93. Microsoft (2020)

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

C) Brute force by checking all pairs

D) Both A and B

Answer: 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.

94. Google (2021)

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

B) Use a greedy approach

C) Use breadth-first search

D) Both A and C

Answer: D) Both A and C

Explanation: You can use dynamic programming or a breadth-first


search (BFS) approach to find the minimum moves to reach the
target in a 1D array. BFS is particularly useful when considering the
minimum steps as a graph traversal.

95. Uber (2020)

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

B) Use a two-pointer approach to move non-zero elements to the front

C) Sort the array

D) Use a sliding window approach


Answer: B) Use a two-pointer approach to move non-zero elements to the front

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.

96. Amazon (2021)

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

B) Use a sliding window approach

C) Sort the array and check for the subarray

D) Use dynamic programming

Answer: A) Use a hash map to track cumulative sums

Explanation: By tracking cumulative sums and checking for the


target sum difference in the hash map, you can find the longest
contiguous subarray in O(n) time.

97. Facebook (2019)

Question: How do you find the kth largest element in an unsorted array?
A) Sort the array and return the kth element

B) Use a heap to track the kth largest element

C) Use quickselect algorithm

D) Both B and C

Answer: D) Both B and C

Explanation: Both using a heap or the quickselect algorithm are


efficient ways to find the kth largest element, with quickselect
providing average O(n) time complexity.

98. Google (2020)

Question: How do you check if an array contains duplicate elements?


A) Sort the array and check adjacent elements
B) Use a hash set to track elements

C) Brute force by comparing all pairs

D) Both A and B

Answer: D) Both A and B

Explanation: Sorting the array or using a hash set both provide


efficient ways to check for duplicates. The hash set method runs in
O(n) time, while sorting the array requires O(n log n) time.

99. Amazon (2020)

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

C) Use dynamic programming

D) Use a hash map to track the largest products

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.

100. Microsoft (2019)

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

B) Use a brute-force approach

C) Use dynamic programming

D) Sort the array and check subarrays

Answer: A) Use a sliding window approach

Explanation: A sliding window approach allows you to efficiently


find the smallest subarray with a sum greater than or equal to the
target by adjusting the window size dynamically.
101. LinkedIn (2020)

Question: How do you find the maximum length of a subarray with sum zero?
A) Use a hash map to track cumulative sums

B) Use a sliding window approach

C) Sort the array and find subarrays with sum zero

D) Brute force by checking all subarrays

Answer: A) Use a hash map to track cumulative sums

Explanation: By using a hash map to store the cumulative sum of


elements and checking for repeated sums, you can find the longest
subarray with a sum of zero in O(n) time.

102. Apple (2019)

Question: How would you implement an algorithm to find the duplicate


number in an array of size n+1 with numbers from 1 to n?
A) Use a hash map to count the occurrences

B) Sort the array and check for duplicates

C) Use Floyd’s Tortoise and Hare cycle detection algorithm

D) Brute force by checking all pairs

Answer: C) Use Floyd’s Tortoise and Hare cycle detection algorithm

Explanation: Floyd’s cycle detection algorithm efficiently finds the


duplicate number in O(n) time and O(1) space by treating the array
as a linked list.

103. Uber (2020)

Question: How do you find the longest increasing subsequence in an array?


A) Use dynamic programming

B) Use a binary search approach with a list

C) Use a sliding window

D) Both A and B
Answer: D) Both A and B

Explanation: The dynamic programming approach and the binary


search method using a list are both efficient ways to find the longest
increasing subsequence. The binary search approach improves the
time complexity to O(n log n).

104. Google (2020)

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

B) Traverse the array and partition the elements

C) Use a two-pointer approach to rearrange the array

D) Sort the array and then rearrange

Answer: C) Use a two-pointer approach to rearrange the array

Explanation: Using a two-pointer technique, one pointer starts at the


beginning of the array and the other at the end, allowing you to
efficiently move negative numbers to the beginning while
maintaining the order of the positive numbers.

105. Amazon (2021)

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

B) Use a hash map to store and count pairs

C) Use dynamic programming

D) Brute force by checking all pairs

Answer: B) Use a hash map to store and count pairs

Explanation: Using a hash map to track the elements and their


counts allows you to efficiently find pairs whose sum equals the
target value in O(n) time.

106. Facebook (2021)


Question: How do you find the longest contiguous subarray with the sum equal
to zero?
A) Use a hash map to track cumulative sums

B) Use a sliding window approach

C) Sort the array and check for subarrays

D) Both A and B

Answer: A) Use a hash map to track cumulative sums

Explanation: By tracking cumulative sums and storing them in a


hash map, you can find the longest contiguous subarray with a sum
of zero in O(n) time.

107. Microsoft (2021)

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 elements of one array

C) Use dynamic programming

D) Both A and B

Answer: D) Both A and B

Explanation: Sorting both arrays and using two pointers is an


efficient way to find common elements, while a hash set can be used
to track the elements of one array and check against the other array.

108. LinkedIn (2021)

Question: How do you rotate an array to the right by k steps?


A) Reverse the entire array and reverse the first k elements and the remaining elements

B) Use a temporary array to store the rotated elements

C) Sort the array and then rotate

D) Brute force by shifting elements


Answer: A) Reverse the entire array and reverse the first k elements and the remaining
elements

Explanation: This is an efficient method for rotating an array,


requiring O(n) time and O(1) extra space.

You might also like