Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Merge Strings Alternately

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Merge Strings Alternately. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Infineon Technologies test utilities receive two diagnostic strings that must be merged character by character. Given strings first and second, create a merged string by taking one character from first, then one from second, repeating until both strings are exhausted. When one string ends, append the remaining characters from the other string in their original order.

Then build a hash map for the merged string. Each distinct character must map to a list of its zero-based positions in the merged string, ordered from left to right.

Return both the merged string and the character-position hash map.

Formal Specification

  • Input: two strings, first and second, containing printable ASCII characters.
  • Output: a two-element result where the first element is the merged string and the second element is a dictionary mapping each character to a list of integer positions.
  • Dictionary keys must include every distinct character in the merged string.

Constraints

  • 0 <= len(first), len(second) <= 10^5
  • len(first) + len(second) <= 2 * 10^5
  • Characters are printable ASCII characters
  • Position lists must be zero-based and ordered

Function Signature

def merge_and_index(first, second):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output