Open In App

Merge Sort Interview Questions and Answers

Last Updated : 12 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Merge sort is defined as a sorting algorithm that works by dividing an array into smaller subarrays, sorting each subarray, and then merging the sorted subarrays back together to form the final sorted array.

Merge Sort Algorithm

Let us discuss some of the interview questions asked about Merge Sort:

1. Is Merge sort In Place?

No, Merge sort is not an in place algorithm. During the “merge” step of the algorithm, we use an additional array to merge the subarrays in sorted and then the content of the merged array is copied to the actual array.

2. Is Merge sort Stable?

Yes, merge sort is stable. While using the merge sort algorithm, the relational ordering of the elements with same value remains unchanged.

3. How can we make Merge sort more efficient?

Merge sort can be made more efficient by replacing recursive calls for smaller subarrays with Insertion sort, where the size of the remaining array is less or equal to 43 as the number of operations required to sort an array of max size 43 will be less in Insertion sort as compared to the number of operations required in Merge sort.

4. Analysis of Merge Sort

A merge sort consists of several passes over the input. The first pass merges segments of size 1, the second merges segments of size 2, and the ith pass merges segments of size 2i-1. Thus, the total number of passes is [log2n]. As merge showed, we can merge two sorted segments in linear time, which means that each pass takes O(n) time. Since there are [log2n] passes, the total computing time is O(n log2n).

5. What types of Datasets work best for Merge Sort?

Merge sort works well on any type of dataset, be it large or small. But Quicksort generally is more efficient for small datasets or on datasets where the elements are more or less evenly distributed over the range.

6. How does the Divide and Conquer Strategy work with Merge Sort?

The Divide and Conquer strategy divides the problem into smaller parts, solves them, and combines the small solved subproblems to get the final solution. 

The same happens with the Merge Sort algorithm. It keeps on dividing the array into two halves until their lengths become 1. Then it starts combining them two at a time. First, the unit cells are combined into sorted arrays of length 2 and these sorted subarrays are combined into another bigger sorted subarrays and so on until the whole sorted array is formed.

7. Is merge sort adaptive or not?

Merge Sort is not adaptive to the existing ordering among the elements. Thus, it has the same computational complexity in all the best, the worst and the average cases.

8. Comparison between Merge Sort and other sorting algorithms

Merge sort is comparatively more efficient than most of the other sorting algorithms in most cases. It has a logarithmic time complexity in every situation. It is also a stable sorting algorithm. However, there is a requirement of extra space for merge sort that can be considered as a drawback. Because of these in some cases, Quick Sort proves to be a better choice. In some special cases, Bucket Sort and Radix Sort are optimal choices because of their less runtime complexity.

9. Can Merge Sort be used on Linked List?

Though merge sort can be used on queue, there is an issue that needs to be tackled to do so. In the linked list, the nodes are not stored in contiguous memory locations, so they cannot be accessed using indices as in arrays. To enable random access to any element, we have to initially map indices with pointers.

10. When does the worst case occur in Merge Sort?

The worst case of Merge Sort will occur when the number of comparisons is maximum. In Merge Sort, the number of comparisons between two subarrays is maximum when the subarrays contain alternate elements of the sorted subarray formed by combining them. For example, comparing {1, 3} and {2, 4} will have the maximum number of comparisons as it has the alternate elements of the sorted subarray {1, 2, 3, 4}. See the below image to find the arrangement corresponding to a sorted array which will give the worst case.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads