Merge Strings Alternately

EasyTwo Pointerstwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given two strings word1 and word2. Build a new string by taking characters alternately, starting with the first character of word1, then the first of word2, then the second of word1, and so on. When one string runs out, append all remaining characters of the other string unchanged. Return the merged string.

Examples

Input: word1 = "abc", word2 = "pqr"
Output: "apbqcr"
a, p, b, q, c, r.
Input: word1 = "ab", word2 = "pqrs"
Output: "apbqrs"
After a, p, b, q the first word is used up, so "rs" is appended.

Constraints

  • 1 <= word1.length, word2.length <= 100
  • `word1` and `word2` consist of lowercase English letters.
  • The output length is `word1.length + word2.length`.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems