Marketshare Inc. Interview Question for Java Developers


Country: India




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

int SwapBit(int n, int i, int j) {
	int bit_i = (n & (1 << i)) >> i;	// get bit i
	int bit_j = (n & (1 << j)) >> j;	// get bit j
	int mask = ~((1 << i) | (1 << j));	// mask to unset bits i and j
	return (n & mask) | (bit_i << j) | (bit_j << i);
}

- Miguel Oliveira June 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Brilliant!

- Murali Mohan August 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SwapIthAndJthBits 
{
	public int swap(int n, int i, int j)
	{
		int a = (1 << i);
		int b = (1 << j);
		
		int x = n & a;
		int y = n & b;
		if ( (x > 0 && y == 0)
			||(x == 0 && y > 0))
		{
		    x = (1<<i);
		    y = (1<<j);
		    n = n ^ (x | y);
		}
		return n;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
	    int n = 29;
	    int r = new SwapIthAndJthBits().swap(n, 1, 4);
	    System.out.println(r);
	}

}

- Suman June 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Interview {

	public static void main(String[] args) {
		int a = 0x800F0001;
		System.out.println(intToBinaryString(a) + "\n" + intToBinaryString(swap(a, 1, 12)));
	}

	private static int swap(int n, int i, int j) {
		int swap = 0;
		int a = 0x80000000 >>> i;
		int b = 0x80000000 >>> j;
		int x = n & a;
		int y = n & b;
		
		if (((x!=0) && (y==0)) || ((y!=0) && (x==0))) {
			swap = n ^ (a | b);
		} else {
			swap = n;
		}
		
		return swap;
	}

	private static String intToBinaryString(int n) {
		return String.format("%32s", Integer.toBinaryString(n)).replace(' ', '0');
	}

Output:

10000000000011110000000000000001
11000000000001110000000000000001

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

int main () {
unsigned char v = 128;
int swp1 = 7;
int swp2 = 0;
unsigned char tmp1 = (1 << swp1) & v;
unsigned char tmp2 = (1 << swp2) & v;
if (swp1 > swp2) {
tmp1 = tmp1 >> (swp1 - swp2);
tmp2 = tmp2 << (swp1 - swp2);
} else {
tmp1 = tmp1 << (swp2 - swp1);
tmp2 = tmp2 >> (swp2 - swp1);
}
tmp1 = tmp1 | tmp2 ;
v &= ~(1 << swp1);
v &= ~(1 << swp2);

printf("Final value = %d\n", tmp1 | v);
}

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

int Swap ( int n, int i, int j )
{
    int a = ((1<<i) | (1<<j)) ;
    int b = (n ^ a) & a ;
    if ( (b!=0) && (b!=a) )
        return ((n & ~a) | b) ;
    else
        return n ;
}

- Anonymous June 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <stdio.h>

int main()
{
int str;
int m = 0, n = 0;
int i,j;
printf("enter integer, i value, j value \n");
scanf("%d%d%d",&str,&i,&j);
if(str & (1 << i))
m = 1;
if(str & (1 << j))
n = 1;
if(m)
{
str |= (1 << j);
}
else
{
str &= (~(1<<j));
}

if(n)
{
str |= (1 << i);
}
else
{
str &= (~(1<<i));
}

printf("str = %d\n", str);
return 0;
}

- prashanthreddy.mtech June 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

if(word[i] != word[i]) {
word[i] = word[i] == 0 ? 1 : 0;
word[j] = word[j] == 0 ? 1 : 0;
}

- Anonymous June 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

//Careercup
//How do you swap bits at indexes i & j of a 32-bit memory word?
#include<iostream>

using namespace std;

int main()
{
   int num = 0;
   int i = 0;
   int j = 0;

   cout<<"Enter Num \n";
   cin>>num;
  
   do
   {
      cout<<"\n Enter i position (0-31)"<<endl;
      cin>>i;
   }while(i < 0 || i > 31);

   do
   {
      cout<<"\n Enter j position (0-31)"<<endl;
      cin>>j;
   }while(j < 0 || j > 31);

   num = num ^ (1<<i) ^ (1 <<j);

   cout<<"After Swaping bits num = "<<num;
}

- Satveer Singh June 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

I think you are trying to flip the bits at positions i & j. The question is asking to swap the bit value at location i with bit value at location j.

- Murali Mohan June 03, 2014 | Flag


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