Append a Second Copy of an Array

Quick Overview

Append a Second Copy of an Array. Return the concatenation of nums with itself: if its length is n, the result has length 2n and entries i and i+n both equal the original nums[i]. Preserve the original order in each half.

Append a Second Copy of an Array

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given `nums` of length `n`, return a length-`2*n` array consisting of the entire input sequence followed by a second copy in the same order. Thus, for every original index `i`, both result positions `i` and `i+n` contain `nums[i]`. ```cpp class Solution { public: vector<int> getConcatenation(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text nums = [4,7] Output: [4,7,4,7] Explanation: Each half is the original two-entry array. ``` **Example 2:** ```text nums = [9] Output: [9,9] Explanation: The single entry appears once in each half. ``` ### Constraints - `1 <= nums.length <= 1000`. - `1 <= nums[i] <= 1000`. ### Clarifying Questions This duplicates the entire sequence rather than placing each repeated value beside itself. ```hint Inspect the structure Which two result positions correspond to one original index? ``` ### Reasoning checkpoints - Return exactly two complete copies, preserving the order and multiplicity of every input entry. - Handle the shortest input without confusing concatenation with repeating each value beside itself. ### Optional extensions How would you concatenate k copies while writing each output position once?

Overview: Append a Second Copy of an Array. Return the concatenation of nums with itself: if its length is n, the result has length 2n and entries i and i+n both equal the original nums[i]. Preserve the original order in each half.

Google logo
Google
Aug 10, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given nums of length n, return a length-2*n array consisting of the entire input sequence followed by a second copy in the same order. Thus, for every original index i, both result positions i and i+n contain nums[i].

class Solution {
public:
    vector<int> getConcatenation(vector<int>& nums);
};

Examples

Example 1:

nums = [4,7]
Output: [4,7,4,7]
Explanation: Each half is the original two-entry array.

Example 2:

nums = [9]
Output: [9,9]
Explanation: The single entry appears once in each half.

Constraints

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

Clarifying Questions Guidance

This duplicates the entire sequence rather than placing each repeated value beside itself.

Reasoning checkpoints

  • Return exactly two complete copies, preserving the order and multiplicity of every input entry.
  • Handle the shortest input without confusing concatenation with repeating each value beside itself.

Optional extensions

How would you concatenate k copies while writing each output position once?