Amazon.com
3.3 of 5 3,092 reviews
www.amazon.com Seattle, WA 5000+ Employees

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 Flag Question

Part of a Software Development Engineer Intern Interview Review - one of 4,410 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 Flag Response

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.

Glassdoor is your free inside look at Amazon.com interview questions and advice. All interview reviews posted anonymously by Amazon.com employees and interview candidates.