Interview Practice Extra 07 – Universal Value Binary Tree

Question Design an algorithm to verify that a tree is a universal value binary tree. Universal value binary tree means all value in that tree is the same. Solution There is two approach for this problem. One is with recursive function and another is with iterative function. For this problem, iterative function makes simpler answer. However,…

Read more...

Interview Practice 28 – Counting One in Binary Expression

Question Given a number, find the number of 1 in the number’s binary expression. For example, binary express of 10 is 1010. So the number of 1 in it is 2. Solution To solve this, we can check each bit by shifting the bits one by one. 1. 1010 -> check 0 2. 101 -> check…

Read more...

Interview Practice 25 – Longest Consecutive Digits

Question Given a function prototype: int continumax(char *output_string,char *input_string). Implement it to find the longest consecutive digits. This function must return the length of the longest digits. The found longest digits should be written to the memory location that output_string is pointing. For example, if input_string is “abcd12345ed125ss123456789”, function returns 9 and output_string becomes “123456789”. Solution This…

Read more...

Interview Practice 19 – Fastest Way to Calculate Fibonacci Sequence

Question Construct an algorithm so that the Nth item of  Fibonacci Sequence can be found in the shortest time. Definition of Fibonacci Sequence is: = 0 , when n = 0 f(n) = 1 , when n = 1 = f(n-1) + f(n-2), when n > 1 Solution There are many solutions for this, the simplest way…

Read more...

Interview Practice 18 – Last Surviving Number in Loop

Consider Consider there is a list containing N numbers, and which formed a ring. That is, item n+1 is item 1. Construct an algorithm such that it traverses through the ring, and remove the Mth item. Repeat this process until there is only one number left in the ring and then output this number. For…

Read more...

Interview Practice 17 – Find The String Appeared Once

Question Given a string, write an algorithm to find the character inside that appeared only once. For example, the result for string “abaccdeff” is b. Solution We can use a hashtable to count the appearance times for each character. Then rescan the hashtable for the character that appeared only once. These two step take O(n)…

Read more...

Interview Practice 15 – Mirror Image of Binary Tree

Question Construct 2 algorithms to make mirror image of any binary tree inputs, one of them using recursive method, another one using looping method. Mirror image means a binary tree that is the horizontal reflection of the original tree. Solution First, to do it in recursive method, we can perform pre-order traversal as usual, see preference….

Read more...

Interview Practice 11 – Greatest Distance Between Two Nodes in Binary Tree

Get the greatest distance between two nodes in a binary tree. Assume links between nodes are bidirectional. Distance is defined as the amount of nodes connected along the path linked two nodes.

Read more...

Interview Practice 10 – Reverse Sentence

Simple task, reverse words in a sentence.

Read more...

Tons of things working on

it’s been a while since the last post. After coming to new york, I have been very busy on my study, personal projects and (of course) playing around. studying in new york university is harsh, but not as hard as i think. I met nice people, hanged out with my beloved new friends. I even…

Read more...