Google Interview Question


Country: United States




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

void fun (int a, int b, int x, int y}
    int arr[2];
    arr[0] = a;
    arr[1] = b;
    y = arr[x];
}

- ranjeet July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Out of the usual thought process to use bitwise or arithmetic operators. Well done

- Phoenix July 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Brilliant! I would have never thought of that...

- Anonymous July 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is how switch-statements are implemented. Basically, base+offset a to piece of code based on integral value. This is less efficient though because it requires storage.

- Yev July 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This solution might not be acceptable due to base+offset under the covers

- Yev July 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome

- kdirishkumar July 02, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Superb...

- rakhee63 July 02, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

While I like this solution, it's one of those things that may or may not click. If it does, you'll impress the interviewer, if not, you're screwed.

- Anonymous July 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

y=a;
for(int i=1;i<=x;i++)
y=b;

- Kuntal Dutta July 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

y=a;
for(int i=1;i<=x;i++)
   y=b;

- Kuntal Dutta July 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good stuff. In python:
y = (a,b)[x]

- totolipton July 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

After the code is compiled to assembly, we definitely need * to calculate the offset of arr[x]. So I do not think this is correct.

How about this?

nbits = sizeof(int) * 8 - 1; // this is a compile time constant, there is no * at runtime.
x1 = (int)((unsigned int)(x) << nbits) >> nbits;
y = (x1 && a) || (x1 && b);

- zjzzjz August 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

y=a & 0xFFFFFFFF & (1 XOR x) | b & 0xFFFFFFFF & (0 XOR x)

- bp November 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
14
of 14 vote

y = (1 - x) * a + x * b

- xuzhiwen1024 June 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is an ideal solution.

- xuguangchen2012 July 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Now try it without arithmetic operators.

- autoboli July 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Part 1 :
y = (a+b) - (!X *b) - (X*a)

Part 2:
Y = (a & !X) | (b & X)

- vivekh September 06, 2015 | Flag
Comment hidden because of low score. Click to expand.
5
of 5 vote

I think the code below would work for the follow up case. Would love to see a simpler solution though.

int mask = 0;
int iter = x;
while (iter > 0)  // if that's not allowed, can manually bitwise-or 32 times
{
    mask |= iter;
    iter = iter << 1; 
}

int y = (b & mask) | (a & (~mask));

- Anonymous July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have voted up as it is a good solution with conditional operator used in while. But part of the question says no conditional operator :)

- Phoenix July 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 5 vote

y = (x && 'a') || 'b'

- Anonymous July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

arr[] = {a, b};
y=arr[x];

- ranjeet July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we use the fact that 0 and 1 can be interpreted as boolean values (0 = false, 1 = true)? Although I suppose this doesn't work in all languages.

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

Don't read this if you haven't given it a try

.

This is an interesting question. I think the interviewer want to check if we have kind of rigid thinking or not.

y = f(x)
f(0) = a
f(1) = b
--> simplest solution would be a linear equation: f(x) = k*x + r
k*0 + r = a --> r = a
k*1 + r = b --> k = b-r = b-a

--> y = f(x) = (b-a) * x + a

- hieu.pham June 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

y = A^B ^ [ A&x' | B&x ]

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

y = A^B^ [ B&X' | A&X]

- Abhijeet June 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

y = a ^ ((a ^ b) & (x))

- SK July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Sorry, what do you mean?

>>> a = 5
>>> b=  20
>>> x = 1
>>> y = a ^ ((a ^ b) & (x))
>>> y
4

- bob22 July 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

bob22 is right. You can't use &, since it will mask all bits but the right most one

- flameshimmer August 23, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

arr[] = {a, b};
y=arr[x];

- ranjeet July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suppose a and b are unsigned 32 bit integers

Then

mask = 0
for i in xrange(32):
    mask |= (x << i)
y = mask & b | ((~mask) & a)

The loop can be avoided just like

mask = (x<<31) | (x<<30) | (x<<29) | ... (x<<1) | x

- bob22 July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void fun (int a, int b, int x, int y}
    int arr[2];
    arr[0] = a;
    arr[1] = b;
    y = arr[x];
}

- ranjeet July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

excellent solution. out of the box thinking

- Phoenix July 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

y = a & x - 1 | b & -x;

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

We can do this using bitwise operations.

a & 0 = 0
a & -1 = a
a | 0 = a

So if x is zero, we can return

(a & -1) | (b & 0) = a | 0 = a.

If x is one, it should be

(a & 0) | (b & -1) = 0 | b = b

Final solution without unnecessary parentheses:

y = a & x - 1 | b & -x

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

x = !x + ~0
     y = ~x&a + x&b

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

Java impl. No additional storage.

int a=2;
		int b=3;
		int x=0;
		int y=(a^b)^((b & ~((x<<31)>>31)) | (a & ((x<<31)>>31)));

- Yev July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is this for real? I woudl assume Google's hiring committee would have thrown this question out.

- RecruitingForSandvineCanada July 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void assignX(int x,int a,int b,int &y)
{
	switch(x)
	{
		case 0:
			y=a;
			break;
		case 1:
			y=b;
	}
}

- sv July 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void assignX(int x,int a,int b,int &y)
{
	switch(x)
	{
		case 0:
			y=a;
			break;
		case 1:
			y=b;
	}
}

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

Short and concise

y=(a&~-x)|(b&-x);

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

int y=a;
for(int i=1;i<x;i++)
y=b;

- kuntal Dutta July 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class One {

private static int a,b,x,y;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Please Enter a and b values");

a = sc.nextInt();
b = sc.nextInt();

System.out.println("Please enter X value");

x = sc.nextInt();

y = (a&~-x)|(b&-x);

System.out.println("The y Value is "+y);
}

}

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

A solution in Java using bitwise operations, plus explanation (similar to another posted here)

public static int assignYBitwise(int a, int b, int x){
        /*
         * The interesting thing here is to obtain a proper
         * mask from x. Since x is a signed integer, ~x doesn't
         * work: 
         * ~0 = ~000...0 = 111...1 = -1 (OK) 
         * ~1 = ~000...1 = 111...0 = -2 (Not OK, should be 0)
         * 
         * A way around this is to shift x 31 bits left, so 
         * 000...0 becomes 000...0 but 000...1 becomes 100...0
         * (that is, Integer.MIN_VALUE). Then, shift 31 bits 
         * right, which won't undo the whole thing but instead
         * copy bit at pos 31 all the way to pos 0, as >> preserves
         * the sign of the integer. After that, the binary complement
         * can be applied:
         * 
         * 000...0  --(<<31)--> 000...0 --(>>31)--> 000...0 --(~)--> 111...1
         * 000...1  --(<<31)--> 100...0 --(>>31)--> 111...1 --(~)--> 000...0
         */
        int m = x<<31;
        m = m>>31;
        m = ~m;
        System.out.println("X="+x+" M="+m);
        int y = m&a | ~m&b ;
        return y;
    }

- Javier Torrente July 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int assignYBitwise(int a, int b, int x){
        /*
         * The interesting thing here is to obtain a proper
         * mask from x. Since x is a signed integer, ~x doesn't
         * work: 
         * ~0 = ~000...0 = 111...1 = -1 (OK) 
         * ~1 = ~000...1 = 111...0 = -2 (Not OK, should be 0)
         * 
         * A way around this is to shift x 31 bits left, so 
         * 000...0 becomes 000...0 but 000...1 becomes 100...0
         * (that is, Integer.MIN_VALUE). Then, shift 31 bits 
         * right, which won't undo the whole thing but instead
         * copy bit at pos 31 all the way to pos 0, as >> preserves
         * the sign of the integer. After that, the binary complement
         * can be applied:
         * 
         * 000...0  --(<<31)--> 000...0 --(>>31)--> 000...0 --(~)--> 111...1
         * 000...1  --(<<31)--> 100...0 --(>>31)--> 111...1 --(~)--> 000...0
         */
        int m = x<<31;
        m = m>>31;
        m = ~m;
        System.out.println("X="+x+" M="+m);
        int y = m&a | ~m&b ;
        return y;
    }

- Javier Torrente July 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

y = a*(!x)+ b*x;

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

y = (~x & a) + (x & b)

- harsv July 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if(x==0)
y=a;
for(int i=0;i<31;i++)
x|=x<<i;
if(x==1)
y=b;
y = a&~x | b&x

- Mahmoud Emam July 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int x=1;int a = 3;int b = 5; int y;


x|=x<<1;
x|=x<<2;
x|=x<<4;
x|=x<<8;
x|=x<<16;
y = a&~x | b&x;

- Mahmoud Emam July 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is solution

y = ((x - 1) & a) | (~(x - 1) & b)

- glebstepanov1992 July 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

y = (x & b) | (x | a)

- kalehv July 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A = (x&1)
B = ~A

x = a ^ b ^ (a & A) ^ (b & B)

- Source July 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A = (x^1)
B = (A^1)

x = a ^ b ^ (a & A) ^ (b & B)

- Source July 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

y = 0x60 | (x << 0x1)

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

{y = 0x60 | (x << 0x1)}

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

Here is solution in Java:

int foo(int a, int b, int x) {
	x = (x << 31) >> 31;
     	return (a & x) | (b & ~x);
}

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

Using Java:

void foo(int a, int b, int x) {
    	x = (x << 31) >> 31;
    	return (a & x) | (b & ~x);
}

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

// not a good solution, but still a solution.
try {
            int tmp = 1/x;
            y = a;
        } catch(IllegalArgumentException exception) {
            y = b;
        }

- jiangok2006 November 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Y = (a & (-(!X))) | (b & (-X));

- Heisenberg December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using just xor and power.
y = a xor x xor 1 xor (a xor b)^x

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

int ans(int a,int b,int x)
{
int y[2]={a,b};
return y[x];
}

- PIYUSH KUMAR May 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int x,y,a,b;
	cin>>a;
	cin>>b;
	cin>>x;
//	cin>>y;
    if(x)
    cout<<"value of y:"<<a<<'\n';
    else
    cout<<"value if y:"<<b<<'\n';

can we use this

- hyfun.me December 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution is:-

y=((1-x) & a) | (x&b)

- Arkadeep Baksi January 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another way:

y = a;

while(x)
{
y = b;
break;
}

- ganesh.koli777 July 19, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Lets say a=5, b=10, y=0, x = 0 or 1

if( x & 1){ // use bitwise
	   assign(b,y);
	}
	else{
	   assign(a,y);
	}

assign(int ab, int y){
ab = ab^y;
y = ab^y;  // y is now assigned
}

- ram.prasad.prabu July 01, 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