Magicsheet logo

Concatenation of Array

Easy
100%
Updated 6/1/2025

Concatenation of Array

What is this problem about?

The Concatenation of Array interview question is a straightforward array manipulation problem. Given an integer array nums of length n, you need to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n. Essentially, you are asked to duplicate the array and append the copy to the end of the original.

Why is this asked in interviews?

This is an "Easy" difficulty Array interview pattern often used as a warm-up or for entry-level positions by companies like Microsoft and Yahoo. It tests your ability to handle basic array indexing, memory allocation, and loops. While simple, it evaluates if a candidate can write clean, bug-free code quickly and whether they understand how to use language-specific shortcuts (like nums + nums in Python or System.arraycopy in Java).

Algorithmic pattern used

The problem uses a simple Simulation pattern.

  1. Initialize a new array of size 2 * n.
  2. Use a single loop from 0 to n-1.
  3. Assign ans[i] and ans[i + n] to nums[i]. This results in a linear time complexity O(N) and linear space complexity O(N) for the output array.

Example explanation

Input: nums = [1, 2, 1]

  1. Length n = 3. Create ans of length 6.
  2. i = 0: ans[0] = 1, ans[3] = 1.
  3. i = 1: ans[1] = 2, ans[4] = 2.
  4. i = 2: ans[2] = 1, ans[5] = 1. Result: [1, 2, 1, 1, 2, 1].

Common mistakes candidates make

  • Off-by-one errors: Incorrectly calculating the length of the new array or the offset for the second half.
  • In-place modification: Attempting to modify the original array (if it's not large enough), which is usually not what the problem asks for.
  • Over-complicating: Using complex library functions or multiple loops when a single, simple loop is sufficient.

Interview preparation tip

Even for easy problems, always mention the time and space complexity. It shows you have a consistent "engineering mindset" regardless of the problem's difficulty.

Similar Questions