EasyArraySimulationUpdated Sep 2026

1929. Concatenation of Array

Asked at Google, Amazon, Microsoft

Difficulty

Easy

Time

O(n)

Space

O(n)

Pattern

Array

Companies

3

Problem

You are given an integer array nums with length n. Build a new array ans that has length 2n.

The rule is simple: ans[i] equals nums[i] for the first half, and ans[i + n] equals nums[i] for the second half. In other words, ans is just nums written twice, one copy right after the other.

Return the new array ans. The original array nums does not change.

Write nums twice

nums121012The input has n = 3 elements.ansfirst copysecond copy121121012345The output has 2n = 6 elements: the same three values,then the same three values again.
The answer is the input array copied into the first half and repeated in the second half.

Constraints

  • ●n == nums.length
  • ●1 <= n <= 1000
  • ●1 <= nums[i] <= 1000

Examples

Example 1
Inputnums = [1,2,1]
Output[1,2,1,1,2,1]
WhyThe first three slots get nums[0], nums[1], nums[2] = 1,2,1. The next three slots get the same values again, so the result is 1,2,1 followed by 1,2,1.
Example 2
Inputnums = [1,3,2,1]
Output[1,3,2,1,1,3,2,1]
WhyWe copy the whole array 1,3,2,1 into the first half, then copy it a second time into the second half. The final array has length 8.

Asked At

3 companies

How to Think About It

1

Direct approach: create a new array of size 2n. Copy nums into the first half and the second half. O(n) time, O(n) space.

2

Why O(n) is optimal: you must read all n elements and write 2n elements. Any solution is at least O(n).

3

Python shortcut: nums + nums concatenates two lists. JavaScript: [...nums, ...nums]. These are O(n) under the hood.

4

Edge cases: empty array (return empty), single element [x] (return [x,x]), already length 1.

Optimal Approach

Step 1: Get n = len(nums).
Step 2: Create ans array of size 2n.
Step 3: For i from 0 to n-1:
ans[i] = nums[i]
ans[i + n] = nums[i]
Step 4: Return ans.

This is the most explicit approach. In practice, you would use language built-ins (nums + nums in Python).

Time: O(n). Space: O(n) for the result array.

One loop fills both halves

ans000000012345Start with an empty result array of size 2n, filledwith placeholders.ans121121012345ii+nWhen i = 0 we set ans[0] = 1 and ans[3] = 1, the samevalue in two spots.ans[i] = nums[i]ans[i + n] = nums[i]loop i from 0 to n - 1
For each index i we write nums[i] into two places: position i and position i + n.
ComplexityTime O(n)Space O(n)We read each of the n input values once and write 2n output values, and the result array itself takes 2n slots.

What Trips People Up in Real Interviews

1

Returning the original array modified in place. The problem asks for a new concatenated array, not a mutation of the input.

2

Using O(n²) string concatenation in a loop. In languages like Java, use StringBuilder or direct array copy, not repeated string concat.

3

Forgetting that the result is size 2n, not n. You are concatenating the array with itself.

4

Off-by-one in the loop. Loop from 0 to n-1 (inclusive), and write to positions i and i+n.

5

Not handling the empty array edge case. If nums is empty, return an empty array.

Solution Code

O(n) time
def getConcatenation(nums):
    n = len(nums)
    ans = [0] * (2 * n)
    for i in range(n):
        ans[i] = nums[i]
        ans[i + n] = nums[i]
    return ans

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Concatenation of Array problem?

Given an integer array `nums`, return an array `ans` such that `ans` is the concatenation of `nums` with itself. This is a straightforward simulation that tests basic array operations.

How do you solve Concatenation of Array?

The optimal approach runs in O(n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Concatenation of Array?

Concatenation of Array is asked at Google, Amazon, Microsoft. It is a easy difficulty problem.

What are common mistakes on Concatenation of Array?
  • Returning the original array modified in place. The problem asks for a new concatenated array, not a mutation of the input.
  • Using `O(n²)` string concatenation in a loop. In languages like Java, use `StringBuilder` or direct array copy, not repeated string concat.
  • Forgetting that the result is size 2n, not n. You are concatenating the array with itself.
  • Off-by-one in the loop. Loop from 0 to n-1 (inclusive), and write to positions i and i+n.
  • Not handling the empty array edge case. If `nums` is empty, return an empty array.