Work in HR? Unlock Free Profile Software Engineer Intern Interview Question

"Given two different strings, find the common characters between the two. For example if string A is "hello" and string B is "elbow" the common characters would be ['e', 'l', 'o']. Give a method that returns unique or duplicate entries."
Tags: algorithm
Add Tags [?]

Part of a Software Engineer Intern Interview Review - one of 4,652 Interview Reviews

Answers & Comments

of 2
Using python sets, create the two sets of characters, then return the intersection. This will return the set of characters that are in both strings.

from sets import Set

def duplicateChars(s1, s2):
    s1set = set()
    s2set = set()
    for c in s1:
    for c in s2:

    return s1set.intersection(s2set)

print duplicateChars("string", "ring")
- Sam on Jan 10, 2014
of 3
Solution in Java:

public class StringIntersection {
    String str1 = "hello";
    String str2 = "elbow";

    public Set findIntersection() {
        String[] arr1 = str1.split("");
        String[] arr2 = str2.split("");

        List<String> list = Arrays.asList(arr1);

        Set<String> set = new LinkedHashSet<String>(Arrays.asList(arr1));


        return set; // returns [e, l, o]
- Chase on Jan 11, 2014

To comment on this question, Sign In with Facebook or Sign Up

Tags are like keywords that help categorize interview questions that have something in common.