Concatenation of Array
Build by Index / Modulo
Asked at 7 companies
6 recent
- Amazon
- Bloomberg
- Infosys
- Microsoft
- Meta
- GE Healthcare
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Counting subarrays with a map of totals
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Caches and duplicate detection: A hash map answers “have I seen this before?” in one step. It's how apps cache results, and how a photo app spots the same picture uploaded twice.
ProblemProblem statementGiven an integer array nums of length n, return an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n. In plain words: write the array twice, back to back.ReadHide
Problem Statement
Given an integer array nums of length n, return an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n. In plain words: write the array twice, back to back.
Added 2026-10-30 from Srujan's LeetCode solved list (photo) — not previously in the repo.
Input / Output
Input: nums = [1,2,1]
Output: [1,2,1,1,2,1]
Input: nums = [1,3,2,1]
Output: [1,3,2,1,1,3,2,1]
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Build by Index — the second half is a copy of the first, shifted by n. Every output position maps to exactly one input position: ans[i] = nums[i % n].
Approach
Brute Force — Modulo Indexing, O(n)
for i in 0..2n-1:
ans[i] = nums[i % n] # wrap around to the start for the second half
Optimized — Fill Both Halves in One Pass, O(n)
for i in 0..n-1:
ans[i] = nums[i] # first copy
ans[i + n] = nums[i] # second copy, same step
Same O(n) time, but one loop instead of two passes / modulo on every index.
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Modulo indexing | O(n) | O(n) | Output array is required |
| Fill both halves | O(n) | O(n) | One pass, no modulo |
Solution code
from typing import List
class SolutionBruteForce:
"""O(n) time | O(n) space — fill ans[i] = nums[i % n] for 2n positions."""
def getConcatenation(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [0] * (2 * n)
for i in range(2 * n):
ans[i] = nums[i % n]
return ans
class Solution:
"""O(n) time | O(n) space — one pass fills both halves."""
def getConcatenation(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [0] * (2 * n)
for i in range(n):
ans[i] = nums[i]
ans[i + n] = nums[i]
return ansOther ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language