• 10

Country: United States

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

``````void fun (int a, int b, int x, int y}
int arr;
arr = a;
arr = b;
y = arr[x];
}``````

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

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

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

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

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.

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

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

Awesome

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

Superb...

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

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.

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

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

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

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

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

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

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

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.

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);

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

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

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

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

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

I think this is an ideal solution.

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

Now try it without arithmetic operators.

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)

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
{
iter = iter << 1;
}

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

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 :)

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

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

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

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

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.

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

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

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

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

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

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

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

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``````

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

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

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

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

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):

The loop can be avoided just like

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

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;
arr = a;
arr = b;
y = arr[x];
}``````

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

excellent solution. out of the box thinking

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

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

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``

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

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

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

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

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.

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;
}
}``````

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;
}
}``````

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

Short and concise

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

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;``````

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();

x = sc.nextInt();

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

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

}

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;
}``````

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;
}``````

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

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

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

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

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``````

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;``````

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

This is solution

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

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

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

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)

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)

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

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

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

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

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);
}``````

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);
}``````

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;
}``````

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

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

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

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

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

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

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

The solution is:-

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

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

Another way:

y = a;

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

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
}``````

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.