Introduction
LeetCode 88 β Merge Sorted Array is one of the most important beginner-friendly array problems asked in coding interviews.
At first glance, the problem looks very easy because both arrays are already sorted. But the real challenge is:
How do we merge them efficiently without using extra space?
This question is commonly asked by companies because it tests:
- Array manipulation
- Two pointer technique
- In-place modification
- Edge case handling
- Space optimization
The most important learning from this problem is understanding:
Why merging from the back is the optimal strategy.
In this article, we will cover:
- Problem understanding
- Brute force approach
- Better approach
- Optimal 3-pointer solution
- Step-by-step dry run
- Time & space complexity
- Common mistakes
- Interview tips
- FAQs
By the end, you will completely understand the logic behind this problem.
Try This Problem
π https://leetcode.com/problems/merge-sorted-array/
Problem Statement
You are given two sorted arrays:
Along with two integers:
The array nums1 has size:
The last n positions are empty spaces represented by 0.
Your task is to merge nums2 into nums1 such that the final array remains sorted.
Example
Example 1
Input
Output
Understanding the Problem
Let us simplify what the question is asking.
We have:
We need:
But there is one important condition:
We must store the answer inside nums1 itself.
That means:
- No returning new array
- Modify nums1 directly
Why This Problem is Tricky
Many beginners immediately think:
This works.
But interviews usually expect a more optimized solution.
The challenge is:
Can we merge without sorting again?
Yes β using the Two Pointer technique.
Approach 1 β Brute Force Solution
Idea
- Copy all elements of
nums2into empty positions ofnums1 - Sort the final array
Java Code
Dry Run of Brute Force
Initial:
After copying:
After sorting:
Time Complexity
Copying
Sorting
Space Complexity
Drawback of Brute Force
Sorting again is unnecessary because:
- Arrays are already sorted
- We can merge smarter
Approach 2 β Extra Array Merge
Idea
Use a third temporary array.
This works exactly like merge step in Merge Sort.
Steps
- Compare elements from both arrays
- Insert smaller one into temp array
- Copy final temp array into nums1
Java Code
Time Complexity
Space Complexity
Can We Do Better?
Yes.
The interview-expected solution uses:
Optimal Approach β Three Pointers from Back
Most Important Observation
The end of nums1 already contains empty spaces.
So instead of merging from front:
We merge from the back.
This avoids overwriting important elements.
Main Idea
We use 3 pointers:
We compare:
The larger element is placed at:
Then move pointers backward.
Why Backward Merging Works
Suppose:
If we start from front:
we overwrite existing values
But from back:
empty spaces already exist
So no data loss occurs.
Optimal Java Solution
Step-by-Step Dry Run
Input
Initial Pointers
Step 1
Compare:
6 is larger.
Place 6 at end.
Move:
Step 2
Compare:
Place 5.
Step 3
Compare:
Place 3.
Step 4
Compare:
Place 2.
Done.
Time Complexity
We traverse both arrays once.
Space Complexity
No extra space used.
Why This is the Best Solution
This solution is optimal because:
β No sorting required
β No extra array required
β Single traversal
β In-place merging
β Interview preferred solution
Common Mistakes
1. Merging from Front
This overwrites elements in nums1.
2. Forgetting Edge Cases
Example:
or
3. Wrong Pointer Initialization
Correct:
4. Array Index Out of Bounds
Always check:
Interview Tips
If interviewer asks:
βWhy merge from back?β
Your answer:
Because nums1 already has empty spaces at the end. Backward traversal prevents overwriting existing sorted elements.
Frequently Asked Questions
Q1. Why not use sorting?
Because arrays are already sorted.
Sorting again wastes time.
Q2. Why start from end?
To safely place larger elements without overwriting.
Q3. Is this similar to Merge Sort?
Yes.
This is essentially the merge step of Merge Sort.
Q4. What if nums2 is empty?
Then nums1 remains unchanged.
Q5. What if nums1 has no valid elements?
Then copy all elements from nums2.
Final Takeaway
The biggest learning from this problem is:
Whenever extra space exists at the end of an array, think about backward traversal.
This pattern appears frequently in interview questions.
Conclusion
LeetCode 88 is one of the best beginner problems to master:
- Two pointers
- In-place array modification
- Efficient merging
- Space optimization
Although the brute force solution works, the optimal 3-pointer approach is the real interview solution.
Once you understand why backward merging works, this problem becomes extremely easy to solve in interviews and coding rounds.





