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
Constraints
- ●
n == nums.length - ●
1 <= n <= 1000 - ●
1 <= nums[i] <= 1000
Examples
nums = [1,2,1][1,2,1,1,2,1]nums = [1,3,2,1][1,3,2,1,1,3,2,1]Asked At
3 companies| Company | Difficulty | |
|---|---|---|
| Easy | View all Google questions → | |
| Amazon | Easy | View all Amazon questions → |
| Microsoft | Easy | View all Microsoft questions → |
How to Think About It
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.
Why O(n) is optimal: you must read all n elements and write 2n elements. Any solution is at least O(n).
Python shortcut: nums + nums concatenates two lists. JavaScript: [...nums, ...nums]. These are O(n) under the hood.
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
What Trips People Up in Real Interviews
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.
Solution Code
O(n) timedef getConcatenation(nums):
n = len(nums)
ans = [0] * (2 * n)
for i in range(n):
ans[i] = nums[i]
ans[i + n] = nums[i]
return ansFrequently 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.