Interview Question


Country: United States
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
1
of 1 vote

Is it just me or does this sound like someone is trying to either get someone else to do their homework or to complete a take-home technical interview.

O(nm) complexity with O(nm) memory where n is the number of words and m is average number of characters.

public static List<Boolean> getAnagrams(BufferedReader in) throws IOException{
    HashSet<int[]> signatures = new HashSet<int[]>();
    ArrayList<Boolean> results = new ArrayList<Boolean>();
    String line = null;

    while((line = in.readLine()) != null){
        int[] signature = buildSignature(line);
        if(signatures.contains(signature)){
            results.add(true);
        }
        else{
            results.add(false);
            signatures.add(signature);
        }
    }
    return results;
}

private static int[] buildSignature(String str){
    int[] sig = new int[26];
    for(int i = 0; i < str.length(); i++){
        char c = str.charAt(i);
        sig[c - 'a']++;
    }
    return sig;
}

- zortlord September 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I've never seen that signature method used for anagrams before, that is extremely clever. Much more efficient than my idea, which was to sort each word and then look that up in the hash table instead.

- Kyle September 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

could you share sample input and output?

- Geek September 02, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More