Work in HR or Recruiting?
Amazon.com
www.amazon.com Seattle, WA 5000+ Employees
Work in HR? Complete Your Profile

201 interview experiences Back to all Amazon.com Interview Questions & Reviews

Interview Question for Software Development Engineering Intern at Amazon.com:
Jun 23, 2012

You are given an array with n positive integers where all values in the array are repeated except for one. Return the one that is not repeated.


Tags: data structures, programming, algorithm
Add Tags [?]

See more for this Amazon.com Software Development Engineering Intern Interview

Helpful Question?  
Yes | No
Inappropriate?

Answers & Comments (6)

Jun 23, 2012

by Interview Candidate:

public static int notRepeated(int[] given) {
  Map<Integer> m = new HashMap<Integer>();
  for(i=0; i < given.length; i++) {
    if(m.get(given[i])) {
      m.put(given[i], 2);
    }
    else
      m.put(given[i], 1);
    for(x:m) {
      if(x == 1)
        return x;
    }
  }
}
Helpful Answer?  
Yes | No
Inappropriate?

1 of 3 people found this helpful

Jul 16, 2012

by XOR it:

If you have an array of positive integers with only ONE non repeated integer, the easiest thing is just xor them all. Whatever you return will be the answer. Perl answer below

sub findNotRepeated{
        my ($arr) = @_;
        if(@$arr==0) {
                return - 1;
        }
        my $val = 0;
        foreach(@$arr) {
                $val ^= $_;
        }
        return($val);
}

sub findMissingElement{
        my ($arr,$arr2) = @_;
        if(@$arr==0 || @$arr2 == 0 ) {
                print " arr2=" .@$arr2 . "X\n";;
                return - 1;
        }
        my $val = 0;
        foreach((@$arr , @$arr2)) {
                $val ^= $_;
        }
        return($val);
}
Helpful Answer?  
Yes | No
Inappropriate?

1 of 4 people found this helpful

Aug 2, 2012

by yogesh:

first sort the array.n then
1)for i=1 to n
     {
      if ( element [i] !=element [i+1] )
           { cout<<element[i]
               break
            }
             else
                    { i=i+2
                     }
}

I dnt have any idea about its time complexity...
Helpful Answer?  
Yes | No
Inappropriate?

Jan 4, 2013

by Caleb:

This answer is in-place with O(n) complexity. A slight improvement over the space complexity of the Hash Map answer.

public int returnNonRepeat(int[] input)
{
    if(input.length < 3) return -1;
    else
    {
        int repeat = null;
        if(input[0] == input[1]) repeat = input[0];
else if(input[0] == input[2]) return input[1];
else return input[0];
        for(int i = 2; i<input.length; i++)
        {
            if(input[i] != repeat) return input[i];
        }
    }
    return -1;
}
Helpful Answer?  
Yes | No
Inappropriate?

Feb 17, 2013

by syam:

Cant use XOR as it fails when any element repeats odd number of times.....

Can use hash map with O(n) time and O(n) space
Helpful Answer?  
Yes | No
Inappropriate?

Feb 20, 2013

by alien:

only 2 possible solutions
1.) using sorting
2.) using additional space. which will be less than o(n)
Helpful Answer?  
Yes | No
Inappropriate?

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

Amazon.com – Why Work for Us?

At Amazon, we believe that everyone is a leader—it's part of what makes us 100% Peculiar. Whether you are a Software Development Engineer, Product Manager, Fulfillment Associate, or Customer Service Representative, you… Full Overview

Provided by employer [?]

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