Interview Question for Software Engineer / Developers






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

Is the representation of floating point number given? for example IEEE754
If so, just count. If not, unsolvable.

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

Yes its IEEE representation.
Still do you need to count the number of bits that are set in 32 bit representation?

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

I think this can't be avoided, unless you use some compiler-specific functions like __builtin_popcount in gcc.

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

If I am not wrong we need t count the number of bits that are set in the mantissa part + 1 for finding the bits set in the floating point number?

- DashDash July 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This comment is a complete quote from:

graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan

- Greg A. Woods November 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only the high bit set, then it will only go once through the loop.

Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie) mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to him that this method "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and published in 1964 in a book edited by Beckenbach.)"

long count_bits(long n) {
unsigned int c; // c accumulates the total bits set in v
for (c = 0; n; c++)
n &= n - 1; // clear the least significant bit set
return c;
}

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

My question here is if such a question is given do we need to find out the number of bits that are set in the 32 bit representation of just the mantissa part as the rest are signed bits and exponent.

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

count=0;
if(n&1)
{
count++;
n=n>>1;
}
return count;

- khush.sriv August 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here, no of bits set in the memory layout is what we need, right? We could use a union as below:

#include <stdio.h>

int main()
{
    union u {
        float num;
        int i;
    } u1;
    u1.num = 0.15625; /* floating number to check */

    int num_bits = 0;
    while (u1.i) {
        num_bits++;
        u1.i = u1.i & (u1.i-1);
    }

    printf("No of bits is %d\n", num_bits);
}

Wouldnt this do?

- jtr.hkcr March 10, 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