Given a list of numbers, sort it using the Merge Sort algorithm.
{
"arr": [5, 8, 3, 9, 4, 1, 7]
}
Output:
[1, 3, 4, 5, 7, 8, 9]
Constraints:
Explanation:
https://stg.interviewkickstart.com /blogs/learn/merge-sort
/*
Asymptotic complexity in terms of size of `arr` `n`:
* Time: O(n * log(n)).
* Auxiliary space: O(n).
* Total space: O(n).
*/
static ArrayList<Integer> merge_sort(ArrayList<Integer> arr) {
int n = arr.size();
int left = 0, right = n - 1;
split(arr, left, right);
return arr;
}
static void split(List<Integer> arr, int l, int h) {
// divide the array into blocks of size [1, 2, 4, 8, ..]
for (int blocks = 1; blocks <= h - l; blocks = 2 * blocks) {
// for blocks = 1, i = 0, 2, 4, 6, 8 and so on
// for each block we split the array into sub arrays and merge them
/* note that each of these subarrays will always be sorted as we are
building the array from smaller subarrays to larger subarrays*/
for (int i = l; i < h; i += 2 * blocks) {
int left = i;
int mid = Math.min(i + blocks - 1, h);
int right = Math.min(i + 2 * blocks - 1, h);
merge(arr, left, mid, right);
}
}
}
/*function to merge 2 sorted arrays*/
static void merge(List<Integer> arr, int left, int mid, int right) {
int[] l = new int[mid - left + 1];
//copies the integers to array l from arr
for(int i = 0; i < mid - left + 1; i++) {
l[i] = arr.get(left + i);
}
int[] r = new int[right - mid];
//copies the integers to array r from arr
for(int i = 0; i < right - mid; i++) {
r[i] = arr.get(mid + i + 1);
}
int i = 0, j = 0, k = left;
//merges arrays l and r back to arr
while(i < l.length && j < r.length) {
if(l[i] <= r[j]) {
arr.set(k, l[i]);
i++;
}
else {
arr.set(k, r[j]);
j++;
}
k++;
}
//merges remaining elements of arrays l and r
while(i < l.length) {
arr.set(k++, l[i++]);
}
while(j < r.length) {
arr.set(k++, r[j++]);
}
}
/*
Asymptotic complexity in terms of size of `arr` `n`:
* Time: O(n * log(n)).
* Auxiliary space: O(n).
* Total space: O(n).
*/
static ArrayList<Integer> merge_sort(ArrayList<Integer> arr) {
int n = arr.size();
int left = 0, right = n - 1;
split(arr, left, right);
return arr;
}
/*function to partition the array into subarrays and then merge them*/
static void split(List<Integer> arr, int left, int right) {
if(left < right) {
int mid = (left + right) / 2;
//sort first and second halves of the array
split(arr, left, mid);
split(arr, mid + 1, right);
//merge the sorted halves
merge(arr, left, mid, right);
}
}
/*function to merge 2 sorted arrays*/
static void merge(List<Integer> arr, int left, int mid, int right) {
int[] l = new int[mid - left + 1];
//copies the integers to array l from arr
for(int i = 0; i < mid - left + 1; i++) {
l[i] = arr.get(left + i);
}
int[] r = new int[right - mid];
//copies the integers to array r from arr
for(int i = 0; i < right - mid; i++) {
r[i] = arr.get(mid + i + 1);
}
int i = 0, j = 0, k = left;
//merges arrays l and r back to arr
while(i < l.length && j < r.length) {
if(l[i] <= r[j]) {
arr.set(k, l[i]);
i++;
}
else {
arr.set(k, r[j]);
j++;
}
k++;
}
//merges remaining elements of arrays l and r
while(i < l.length) {
arr.set(k++, l[i++]);
}
while(j < r.length) {
arr.set(k++, r[j++]);
}
}
We hope that these solutions to merge sort problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.
If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart’s FREE webinar to understand the best way to prepare.
Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.
We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:
â€To learn more, register for the FREE webinar.
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
Just drop your name and email so we can send your Power Patterns PDF straight to your inbox. No Spam!
By sharing your contact details, you agree to our privacy policy.
Time Zone: Asia/Dhaka
We’ve sent the Power Patterns PDF to your inbox — it should arrive in the next 30 seconds.
📩 Can’t find it? Check your promotions or spam folder — and mark us as safe so you don’t miss future insights.
We’re hosting a private session where FAANG insiders walk through how they actually use these Power Patterns to crack interviews — and what sets top performers apart.
🎯 If you liked the PDF, you’ll love what we’re sharing next.
Time Zone: