Amazon Interview Question for Computer Scientists


Country: United States
Interview Type: Phone Interview




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

c#.

static private bool AllAreEqualToNum( int a, int b, int c, int d, int num ) {
    return ( (num ^ a) == 0 && (num ^ b) == 0 && (num ^ c) == 0 && (num ^ d) == 0 );
}

- zr.roman January 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

but it's nothing special, it is fully equivalent of "return (num == a && num == b && num == c && num == d)", quite straightforward.

- zr.roman January 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Avoiding 'if' statements for logical operators may not yield performance gain on higher level languages. Also, there branch prediction prevents pipeline stalls for jmp calls. Using OR operator is functionally and philosophically correct, not performance wise.

- Noobie January 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

c#.
general solution for any number of given variables.
Without if statements.

static bool AreAllEqual( int num, List<int> variables ) {
    try{
      int[] arr = new int[1];
      variables.Add( num );
      for( int i = variables.Count - 1; i > 0; i-- ) {
          arr[ variables[ i ] ^ variables[ i - 1 ] ] = 0;
      }
      return true;
  } catch( IndexOutOfRangeException ){
      return false;
  }
}

- zr.roman January 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

boolean allEquals(int number, int a, int b, int c, int d) {
        return (number^a^b^c^d)==number;
    }

- Anonymous January 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

quite strange task, what a trick here?
we can just "return (num == a && num == b && num == c && num == d)" without any if statements.

- zr.roman January 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return (a ^ b) == 0 && (c ^ d) == 0 && (a ^ d) == 0 && (a ^ num) == 0;

- zr.roman January 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{ (num ^ a) | (num ^ b) | (num ^ c) | (num ^ d) }

- anon January 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

What if
a = 1
b = 2
c = 4
d = 8
num = 0

- RoBa January 11, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In single comparison
return (5==A/B+B/C+C/D+D/E +E/200);

- V0734 January 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

e is num here.
fails on: a = 7, b = 1, c = -1, d = 1, num = -1.

- zr.roman January 11, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

(Java) function that checks if 4 numbers are equal

public static boolean numbersEqual(int a, int b, int c, int d) {
		return (a ^ b ^ c ^ d) == 0;
	}

- andrea January 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int ABCD(int *pta, int *ptb, int *ptc, int *ptd)
{
	return *pta == 200 && *ptb == 200 && *ptc == 200 && *ptd == 200;
}

- AlgarveBeachBum January 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi,
the above will work always.
a = b = c =4; d = 5;
a&b&c&d gives 4 only.

when x -> given num and a,b,c,d ->nums to be compared
printf... !((a^x) || (a^b) || (b^c) || (c^d));

- aditya January 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return ((A & B & C & D) == 200)

- abc January 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean compareFourToNum(int a,int b, int c , int d, int num){
        return ((num && a) && (num &&b) && (num &&c) && (num &&d)) == num;
        }

- Bahram January 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean compareFourToNum(int a,int b, int c , int d, int num){
        return ((num && a) && (num &&b) && (num &&c) && (num &&d)) == num;
        }

- Anonymous January 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return ((a|b|c|d == 200) && (a&b&c&d == 200))

any thoughts?

- Rafik January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return ((a&b&c&d == 200) && (a|b|c|d == 200))

- Rafik January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

that should work?

int checkEqual(int x,int a,int b,int c,int d)
{
 return !((x-a)||(x-b)||(x-c)||(x-d));
}

- Anonymous January 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int checkEqual(int x,int a,int b,int c,int d)
{
return !((x-a)||(x-b)||(x-c)||(x-d));
}

- mka January 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int checkEqual(int x,int a,int b,int c,int d)
{
 return !((x-a)||(x-b)||(x-c)||(x-d));
}

- mka January 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int checkEqual(int x,int a,int b,int c,int d)
{
 return !((x-a)||(x-b)||(x-c)||(x-d));
}

- mka January 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int checkEqual(int x,int a,int b,int c,int d)
{
 return !((x-a)||(x-b)||(x-c)||(x-d));
}

- mka January 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ans=(a==n?(b==n?(c==n?(d==n?"true":"false"):"false"):"false"):"false");
return ans;

- Anonymous February 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

printf ("%s",((a^n)&&(c^n)&&(b^n)&&(d^n)==0?"False":"True"));

- Aditya February 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int andVal= a&b&c&d;
int xorVal=input^a^b^c^d;
return (andVal==input && xorVal==input);

- Gap February 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ CheckEqual(const in& Num1, const int& Num2, const int& Num3, const int& Num4, const int& var){ return !( (Num1^Num2) | (Num3^Num4) | (Num1^ Num3) | (var^Num2)); } - Anonymous April 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since there are four variables to compare you can add all four variables a+b+c+d. If each has value 200, then the end result should be 800.
Do a left shift operation on the number 200 by 2 times to multiply the number by 4.
e.g 200 << 2
This is the most efficient way to multiply a number in a multiple of 2^x in just one cpu instruction. where x is the number of left shifts.

This will make the number 200 as 800.
You will need atleast one comparision. It does not matter whether the comparision is in a return statement or it is if/switch-case statement. I dont think there is any other way to compare.

#define TRUE 1
#define FALSE 0

int checkNumber(int a, int b, int c, int d)
{
    if((a+b+c+d) == (200<<2)){
         return TRUE;
    }
    else{
         return FALSE;
    }
}

- richierich October 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

return (a+b+c+d) == (4 * num);

- zr.roman January 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

num = 2, a = 1, b = 3, c =, d = 2 -> a + b + c + d = 4 * num yet a == num && b == num && c == num & d == num is false

- Anonymous January 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#!/usr/bin/env python3

    def solution(num, a, b, c, d):
        return a + b + c + d == 4*num

    # Your solution fail on this input
    assert not solution(4, 1, 4, 4, 7)

- JP Ventura January 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

you are right, I'm wrong.

- zr.roman January 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

hahahaha

- gen-x-s January 10, 2016 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

i think this would be okay..
suppose all are long integer type ...
then.....
return !(sqrt(sqrt(A*B)*sqrt(C*D))^200)

- Lumberdaar January 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

guessing since doing

return (num == a && num == b && num == c && num == d)

would require 4 comparisons that doing a single bitwise operation like

(a & b & c & d) == num

would be faster?

- thrive January 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"(a & b & c & d) == num" fails on 1,0,0,0, num = 0.

- zr.roman January 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#!/usr/bin/env python3

from functools import reduce


def bitwise_and(a, b):
    return a & b


def solution(number, sequence):
    return reduce(bitwise_and, sequence) == number

assert solution(4, [4, 4, 4, 4])
assert not solution(4, [1, 4, 4, 7])
assert not solution(4, [1, 2, 3, 4])
assert not solution(4, [-10, 0, 4, 10])

- JP Ventura January 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what is logic of reduce method?

- zr.roman January 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

fails on input (0, [1, 0, 0, 0])

- zr.roman January 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

return (a | b | c | d) == num;

- joseph January 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

What if
a = 1
b = 2
c = 4
d = 8
num = 15

- RoBa January 11, 2016 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

(Java) function that checks if 4 numbers are equal

public static boolean numbersEqual(int a, int b, int c, int d) {
		return (a ^ b ^ c ^ d) == 0;
	}

- andrea January 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

You can do it without any comparison.
return !(num - (a&b&c&d))

- James Zheng January 15, 2016 | 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