GeeksforGeeks » Interview Questions

Microsoft Interview Question about Algorithms, Bit Magic

(5 posts)
  1. Dheeraj
    guest
    Posted 1 year ago #

    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 = b
    

    There is a logical, arithmetic and a datastructure soln to the above problem.

  2. fat0ss
    guest
    Posted 1 year ago #

    Yes, create an array of size 2
    A[2] = {a, b};
    y = a[x]; is the solution ;-)

  3. fat0ss
    guest
    Posted 1 year ago #

    Yes, create an array of size 2
    A[2] = {a, b};
    y = a[x]; is the solution ;-)

  4. gauravs
    Member
    Posted 1 year ago #

    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;

  5. surya
    Member
    Posted 6 months ago #

    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.

RSS feed for this topic