Interview Question for Software Engineer / Developers






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

i is the number of input

int count=0;

if(i&0xffff0000){ count |= 1<<4; i = (i&0xffff0000)>>4;}
...

- geniusxsy January 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a typo/mistake:

should be:

if(i&0xffff0000){ count |= 4; i = (i&0xffff0000)>>4;}

- SiyangXie January 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain your solution?
What's your output?
"count"?
count never changes in your program.

- Anonymous January 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <assert.h>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
int i = 0;
cin>>i;
int index = 0, temp = i;

if(temp&0xffff0000)
{
index += 16;
temp = (temp&0xffff0000)>>16;
}

if(temp&0x0000ff00)
{
index += 8;
temp = (temp&0x0000ff00)>>8;
}

if(temp&0x000000f0)
{
index += 4;
temp = (temp&0x000000f0)>>4;
}

if(temp&0x0000000c0)
{
index += 2;
temp = (temp&0x0000000c0)>>2;
}

if(temp&0x00000002)
++index;

cout<<index<<endl;
return 0;
}

//geniusxsy's post actually said all, I just want to check if I can finish it in 15mins

- Bingsuns January 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a question for you:

how do you make your code machine independent? i.e., it should work for various size of int.

- geniusxsy January 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can use int32_t

- Bingsuns January 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

no, I mean, how would you do to make your code more generic to handle int of 32bit/64bit even 128bit...

- geniusxsy January 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

a hint: use sizeof(int)

- geniusxsy January 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int most_significatn_bit(const unsigned int input)
{
if (input == 0) {
return 0;
}

unsigned int n = 0x80000000;
int nIndx = 32;
for(int i = 32; i > 0; i --) {
if (input & n) {
return i;
}
n = n >> 1;
}
return 0;
}

- Anonymous January 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is Simple O(n)...Anyone can do...can u do it in O(1)..dat matters....

- Prince June 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int most_sign_One(int input)
	{

		int tmp = 1;
		int i = 0;
		
		while(input >= tmp)
		{
			i++;
			tmp = tmp<<1;
		}

	return i-1;

	}

- Anonymous January 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int i=0;
while(number){ number=number<<1; i++; }
return (sizeof(number)-i);

- Naga Samrat Chowdary, Narla May 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Depending upon the architecture of the machine, the most significant bit is either the left most or the right most. So one way could be to left shift or right shift by 1

- abhimanipal February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

haha... just take log to the base 2. best of luck guys. :P

- sumo March 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

unsigned msb_set(unsigned int n){
unsigned count = 0; // r will be log2(n)

while (n >>= 1)
{
count++;
}

return count;
}

- Sudipto May 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think counting the no of leading zeros i.e from M.S.B of any no could be a solution.

- somename May 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// find the position of the most significant 1 bit?
int GetFirstMsb1(int num)
{
if (!num)
return -1;

int pos = 0, i = 0;
unsigned int mask = 0xff000000;
int n = num & mask;
do
{
if (n)
{
if (n >= 128)
pos = 0 + i;
else if (n >= 64)
pos = 1 + i;
else if (n >= 32)
pos = 2 + i;
else if (n >= 16)
pos = 3 + i;
else if (n >= 8)
pos = 4 + i;
else if (n >= 4)
pos = 5 + i;
else if (n >= 2)
pos = 6 + i;
else if (n >= 1)
pos = 7 + i;

return pos;
}

mask >>= 8;
n = num & mask;
i += 8;
}
while (i < sizeof(int) * 8);

return pos;
}

- Nina June 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The best way to do,for an integer, is to find out the log base 2,as suggested by sumo.

- dipendra July 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int bit = !(n ^ (1 << sizeof(int)))

- M August 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

log(n)/log(2)

- swapnilsj August 07, 2016 | 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