Amazon Interview Question for Software Engineer in Tests






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

XOR all the elements of array.

- insigniya March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a = {5,5,5,2,2,7,8,7,8,12,10,10,12};
		int b = a[0];
		for(int i = 1; i < a.length ; i++){
			b = b^a[i];
		}
		System.out.println("--> " + b);

	}

- Sad boy March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Should we assume every time that the array will contain only positive elements and only one of them will be repeated odd number of times?

- Apurva March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not one of the best.
Suggestions appreciated.

int i=0; sum=0; count=0;
while( a[i] == a[i+1] && (i+1) < n )
{
  sum = sum + a[i]; 
  i++; count++;
}
sum = sum + a[i]; count ++;

if( sum % 2 != 0 )
{
  cout<< a[i]; break;
}
else if ( sum % 2 ==0 && count % 2 != 0 )
{
  cout<< a[i]; break;
}
else
{
  i++; count = 0; sum = 0;
}

- Vaibhavs March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Apurva : XOR the value +ve or -ve 1 1 = 0 0 0 = 1

int arr2[] = {-2,-2,10,10,20,20,20};
int element = arr2[0];
for(int i=1;i<arr2.length;i++) {
element = element ^ arr2[i];
}
System.out.println(element);

Works fine and gives me output 20

- Anonymous March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry 1 1 = 0 , 0 0 = 0

- Anonymous March 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about XOR?

#include <stdio.h>
int
main()
{
  unsigned int a[] = {5, 5, 5, 2, 2, 7, 8, 7, 8, 12, 10, 10, 12};
  unsigned int i = 0, x = 0;

  for(i = 0; i < sizeof(a) / sizeof(int); i++) {
    x ^= a[i];
  }

  printf("%d\n", x);
}

Output:
5

- leeym March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

http : // www . testskillshome . com/?p=2276

- remo March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why does this happen? I mean what's the logic?

- Anonymous March 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does anyone know if its possible to do a hashmap type something for integers?

- hashmap concept for integers? April 17, 2011 | 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