Interview Question


Country: United States




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

Pretty simple one !! Source : Cracking the coding interviews

say Input: N = 1000000000, M = 10011, i = 2(startIndex), j = 6(endIndex)

The algorithm would look like
1. Clearing the bits j through i in N
2. Shift M so that it lines up with bits j through i
3. Merge M and N.

public static int updateBits(int n, int m, int i, int j) {
int max = ~0; /* All 1’s */

// 1’s through position j, then 0’s
int left = max - ((1 << j) - 1);

// 1’s after position i
int right = ((1 << i) - 1);

 // 1’s, with 0s between i and j
 int mask = left | right;

 // Clear i through j, then put m in there
 return (n & mask) | (m << i);
 }

- ram.prasad.prabu June 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I thought we expect results something like:
if A=1000011110000
and B=1000000000000
copy from bits 4 to 8
then C=1000011110000

Am I missing something in this word puzzle ?

- results June 15, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static int copyBits(int A, int B, int startIndex, int EndIndex) {
        if (startIndex > EndIndex) return 0;
        int weight = 0;
        int tempA;
        for (int i = startIndex; i < EndIndex; i++) {
            tempA = A >> i;
            weight = 1 << i;
            if ((tempA & 0x01) == 1)
                B = B | weight;  
        }
        
        return B;
    }

- miroslavign June 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

public static int copyBits(int A, int B, int startIndex, int EndIndex) {
        if (startIndex > EndIndex) return 0;
        int weight = 0;
        int tempA;
        for (int i = startIndex; i < EndIndex; i++) {
            tempA = A >> i;
            weight = 1 << i;
            if ((tempA & 0x01) == 1)
                B = B | weight;  
        }
        
        return B;
    }

- miroslavign June 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

unsigned int CopyBits (int n, int m, int i, int j) {
  unsigned int max = ~0;

  unsigned int left = max - ((1 << (j+1)) - 1);

  unsigned int right = ((1 << i) - 1);

  unsigned int mask = left | right;
  unsigned int mask2 = ~mask;

  return ((n & mask) | (m & mask2));
}

- Anonymous June 15, 2015 | 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