co89757
9/29/2014 - 6:12 AM

coding interview drill with commentary

coding interview drill with commentary

Fundamentals

Gray Code

Binary2Gray conversion

unsigned int binary2gray(unsigned int bi){
return bi ^ (bi>>1) ;
}

###Bit manipulation

clearing the last set bit

x & (x-1)

Binary Tree Iterator

Tree iterator

Sorted Arrays

// remove duplicates from an array
public int removeDuplicates(int[] A) {
    if (A == null || A.length == 0) {
        return 0;
    }
    int left = 1;
    int right = 1;
    while (right < A.length) {
        if (A[right - 1] != A[right]) {
            A[left] = A[right];
            left++;
        }
        right++;
    }
    return left;
}