Expedia Interview Question for Software Engineer in Tests


Country: United States




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

int Add(int x, int y)
{ 
    while (y != 0)
    {
        int carry = x & y; 
        x = x ^ y;
        y = carry << 1;
    }
    return x;
}

- Chandan Singh September 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

logic please

- rawat011 June 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Here is the code to mulitply two numbers using the bitwise opertators
vid mulitply_two_numbers(int n,int m){
int i=0;
int res = 0;
for(i=0;i< 32; i++){
if(n & 1 << i)
res= res + (m<<i);
}
printf(" %d*%d = %d", n,m, res);
}

- G Hareesh September 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Any algorithm/explanation to help pls?

- JustCoding September 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are using the addition here
res= res + (m<<i);

- rawat011 June 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package test;

import java.util.Stack;

public class BitWiseMultiplication {
	static int bit(int num) {
		StringBuffer sb = new StringBuffer();
		Stack<Integer> stack = new Stack<Integer>();
		
		while(num>=1) {
			stack.push(num%2);
			num = num/2;
		}
		while(!stack.isEmpty()) {
			sb.append(stack.pop());
		}
		return Integer.valueOf(sb.toString());
	}
	
	static int bitAdd(int a, int b) {
		Stack<Integer> stack = new Stack<Integer>();
		int remainder = 0;
		while (a>=1 || b>=1) {
			int temp = (a%10)+(b%10)+remainder;
			if (temp <= 1) {
				stack.push(temp);
			} else {
				stack.push(0);
				remainder=1;
			}
			a=a/10;
			b=b/10;
		}
		StringBuffer sb = new StringBuffer();
		while(!stack.isEmpty()) {
			sb.append(stack.pop());
		}
		return Integer.valueOf(sb.toString());
	}
	
	static int convertBitToInt(int bitNum) {
		int i=0;
		int result = 0;
		while(bitNum>=1) {
			result += Math.pow(2, i)*(bitNum%10);
			bitNum = bitNum/10;
			i++;
		}
		return result;
	}
	public static void main(String[] args) {
		int a = 9;
		int b = 2;
		int bitA = bit(a);
		int bitB = bit(b);
		System.out.println("Num A:: "+a);
		System.out.println("Num B:: "+b);
		System.out.println("Bit A:: "+bitA);
		System.out.println("Bit B:: "+bitB);
		
		int bitMulResult = bitA*bitB;
		System.out.println("Bit Mul Result:: "+bitMulResult);
		
		int bitAddResult = bitAdd(bitA, bitB);
		System.out.println("Bit Add Result:: "+bitAddResult);
		
		int mulResult = convertBitToInt(bitMulResult);
		System.out.println("Int Mul Result:: "+mulResult);
		
		int addResult = convertBitToInt(bitAddResult);
		System.out.println("Int Result:: "+addResult);
	}
}

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

package test;

import java.util.Stack;

public class BitWiseOperations {
	static int bit(int num) {
		StringBuffer sb = new StringBuffer();
		Stack<Integer> stack = new Stack<Integer>();
		
		while(num>=1) {
			stack.push(num%2);
			num = num/2;
		}
		while(!stack.isEmpty()) {
			sb.append(stack.pop());
		}
		return Integer.valueOf(sb.toString());
	}
	
	static int bitAdd(int a, int b) {
		Stack<Integer> stack = new Stack<Integer>();
		int remainder = 0;
		while (a>=1 || b>=1) {
			int temp = (a%10)+(b%10)+remainder;
			remainder=0;
			if (temp <= 1) {
				stack.push(temp);
			} else {
				stack.push(0);
				remainder=1;
			}
			a=a/10;
			b=b/10;
		}
		if (remainder == 1) {
			stack.push(1);
		}
		StringBuffer sb = new StringBuffer();
		while(!stack.isEmpty()) {
			sb.append(stack.pop());
		}
		return Integer.valueOf(sb.toString());
	}
	
	static int convertBitToInt(int bitNum) {
		int i=0;
		int result = 0;
		while(bitNum>=1) {
			result += Math.pow(2, i)*(bitNum%10);
			bitNum = bitNum/10;
			i++;
		}
		return result;
	}
	public static void main(String[] args) {
		int a = 4;
		int b = 4;
		int bitA = bit(a);
		int bitB = bit(b);
		System.out.println("Num A:: "+a);
		System.out.println("Num B:: "+b);
		System.out.println("Bit A:: "+bitA);
		System.out.println("Bit B:: "+bitB);
		
		int bitMulResult = bitA*bitB;
		System.out.println("Bit Mul Result:: "+bitMulResult);
		
		int bitAddResult = bitAdd(bitA, bitB);
		System.out.println("Bit Add Result:: "+bitAddResult);
		
		int mulResult = convertBitToInt(bitMulResult);
		System.out.println("Int Mul Result:: "+mulResult);
		
		int addResult = convertBitToInt(bitAddResult);
		System.out.println("Int Result:: "+addResult);
	}
}

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

public static int multiply(int x, int y) {
    return ((x & 1) > 0 ? y : 0) + ((x & ~1) > 0 ? multiply(x >> 1, y << 1) : 0);
}

- chaitu.jil October 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int multiply(int x, int y) {
    if (y == 0 || x == 0) {
        return 0;
    }
    if (x == 1) {
        return y;
    } else {
        return multiply(x >> 1, y << 1);
    }
}

- chaitu.jil October 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this website is very bad

- harsa December 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Bitwise Multiplication

public int Multiplication(int i, int j)
        {
            // take small number as 2nd number to make it faster
            if (i < j)
            {
                int temp = i;
                i = j;
                j = temp;
            }

            int totalBits = 0;
            int m = 0;

            // calculate bits in 2nd number
            while (Math.Pow(2, m) < j)
            {
                totalBits++;
                m++;
            }

            int sum = 0;
            int carry = 0;

            for (int n = 0; n < totalBits; n++)
            {
                // Making AND between 2nd number and numbers 1, 2, 4, 8 .. to detect whether bit    in a specific location in 2nd number is '0' or '1'
                int digit = j & (int)Math.Pow(2, n);

                //If bit in 2nd number is '1' then only do below operation
                if(digit > 0)
                {
                    // this is considering both numbers are 8 bits
                    digit = 255;

                    // Below operation is same when we do a decimal multiplication
                    carry = ((digit & i) << n) & sum;
                    sum = ((digit & i) << n) ^ sum;

                    while (carry != 0)
                    {
                        int newCarry = sum & (carry << 1);
                        sum = sum ^ (carry << 1);
                        carry = newCarry;
                    }
                }               
            }

            return sum;
        }

- pritam83 March 03, 2013 | 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