Concatenation of Array
Asked at Google, Amazon, Microsoft
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.
Asked At
| 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.
Visual walkthrough for nums=[1,2,1]:
n=3. ans = [0,0,0,0,0,0]
ans[0]=1, ans[1]=2, ans[2]=1 (copy first half)
ans[3]=1, ans[4]=2, ans[5]=1 (copy second half)
Result: [1,2,1,1,2,1]
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.
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
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 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 is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.
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.