Google Interview Question


Country: United States
Interview Type: In-Person




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

int add (int a, int b)
{
	int carry, sum;
	if (b == 0)
	{
		return a;
	}
	
	sum = a ^ b; // xor takes sum
	carry = a & b; // collect carry;
	carry = carry << 1;
	return ( add (sum, carry) );
}

Like the way I learnt adding numbers when I'm a kid. Additionally, no math operator is used

- code_monkey October 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good, but honestly I'd give you points off for the variable names you've chosen. Why not just call it "sum" and "carry" or something?

- Anonymous October 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oops! Dint pay much attention to them :)
Nevertheless corrected.

Thnx.

- code_monkey October 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually, I'm gonna post the easiest implementation of add and minus here:
int add (int a, int b)
{
do a^=b; while (b = (~a&b) << 1);
return a;
}
int minus (int a, int b)
{
do a^=b; while(b = (a&b) << 1);
return a;
}

See how subtle the difference is ~a or a

- Fentoyal September 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
16
of 18 vote

return(a-(-b))

- anonymous October 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice one mate :) :)

- Shyam October 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The way I've seen this asked isn't like it was asked here. Implement addition of unsigned integers without any arithmetic operators.

- Anonymous October 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

They are unsigned integers. So, as far as I can tell, there is no way to represent negative numbers.

For signed integers, the first bit tells whether the number is positive or negative. For unsigned, the first bit is part of the number and hence, only positive numbers are allowed.

So, -b can't be represented using unsigned numbers.

- Prash October 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

cool!

- ChenH1989 October 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oh ...

- nz2324nz2324 October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Funny, but nice. +1

- Murali Mohan September 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
7
of 7 vote

Well use a+b = (a^2 - b^2)/(a-b)
If a=b, just do a+b=2*a

as simple as that!

- Epic_coder October 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

As I mentioned in the other comment, negative numbers can't be represented using unsigned numbers. In this example, if a < b, then a- b will be negative and hence, can't be assigned to an unsigned integer.

- Prash October 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Proof is shown for an unsigned byte (0xFF = 255). However the math works out the same for all unsigned types.

x + y = 255 + (-255 + x + y)
x + y = 255 - (255 - x - y)
x + y = 255 - ((255 - x) - y)
x + y = 255 - ((~x) - y)
x + y = ~((~x) - y)

To implement for an unsigned int:

unsigned int Add(unsigned int x, unsigned int y)
{
    return ~((~x) - y);
}

- uclabrianh October 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

bool carry(bool ci, bool a, bool b)
{
bool car = (~ci & a & b) | (ci & ~a & b) | (ci & a & ~b) | (ci & a & b);
return car;
}

bool sum(bool ci, bool a, bool b)
{
bool s = (~ci & ~a & b) | (~ci & a & ~b) | (ci & ~a & ~b) | (ci & a & b);
return s;
}

void add(int a, int b)
{
int a1 = 1, b1 = 1,ci = 0, output = 0, totalsum = 0;
bool c2 = 0;

for(int i =0; i < 8; i++)
{
bool a0 = a & a1;
bool b0 = b & a1;
c2 = carry(ci, a0, b0);
int s= sum(ci, a0, b0);
s = s<<(i);
totalsum = totalsum | s;
ci = c2;
a1 = a1<<1;

}
cout<<totalsum<<endl;
}

int main()
{

add(13,35);
return 0;
}

- wizdolikecuteee October 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// implement 1
unsigned Add(unsigned a, unsigned b) {
	if(!a)return b;
	if(!b)return a;
	return Add(a^b,(a&b)<<1);
}
//implement 2
unsigned Add(unsigned a, unsigned b, unsigned carry) {
	if(!(a|b))return carry; //both a and b is zero. 
	if(!((a|b)&1)) return Add(a>>1,b>>1,0)<<1|carry; // the lowest bit of a and b is 0
	if(!(a&b&1)) return Add(a>>1,b>>1,carry)<<1|(!carry); //the lowest bit of a and b must be 1 one and 1 zero 
	return Add(a>>1,b>>1,1)<<1|carry;// both 1
}

- ChenH1989 October 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(A-(0xFFFFFFFF-B)-1) and (A+B) is modulus 2^32 congruences.

- Louix November 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

uint32 sum(uint32 a, uint32 b) {
         if(a==b)
             return 2*a;
         if(a>b)
              return ((a*a - b*b) / (a - b));
         return ((b*b - a*a) / (b-a));

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

A solution is to consider the add operation in two passes:
* first add bits set only in one of the number (logical or, and reverse bits set by logical and)
* remaining case is when the two bits are set to 1 (spotted by the logical and), and in such case we have to add the carry, that is, the bit position shifted - multiple carries may be present, and we need to loop until no carry is still there

Iterative version:

unsigned int sum(unsigned int a, unsigned int b) {
  for(;;) {
    const unsigned int x = a & b;
    a |= b;
    if (x == 0)
      break;
    a ^= x;
    b = x << 1;
  }
  return a;
}

- xroche February 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int add(int i, int j) {
		if (i == 0 || j == 0) {
			return i | j;
		}
		return add(i ^ j, (i & j) << 1);
	}

- A February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

u32 add_u32(u32 a, u32 b)
{
    u32 tmp;
    
    while (b) {
        tmp = (a & b) << 1;
        a ^= b;
        b = tmp;
    }
    
    return a;
}

- c0der August 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sum (int a,int b)
{
return ( a - ( (-1)*b ) );
}

bhooyaa!

- Naman Gupta August 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function add(a, b) {
    return Math.log(Math.pow(Math.E, a) * Math.pow(Math.E, b));
}

This isn't really useful in real life thought since the product grows too quickly.

- Marek May 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
	long long x = atoi(argv[1]);
	long long y = atoi(argv[2]);

	long long S=0;
	
	if(x==0 || y == 0) {
		printf("%lld * %lld = %lld\n", x, y, S);
		return 0;
	}

	long long t = x;
	while(t){
		S += y;
		t--;
	}
	printf("%lld * %lld = %lld\n", x, y, S);
	return S;
}

- SAM_THE_EXPERT May 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no + operator to be used dude!

- Psycho July 14, 2013 | Flag


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