FastPrepMerge Sorted Array
Problems/Merge Sorted Array

Merge Sorted Array

Amazon logoAmazon● EasyNEW GRADONSITE INTERVIEW

Updated Jul 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given two integer arrays nums1 and nums2, each sorted in non-decreasing order, and two integers m and n representing the number of valid elements in the arrays.

Merge the valid elements of nums1 and all elements of nums2 into nums1 in non-decreasing order.

  • nums1 has length m + n. Its first m elements are valid input values, while its final n positions are placeholders that should be ignored before the merge.
  • nums2 has length n.

Modify nums1 in place, then return the merged nums1.

Function

mergeSortedArray(nums1: int[], m: int, nums2: int[], n: int) → int[]

Examples

Example 1

nums1 = [1,2,3,0,0,0]m = 3nums2 = [2,5,6]n = 3return = [1,2,2,3,5,6]

The valid portions are [1,2,3] and [2,5,6]. Their merged order is [1,2,2,3,5,6].

Example 2

nums1 = [1]m = 1nums2 = []n = 0return = [1]

The second array is empty, so nums1 remains unchanged.

Example 3

nums1 = [0]m = 0nums2 = [1]n = 1return = [1]

nums1 has no valid input elements. Its single position is merge capacity for the value from nums2.

Constraints

  • nums1.length == m + n
  • nums2.length == n
  • 0 <= m, n <= 200
  • 1 <= m + n <= 200
  • -10^9 <= nums1[i], nums2[j] <= 10^9
  • The first m elements of nums1 and all elements of nums2 are sorted in non-decreasing order.

More Amazon Onsite Interview questions

  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dungeon GameONSITE INTERVIEW · Seen Nov 2026
  • Integer BreakONSITE INTERVIEW · Seen Nov 2026
  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Merge Sorted ArrayDP World · ONSITE INTERVIEW
  • Merge Two Arrays Into Sorted OrderNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
  • Two SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Squares of a Sorted ArrayBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Assign CookiesExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
  • Combine Two Sorted Lists Into OneDocuSign · ONSITE INTERVIEWDocuSign interview questions
  • 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
  • Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
  • Count Triplets With Sum at Most TargetGoogle · ONSITE INTERVIEWGoogle interview questions

Interview reports that mention this question

  • Amazon Frontend Engineer Interviews: 21 Experiences (Part 1 of 2)

Amazon interview experiences

  • Amazon SWE OA with AI Coding: 1 Experience
  • Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
  • Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
  • Amazon BIE Intern SQL and Interview Questions: 17 Experiences
  • Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
See Amazon hiring insights
Saved
class Solution {
    public int[] mergeSortedArray(int[] nums1, int m, int[] nums2, int n) {
      // write your code here
    }
}
Checking account…