Merge Sorted Arrays
Easy About 10 min Free, no account needed
Merge two sorted arrays into one - without destroying what makes them special.
The brief
Given two integer arrays a and b, each sorted ascending, return a single
sorted array containing all elements of both (duplicates included).
The obvious approach repeatedly takes the smaller front element, but removing the front of a list shifts every remaining element, so the merge degrades to O(n2). The hidden test merges 200,000 elements, so any per-element shifting is too slow. You will need a merge that copies each element without shifting the rest.
Starter code
using System;
using System.Collections.Generic;
public class Solution
{
// TODO: return all elements of two sorted arrays, merged into one sorted array.
public int[] MergeSorted(int[] a, int[] b)
{
throw new NotImplementedException();
}
}How it is graded
Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.
Related guides
What it practices
- Two pointers
- Walk a sorted or symmetric sequence from both ends, or fast and slow, instead of checking every pair.
Related katas
- Happy Number Easy Free Does repeatedly summing squared digits reach 1 - and how do you KNOW it will not?
- Move Zeroes Easy Free Push all zeros to the end while keeping the other values in order.
- Remove Duplicates From Sorted Array Easy Free Deduplicate a sorted array without forgetting that it is sorted.
- Two Sum II (Sorted Input) Easy Free Two Sum again - but the array is sorted, and that changes everything.