Saturday, 14 September 2013

Bit Tricks

The basic Bit Operations are & (AND) , |(OR) , ^(X-OR), ~(Negation).

Checking nth bit set or not

Let 'x' be the given number.
 (x & (1<<n)) >> n

Setting the nth bit of number
x = ( x | (1<<n) )

Wednesday, 11 September 2013

Pop Vs Imap Protocol

Both POP3 and IMAP protocol are used for email retrieval process.

Pop Protocol



  • Pop protocol downloads the mail from the server, stores it locally and deletes from the server.

  • POP3 is useful only when you access email from only one computer.

  • POP3 is a one-way communication.

  • All mails are stored locally and without internet connection we can access them.

  • When the mail is accidentally deleted, we cannot recover it.


IMAP Protocol



  • IMAP protocol connects the remote server, caches the mail locally and then disconnects from the server.

  • IMAP is useful when you want access e-mail from multiple systems.

  • IMAP is a 2-way communication

Wednesday, 4 September 2013

Puzzle - A Bird and Two Trains

Puzzle


Consider two trains starting from two points X and Y respectively. Trains which starts from X travels at 15 Km/hr while the train which starts from Y travels at 20 Km/hr. Both trains start at the same time. A Bird also start from X at the same time. The speed of the Bird is 25 Km/hr. The Bird flies from X to Y until it meets the train started from Y. After that it changes its direction and fly towards the train started from X. When it meets X it start flying toward Y and so on until the train meets. What is the total distance covered by the bird ?


Hint : Think Simple !

Puzzle - A Gold For 7 Days

Puzzle


A loyal worker is working under you for 7(seven) days and you have only one gold bar to pay him. You must pay the worker at the end of each day. Otherwise, he wont come for next day's work. One more constraint is you have to make only two cuts in the gold bar. How can you pay him at the end of each day ?

Hint:   Barter System

Puzzle - Three Ants Problem

Puzzle


Three ants are sitting in the corners of the equilateral triangle. All ants start at same time and move at same speed constantly. The speeds of three ants are equal. Each ant picks up a direction randomly at first and moves in that direction. What is the probability that none of the ants will collide with each other ?

Tuesday, 3 September 2013

Puzzle - Red and Blue Marble

Puzzle


We have 50 Red , 50 Blue Marbles and 2 jars. One of the jar is chosen at random and then one marble is chosen from that jar at random. How would we maximize the chance of choosing red marble ? What is the probability of choosing red marble ?. All 100 marbles should be placed in the jar.

Monday, 2 September 2013

Unique Binary Search Trees

Problem Statement


Given n, how many structurally unique BST's (binary search trees) that store values 1...n?

For example,
Given n = 3, there are a total of 5 unique BST's.
   1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

Path Sum in Binary Tree

Problem Statement


Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

Addition in Linked List

Problem Statement


You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

Sunday, 1 September 2013

Flatten Binary Tree to Linked List

Problem Statement


Given a binary tree, flatten it to a linked list in-place.

For example,
Given
         1
/ \
2 5
/ \ \
3 4 6

The flattened tree should look like:
   1
\
2
\
3
\
4
\
5
\
6