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.

- sandip.03934 August 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

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

- eric decker August 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

perhaps

return !!(i^j)

is a better answer?

- minglotus August 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes
{{{ return !!(i - j); }}} is a better solution I think since {{{i ^ j}}} might be any integer, while {{{!!(i-j};}} is a bool variable - minglotus August 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes
{{{ return !!(i - j); }}} is a better solution I think since {{{i ^ j}}} might be any integer, while {{{!!(i-j};}} is a bool variable - minglotus August 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes
{{{ return !!(i - j); }}} is a better solution I think since {{{i ^ j}}} might be any integer, while {{{!!(i-j};}} is a bool variable - minglotus6 August 11, 2015 | Flag
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;
		}
	}
}

- Anonymous August 03, 2015 | Flag Reply
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");

- csetariq August 03, 2015 | Flag Reply
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

- yugandharr147 August 06, 2015 | Flag Reply
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

- Rohan October 06, 2015 | Flag Reply
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");
}

- mukesh August 03, 2015 | Flag Reply
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;
}
}
}

- Hari Krishna August 03, 2015 | Flag Reply
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;
		}
	}
}

- krishna August 03, 2015 | Flag Reply
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.

- Sandip Singh August 03, 2015 | Flag Reply
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));
}

- Yang August 03, 2015 | Flag Reply
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));
}

- soneyoung August 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- divm01986 August 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous August 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- be August 03, 2015 | Flag Reply
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;
}

- yugandharr147 August 07, 2015 | Flag Reply
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);
}

- hnatsu August 07, 2015 | Flag Reply
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.

- pranav August 14, 2015 | Flag Reply
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]

- kirag0112 August 15, 2015 | Flag Reply
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];
    }

- yaronbic October 02, 2015 | Flag Reply
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;
    }
}

- jiangok2006 November 09, 2015 | Flag Reply
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

- divm01986 January 04, 2016 | Flag Reply
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;
}

- naish January 23, 2016 | Flag Reply
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");

- c June 02, 2016 | Flag Reply
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");
}

- c June 02, 2016 | Flag Reply
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;
}

- Kr.satish123 September 01, 2016 | Flag Reply
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";
}
}

- maheshvangala1997 June 27, 2017 | Flag Reply
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");
}

- mukesh August 03, 2015 | 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