## Google Interview Question for Software Developers

Country: United States

Comment hidden because of low score. Click to expand.
16
of 18 vote

``````int compare (int i, int j) {
return i ^ j;
}``````

In case the return is 0 then the numbers are equal else not equal.

Comment hidden because of low score. Click to expand.
2

Why not just return (i-j) in that case? It seems like this logic is more generic since some languages do not have XOR.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

perhaps

``return !!(i^j)``

Comment hidden because of low score. Click to expand.
0
{{{ return !!(i - j); }}} is a better solution I think since {{{i ^ j}}} might be any integer, while {{{!!(i-j};}} is a bool variable
Comment hidden because of low score. Click to expand.
0
{{{ return !!(i - j); }}} is a better solution I think since {{{i ^ j}}} might be any integer, while {{{!!(i-j};}} is a bool variable
Comment hidden because of low score. Click to expand.
0
{{{ return !!(i - j); }}} is a better solution I think since {{{i ^ j}}} might be any integer, while {{{!!(i-j};}} is a bool variable
Comment hidden because of low score. Click to expand.
5
of 5 vote

``````public class EqualityCheck {

private static boolean result[] = { true };

public static boolean isEqual(int num1, int num2) {

int out = num1 ^ num2;

try {
return result[out];
} catch (Exception e) {
return false;
}
}
}``````

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

Works for languages like C

``````if(!(a-b))
printf("Equal\n");
else
printf("Not Equal\n");``````

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

in the java

``````public boolean isEqual(int a,int b)
{
switch(a-b){
case 0:return true;
default: return false;
}
}

//in case of javascript
function isEqual(var a,var b){
if(a-b){
return true;
}
return false;
}``````

in case of JavaScript

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

``````def compare(a,b):
result=[true]
try:
return result[a-b]
except Exception:
return False``````

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

void fun()
{
int i=2;
if (!(i-i))
printf("number are equal\n");
else
printf("not equal\n");
}

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

public class EqualityCheck {

private static boolean result[] = { true };

public static boolean isEqual(int num1, int num2) {

int out = num1 ^ num2;

try {
return result[out];
} catch (Exception e) {
return false;
}
}
}

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

``````public class EqualityCheck {

private static boolean result[] = { true };

public static boolean isEqual(int num1, int num2) {

int out = num1 ^ num2;

try {
return result[out];
} catch (Exception e) {
return false;
}
}
}``````

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

``````public int isEqual(int i, int j) {
return i ^ j;
}``````

if the integer returned is zero the numbers are equals else not equal.

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

``````bool isEqual(int a, int b) {
return 1^(((a-b)>>31)|((b-a)>>31));
}``````

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

``````int isEqual(int a, int b) {
return 1^(((a-b)>>31)|((b-a)>>31));
}``````

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

public int isEqual(int a, int b)
{
return (a^b);
}

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

return ((a-b)>>31) + ((b-a)>>31)

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

return ((a-b)>>31) + ((b-a) >> 31)

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

``````public boolean isEqual(int a,int b){
if((a-b) >0 && (a-b)<0){
return true;
}
return false;
}``````

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

c#

``````public bool Equal(int a, int b)
{
return !Convert.ToBoolean(a - b);
}``````

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

we can take avg of the integers and subtract the avg with both the integers. if the both results are zero then they are equal else not equal.

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

``````def compare(x, y):
compared = cmp(x, y)
switch = {0 : "{} equals {}".format(x, y),
-1 : "{} is less than {}".format(x, y),
1 : "{} is greater than {}".format(x, y)}

return switch[compared]``````

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

``````public static boolean equal(int a, int b) {
boolean[] results = {true, false};
int compare = (((a-b)%2) + 2) % 2;
return results[compare];
}``````

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

Just think loud.

``````boolean isEqual(int a, int b) {
try {
double a = 1/(a-b);
return false;
}  catch (InvalidOperationException) {
return true;
}
}``````

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

``````public int isEqual(int a, int b)
{
return (a^b);
}
//If they are the same it will return 0 otherwise some other number``````

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

``````#include <iostream>

using namespace std;

int main()
{
int a,b;
char exit = 'n';

while(  exit=='n')
{

cout << "Enter First Integer number : ";
cin >> a;
cout << "Enter second integer number :";
cin >> b;

cout<<endl;

if(!(a^b))
{
cout << "Number is equal " <<  endl;
}
else
{
cout << "Number is NOT equal " <<  endl;
}

cout << "Do you want to exit ? Y/N" << endl;
cin >>exit;
}

return 0;
}``````

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

if((x^y)==0)
{
printf("Equal\n");
}
else
printf("Not Equal\n");

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

{
if((x^y)==0)
printf("Equal\n");
else
printf("Not Equal\n");
}

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

// true == 1
// false ==0

#include <bits/stdc++.h>
using namespace std;
bool foo(int a , int b)
{
return a^b;
}
int main()
{
int a=0,b=10;
cout<<foo(a,b);
return 0;
}

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

#include<iostream>
using namespace std;
main()
{
int a,b,c;
cin>>a>>b;
c=a-b;
if(c==0)
{
cout<<"integers are equal";
}
else
{
cout<<"integers are unequal";
}
}

Comment hidden because of low score. Click to expand.
-1
of 1 vote

void fun()
{
int i=2;
if (!(i-i))
printf("number are equal\n");
else
printf("not equal\n");
}

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.

### 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.