Skip to content
Katabench
Try free

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 performance bar

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

C#
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.

C# coding challenges →

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