This is a common warm-up interview problem because it tests clean implementation of simple rules. It checks whether you can manage indices and loop conditions without off-by-one mistakes. It also ensures you handle uneven input lengths correctly. Finally, it highlights whether you build strings efficiently and cleanly.

Problem statement

You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If one string is longer than the other, append the additional letters onto the end of the merged string.

Return the merged string.

Constraints:

  • 1 ≤ word1.length, word2.length ≤ 100
  • word1, word2 consist of lowercase English letters

Examples

Input word1Input word2OutputExplanation
"abc""pqr""apbqcr"Characters are taken alternately: a, p, b, q, c, r
"ab""pqrs""apbqrs"After exhausting word1, remaining characters from word2 (“rs”) are appended
"abcd""pq""apbqcd"After exhausting word2, remaining characters from word1 (“cd”) are appended

Why repeated concatenation is inefficient

A straightforward implementation often builds the answer with repeated string concatenation inside a loop. In Python, every concatenation produces a new string and copies the old content, so the work increases as the output grows. Even though you only intend to place each character once, you end up repeatedly re-copying earlier characters.

This is the kind of detail interviewers watch for: using a construction method that avoids unnecessary repeated work while keeping the logic simple.

Optimized approach using two-pointer technique

The efficient way is to walk through both strings with two indices: i for word1 and j for word2. As long as either index is still in range, append word1[i] if it exists, then append word2[j] if it exists, advancing the corresponding indexpointer each time.

Instead of concatenating strings repeatedly, store characters in a list and join once at the end. This avoids repeated copying and guarantees each character is written exactly once. The “leftover” characters don’t require special handling: when one string ends, its index fails the bounds check and the other string continues contributing until it ends too.

This is exactly what the two-pointer pattern is for: coordinating progress across two independent sequences without missing elements or introducing extra loops.

Step-by-step algorithm

  1. Initialize two pointers i and j to 0, representing current positions in word1 and word2 respectively
  2. Create an empty list result to build the merged string efficiently
  3. While both pointers are within their respective string bounds, alternate appending characters from word1 and word2
  4. After the main loop, append any remaining characters from word1 if i hasn’t reached the end
  5. Similarly, append any remaining characters from word2 if j hasn’t reached the end
  6. Join the list into a string and return the result.
1 / 7

Code implementation

Time complexity

The time complexity of this algorithm is O(n + m) where n is the length of word1 and m is the length of word2. We traverse each character in both strings exactly once. Even though we have three while loops, each character from each input string is processed exactly one time total. The first loop processes min(n, m) characters from each string, and the remaining loops process the leftover characters from whichever string is longer. String joining at the end is also O(n + m) since we must process each character in the result list.

Space complexity

The space complexity of this algorithm is O(n + m) where n and m are the lengths of the input strings. We store the result in a list that grows to size n + m in the worst case, which occurs when we merge all characters from both strings.

Common pitfalls and interview tips

  1. Empty string handling: While the constraints guarantee both strings have at least one character, always clarify assumptions in real interviews. If empty strings were allowed, our solution handles them gracefully since the while loops would simply not execute.
  2. String concatenation in loops: A common mistake is using result += word1[i] repeatedly in Python. Due to string immutability, this creates a new string object each time, leading to O((n + m)²) time complexity. Always use a list and join once at the end for O(n + m) performance.
  3. Off-by-one errors with indices: Be careful not to increment pointers before appending characters or vice versa. The pattern should be consistent: append, then increment. Alternatively, you can use Python’s cleaner approach with slicing to append remaining characters using result.extend(word1[i:]) instead of a second loop.