GeeksforGeeks » Interview Questions
Microsoft Interview Question about Algorithms, Bit Magic
(5 posts)-
What are the different ways to say, the value of x can be either a 0 or a 1. Apparently the if then else solution has a jump when written out in assembly.
if (x == 0) y = a else y = bThere is a logical, arithmetic and a datastructure soln to the above problem.
-
Yes, create an array of size 2
A[2] = {a, b};
y = a[x]; is the solution ;-) -
Yes, create an array of size 2
A[2] = {a, b};
y = a[x]; is the solution ;-) -
Here you are assuming that value of x will be binary only.
whereas in the problem statement even if x = 10 or x = 2, then also the value of y = b;
-
I got the following solutions:
y= (!x) * a + x*b; (logical)
y= a+(b-a)*x; (arithematic)
int arr[2]={a,b}; // Given the only possible values are 0 or 1 for x
y=arr[x]; (DS)
Reply
You must log in to post.