JPMorgan Data Engineer: Interview Question (Python)

Check out this important interview question asked in JPMorgan Data Engineer interviews.

Mentor

Blog

Two strings are said to be complete if, upon concatenation, they contain all 26 English alphabets. For example, "abcdefghi" and "jklmnopqrstuvwxyz" are complete as they together have all characters from 'a' to 'z'.

You are given two sets of strings of sizes n and m respectively, and you need to find the number of pairs of strings, where one string is from the first set and the other is from the second set, such that the concatenation of the two strings is a complete string containing all 26 alphabets.

Solution:

To solve this problem, we can follow these steps:

  1. Create a function that checks if a string contains all letters of the English alphabet.
    1. For each string in the first set, create a bitmask representing which letters are present.
      1. Do the same for each string in the second set.
        1. Count the number of pairs where the OR operation of their bitmasks results in a bitmask with all bits set (indicating all letters are present).
          1. Return the total count of such pairs.

            Here's the Python code to implement this solution:

            def is_complete(s1, s2):

            # Function to check if the concatenation of two strings contains all English alphabets

            return set(s1 + s2) == set("abcdefghijklmnopqrstuvwxyz")

            def count_complete_pairs(set1, set2):

            # Function to count the number of complete pairs

            count = 0

            for s1 in set1:

            for s2 in set2:

            if is_complete(s1, s2):

            count += 1

            return count

            # Example usage:

            set1 = ["abcdefghi", "hello", "world", "python"]

            set2 = ["jklmnopqrstuvwxyz", "example", "qwerty", "asdfg"]

            count = count_complete_pairs(set1, set2)

            print(count) # Output will depend on the input sets

            This code will output the number of pairs from set1 and set2 that form a complete string.

            Hope this helps you in your preparation. 

            Got questions?

            Schedule a trial call with me; let's discuss. :)