00:00

Problem Statement

Input/Output Examples

Visual Explanation

The result is created once with length a.length + b.length. The first copy starts at index 0, and the second starts at index a.length, so nothing is overwritten.

Approaches

Both approaches build a new array. The first copies memory blocks directly; the second joins two streams.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n and m are the lengths of the two arrays. Every element is copied once into a result array of size n + m.

Interview Tips and Recommended Approach

  • Size the result correctly. The new array must have length a.length + b.length; Java arrays cannot grow later.
  • Get the destination offset right. The second copy must start at a.length, otherwise it overwrites the first array's elements.
  • Do not confuse this with merging sorted arrays. If the interviewer wants a sorted result, use two pointers to merge in order instead of copying blocks.
  • Know the other copy tools: Arrays.copyOf(a, a.length + b.length) creates a larger copy of a that you can then fill with b.

Recommended Approach: System.arraycopy()

Why: it allocates the result once and copies each input in a single, highly optimised call. It is the standard way to combine arrays in Java and makes the index arithmetic easy to explain.