EASY
Two PointersString
Updated Sep 2026

Merge Strings Alternately

Asked at Meta

Problem

Given two strings word1 and word2, merge them by alternating characters starting with word1. If one string is longer than the other, append the remaining characters at the end. This problem tests basic string manipulation and two-pointer traversal.

Asked At

CompanyDifficulty
MetaEASYView all Meta questions →

How to Think About It

1.

Brute force: iterate through both strings simultaneously, appending one character from each at a time.

2.

Use two pointers, one for each string, advancing them alternately.

3.

Handle the case where one string is shorter by appending the suffix of the longer string.

4.

Build the result using a list or StringBuilder for O(n) concatenation instead of repeated string concatenation.

5.

Time O(m+n) where m and n are the lengths of the two strings, space O(m+n) for the result.

Optimal Approach

Initialize two pointers i and j at the start of each string. While both pointers are within bounds, append word1[i] then word2[j] to the result and increment both. After the loop, append the remaining characters from whichever string has leftover characters. Using a StringBuilder ensures O(m+n) time. Space is O(m+n) for the output string.

What Trips People Up in Real Interviews

1.

Clarify whether the merge starts with word1 or word2 (always word1).

2.

Ask about empty strings as input (return the other string).

3.

Mention using StringBuilder or list for efficiency instead of string concatenation.

4.

Edge cases: equal length strings, one empty string, single character strings.

5.

Walk through an example like "abc" + "pqr" producing "apbqcr".

Solution Code

def mergeAlternately(word1, word2):
    result = []
    i = j = 0
    while i < len(word1) and j < len(word2):
        result.append(word1[i])
        result.append(word2[j])
        i += 1
        j += 1
    result.append(word1[i:])
    result.append(word2[j:])
    return ''.join(result)

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Merge Strings Alternately problem?

Given two strings word1 and word2, merge them by alternating characters starting with word1. If one string is longer than the other, append the remaining characters at the end. This problem tests basic string manipulation and two-pointer traversal.

How do you solve Merge Strings Alternately?

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 Merge Strings Alternately?

Merge Strings Alternately is asked at Meta. It is a easy difficulty problem.

What are common mistakes on Merge Strings Alternately?
  • Clarify whether the merge starts with word1 or word2 (always word1).
  • Ask about empty strings as input (return the other string).
  • Mention using StringBuilder or list for efficiency instead of string concatenation.
  • Edge cases: equal length strings, one empty string, single character strings.
  • Walk through an example like "abc" + "pqr" producing "apbqcr".