Aug 042017
 

You may solve the simple probability, “Given a set of n randomly chosen people, what is the probability of two people having same birthday?”

You may also solve,  “At least how many people need to be picked to make the probability 100% that at least two of them have same birthday?”

Answer to the second question is 367, because there are 366 possible birthdays (including Feb-26).

The question now is, “At least how many people need to be picked to make the probability 99.9% that at least two of them have same birthday?”

Answer is 70.

And if the question is,“At least how many people need to be picked to make the probability 50% that at least two of them have same birthday?”

Answer is 23.

Continue reading »

Aug 032017
 

Let us design a data structure in which we want to store numbers (taking it for simplicity, else it can be anything). We want to minimise the time taken by the following three operations:

  • Insertion, time taken to insert a new value.
  • Deletion, time taken to remove an existing value.
  • Searching, time taken to search for a value in the data structure and see if it is present or not.

Which data structure do you suggest ?

Continue reading »

Aug 022017
 

Doctor gave you two types of medicine tablets (pils) A and B, and asked you to take one from each daily. The two tablets are in different bottles and look exactly same

One day, while taking the pills, you open Bottle-A and tap one pill out in your hand. Then you open the second bottle to take one pill from it, but accidentally two pills pop out from the bottle on your hand. Now you have 1 A-Pill and 2 B-Pills in your hand and you cannot distinguish them.

The pills are very expensive and you do not want to throw them, neither can you afford to take wrong pills. What should you do?
Continue reading »

Jun 282017
 

Print Next Greater Element for every element in the array. The Next greater Element of element x is first greater element on its right side.

Elements for which no greater element exist, next greater element is -1.

Example:
- For any array, rightmost element always has next greater element as -1.
- All elements of an array sorted in decreasing order have next greater element as -1.
- For input array {5, 9, 1, 15}, the next greater elements of each element are as follows.
{9, 15, 15, -1}
- For input array {20, 17, 15, 10}, the next greater elements are {-1, -1, -1, -1}

Continue reading »

Jun 102017
 

Binary tree is a specialization of Graph data structure. Node in a Graph is called a Vertex and the connection between two vertices is called an Edge.

If we use this terminology, A Binary tree edge is only from a parent to its (at the most two) children.  In Graph, any vertex (Node) can connect to any vertex and there is no limit on the number of vertices that a vertex can connect to. The connection can be uni-directional (as in Binary trees) or bi-directional. When connections are uni-directional the edge is called directed edge and graph is called directed graph. When connections are bi-directional, edge is called un-directed edge and graph is called undirected graph. Because of these connections, there is no hierarchy among the vertices of a Graph. Continue reading »