Interleave Two Words Starting with the First

Quick Overview

Interleave Two Words Starting with the First. Create a string by alternately taking the next character from word1 and word2, starting with word1. After one word ends, append all remaining characters of the other in their existing order.

Interleave Two Words Starting with the First

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Create a string by alternately taking the next character from `word1` and `word2`, starting with word1. After one word ends, append all remaining characters of the other in their existing order. Return the resulting string. ```cpp class Solution { public: string mergeAlternately(string word1, string word2); }; ``` ### Examples **Example 1:** ```text word1 = "dog", word2 = "xy" Output: "dxoyg" Explanation: Take d,x,o,y, then the remaining g. ``` **Example 2:** ```text word1 = "hi", word2 = "abcd" Output: "haibcd" Explanation: Alternate h,a,i,b, then append c,d. ``` ### Constraints - Each word length is in `[1,100]`. - Both words contain lowercase English letters only. ### Clarifying Questions Preserve the relative order within each input word. Alternation always begins with word1. ```hint Inspect the structure Which word supplies the first character, and what happens after one word is exhausted? ``` ### Reasoning checkpoints - Start each pair with word1. - Append the longer word’s unmatched suffix without reordering. ### Optional extensions How would you interleave an arbitrary list of words in round-robin order?

Overview: Interleave Two Words Starting with the First. Create a string by alternately taking the next character from word1 and word2, starting with word1. After one word ends, append all remaining characters of the other in their existing order.

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

Problem

Create a string by alternately taking the next character from word1 and word2, starting with word1. After one word ends, append all remaining characters of the other in their existing order. Return the resulting string.

class Solution {
public:
    string mergeAlternately(string word1, string word2);
};

Examples

Example 1:

word1 = "dog", word2 = "xy"
Output: "dxoyg"
Explanation: Take d,x,o,y, then the remaining g.

Example 2:

word1 = "hi", word2 = "abcd"
Output: "haibcd"
Explanation: Alternate h,a,i,b, then append c,d.

Constraints

  • Each word length is in [1,100] .
  • Both words contain lowercase English letters only.

Clarifying Questions Guidance

Preserve the relative order within each input word. Alternation always begins with word1.

Reasoning checkpoints

  • Start each pair with word1.
  • Append the longer word’s unmatched suffix without reordering.

Optional extensions

How would you interleave an arbitrary list of words in round-robin order?