Skip to content

Instantly share code, notes, and snippets.

@co89757
Last active August 29, 2015 14:07
Show Gist options
  • Save co89757/4f965d8fa4d033731870 to your computer and use it in GitHub Desktop.
Save co89757/4f965d8fa4d033731870 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment