Amazon.com

  www.amazon.com
Work in HR? Unlock Free Profile

Amazon.com Software Development Engineer Intern Interview Question

"You have two files containing customer IDs. One contains IDs for all unique customers that visited the website on Monday, and the other contains IDs for all the unique customers that visited the site on Tuesday. Return an array containing the IDs of customers that visited the site either on Monday or Tuesday, but did not visit on both days."
Add Tags [?]
Answer

Part of a Software Development Engineer Intern Interview Review - one of 4,640 Amazon.com Interview Reviews

Answers & Comments

3
of 3
votes
Here is an O(n+m) solution.

public class UniqueCustomers {
    public static int[] uniqueCustomers(int[] monday, int[] tuesday) {
        HashSet<Integer> ids = new HashSet<Integer>();

        int max = monday.length > tuesday.length ? monday.length : tuesday.length;

        for(int i = 0; i < max; i++) {
            if(monday.length > i) {
                if(!ids.add(monday[i])) {
                    ids.remove(monday[i]);
                }
            }

            if(tuesday.length > i) {
                if(!ids.add(tuesday[i])) {
                    ids.remove(tuesday[i]);
                }
            }
        }

        Iterator<Integer> iter = ids.iterator();

        int[] uniqueIDs = new int[ids.size()];
        int j = 0;

        while(iter.hasNext()) {
            uniqueIDs[j] = iter.next();
            j++;
        }
        return uniqueIDs;
    }
}
- Interview Candidate on Mar 12, 2013

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.