Browsing the blog archives for February, 2011

Balanced Tree

Tweet Question: How would you check if a binary tree is balanced? Answer: A tree is considered balanced when the difference between the min depth and max depth does not exceed 1. Recursive algorithms always work well on trees, so here’s some code. int min_depth( Node * root ) { if( !root ) { return […]

5 Comments

Queue Using Stacks

Tweet Question: How would you use stacks to implement a queue? Answer: So how could we go about doing this? What if we used two stacks and used one as the incoming stack and the other as the outgoing stack? A queue is a FIFO structure, so when we enqueue something, we need to make […]

3 Comments

Bit Swaps

Tweet Question: How would you find the number of bit swaps required to convert integer A to integer B? Answer: Gut instinct might suggest that you go through every bit of A and every bit of B while simultaneously updating a count of the bits that are different. But how about a cooler way to […]

Challenge – Equal Probability between 1 and 7

Tweet Question: Write a method to generate a random number between 1 and 7, given a method that generates a random number between 1 and 5. The distribution between each of the numbers must be uniform. Challenge: Do you know the answer to this question? Post in the comments. Let’s see who is up for the […]

84 Comments