## Are you a makkhichoose

We know that future of online shopping is bright in countries like India. With sites like snapdeal, flipkart, amazon, shopclues, jabong, myntra etc, things will heat up further, which will be beneficial for end customer as he will get multiple options.

With Online shopping, people are also moving away from loyalty to one vendor or brand. Earlier we used to go for same shop for various electronics needs, but now before buying we can compare prices and offers on different websites (plus nearby stores). And with this knowledge, there are various player which are ready to help you with comparison among online sites. There are websites like Junglee, which will help you compare various prices, and I recently came across some browser extensions which will help you compare prices on the fly when you are viewing the product itself. Makkhichoose is one such extensions. Though it is not cooked up completely yet (I could see some products on various sites which were not reported correctly), but it definitely shows the where the future of online shopping heading to.

## OWASP top 10

Here are top 10 web security threats highlighted by OWASP or Open Web Application Security Project

## Division problem

Problem statement- 3 divides 111 , 13 divides 111111 etc.. find a number having all one’s which is shortest divisible by a given number which has 3 as its last digit.

Logic- The challenge here is that you will soon run out of limit for int or long if you keep on increasing the number. So the idea is to divide in a way which you used to do on paper for large numbers

13)111111(8547

104

——-

71

65

——–

61

54

———-

71

71

————

0

Start with 1 (or 11) and keep adding one, keep a count of 1s added, divide by the required number unless the reminder is 0.

Implementation

`public static void divide(int num)`

{

int division=11;

int count=2;

while(true)

{

if(division%num==0)

{

System.out.println("count:"+count);

break;

}

else

{

int remindor=division%num;

division=remindor*10+1;

count++;

}

}

}

## InOrder, PreOrder and PostOrder traversal

I had discussed Breadth first and Depth First traversal algorithms sometime back.

There are 3 important traversing algorithms for tress. http://en.wikipedia.org/wiki/Tree_traversal

InOrder

-Travel Left, Travel Root, Travel Right.

PreOrder

-Root, Left, Right

PostOrder

-Left, Right, Root

Here is recursive implemenation

`public void inOrder(Node root)`

{

if(root.left!=null)

inOrder(root.left);

System.out.println(root.number);

if(root.right!=null)

inOrder(root.right);

}

public void preOrder(Node root)

{

```
```System.out.println(root.number);

if(root.left!=null)

preOrder(root.left);

if(root.right!=null)

preOrder(root.right);

}

`public void postOrder(Node root)`

{

if(root.left!=null)

postOrder(root.left);

if(root.right!=null)

postOrder(root.right);

System.out.println(root.number);

}

## Basketball Probability Puzzle

Problem: You have two options

1: One successful shot at the hoop

2: 2 of 3 successful shots at the hoop

Let P be the probability of making a successful hit, for what value of P will you choose option 2 over 1.

Solution:

If P is probability for option 1

Option 2, we can have 2 cases of success, one all 3 shots are successful – P*P*P

or 2 Hits and 1 Miss (HHM, HMH, MHH) 3 cases, each with probability of P*P*(1-P) i.e. 1-P for one miss. as there are 3 cases, total probability is 3*P*P*(1-p). Combining both result for option 2, P*P*P + 3*P*P*(1-P).

To prefer option 2,

P*P*P + 3*P*P*(1-P)>P

P*P+3P(1-P)>1

Can be solved to quadratic equation

-2P^2+3P-1>0

or

(2P-1)(P-1)>0

P>1 not possible so

P>1/2 or .5

Choose option 2 when probability of hitting hoop is more than 0.5

## Monty Hall problem

Monty Hall problem is an interesting problem that plays with probability.

Problem: There are three doors, say A, B and C. Behind one of the doors there is a prize and two are empty. You have to make a choice and try to win the prize. You choose a door say A, now host opens the door C, which is empty. The question is, should you switch the doors?

Solution: According to one solution proposed, Switching doors will increase probability of winning. This is the explanation behind that

Initially all 3 doors has probability of 1/3 of winning. So when we choose door A, we had probability of winning – 1/3. Now Doors B & C have collective probability of winning as 2/3. So when we know C is empty, probability of prize behind B is 2/3(?).

Frankly the argument does not look very strong, for me the new probability should be 1/2 for each door.

Read more here- http://en.wikipedia.org/wiki/Monty_Hall_problem

## Graph Data Structure

Graph is distant relative of Tree. Infact it is complexer than tree, as in tree, a node usually have link with nodes one level up or down and does not conatin any cycles, whereas graph does not have any such restrictions. Graph data structure is more useful in case of something like traveling salesman problem, or to represent computer network, i.e. a structure where we have nodes and relationship between them.

In the above graph structure, we have 4 nodes or Vertices and 6 Edges (connectors). A graph is usually represented as G=(V,E).

Please note that the graph given above is directed, that is we can see link is from 1->2 and not 2->1. In undirected graph, the arrows showing direction of connection will not be there and connection can be considered both ways.

**Adjacency Matrix**

The above graph can be shown as

[1] [2] [3] [4]

[1] 0 1 0 1

[2] 0 0 1 1

[3] 1 0 0 0

[4] 0 0 1 0

if the same graph was undirected

[1] [2] [3] [4]

[1] 0 1 1 1

[2] 1 0 1 1

[3] 1 1 0 1

[4] 1 1 1 0

Always Aij=Aji in undirected

**Adjacency List Representation**

Node1->[Node2, Node4]

Node2->[Node3, Node4]

Node3->[Node1]

Node4->[Node3]

Other useful information for Graphs

**Degree**: Number of edges being attached to current vertex

** In-Degree:** Number of edges coming into the vertex in case of directed graph

**Out-Degree:** Number of edges going out from the vertex in case of directed graph

**Weighted Graphs:**

This is important in case we are trying to solve problems like traveling salesman. For example, if we look at above graph, we can see there are 2 ways to reach Vertex 3 from Vertex 1, that is, 1->4 or 1->2->4. Say these vertices represent cities, and edge weight represent distance in this case.

1->2= weight 20

2->4= 30

1->4= 60

So we can choose 1->2->4 over 1->4 as it will be shorter distance.

## Depth First Search

In last post I talked about Breadth First Search. An alternative to that, is Depth First Search, used in cases we want to check upto a depth level first rather than breadth.

So let’s look back at the last example

This time we would like to parse something like

1. Check Root- 10

2. Check till depth level for one node- 18,7

3. Repeat for other nodes 22,15 and then 5, 12

A straight forward way is to modify the Breadth First Search algorithm to use Stack instead of Queue.

1. Push Root Node R to Stack S

2. Pop s node from S

3 a . If s.val is required value, return Success

3 b. Else Push all Neighbor/ child nodes to stack

4. If Stack is not empty, Repeat from 2

5. If Stack is empty, we have traversed all the nodes and not found the required node. Return failure.

## Breadth First Search

Breadth First search is an effective search mechanism for Graphs and Tree Data structure. As the name suggest, this technique makes sure that all the nodes at same level/ breadth are checked before moving to deeper levels.

The above structure is hybrid of tree and graph (Still a tree as there are no cycles). Let us what it means to do a breadth first search here.

We will start with root node i.e. 10.

Move to level 2 and check nodes 18, 22, 5.

Check the next level- 7, 15, 12 and so on.

Here is algorithm to implement Breadth first search

1. Add root node R to queue- Q

2. Fetch next node N from Q

3 a. If the N.val is required val, return success

3 b. Else Find all Child/ Neighbour nodes which are not traversed already and add to Queue

4. If there are more nodes in Q, Repeat from 2.

5. If Q is empty, we know we have traversed all nodes, return failure as node not found

Read more here- http://en.wikipedia.org/wiki/Breadth-first_search

http://en.wikipedia.org/wiki/Breadth-first_search

## Understanding Permutation and Combination -2

**Combinations:**

In last post I talked about calculating permutation. Combinaitons differ from permutation in that ordering does not matter in case of combinations. For example we were concerned about first digit, second digit etc, whereas in combination it will not matter. For example we need to pull 4 balls out of 10, order does not matter. To make it easy, let us say 2 balls are to be choosen out of 4

So we can choose

1,2

1,3

1,4

2,3

2,4

3,4

Total 6

Whereas when we were talking about permutation, we were concerned about order as well. So 1,2 was different from 2,1

1,2

1,3

1,4

2,1

2,3

2,4

3,1

3,2

3,4

4,1

4,2

4,3

Total 12, same as 4P2 or 4!/(4-2)!

So the difference between permutation and combination is that we have removed ordering info in case of combinations. In how many ways we can order 2 numbers, exactly 2 (1,2 or 2,1). In how many ways we can order 3 objects- 6 (1,2,3; 1,3,2; 2,1,3; 2,3,1; 3,1,2; 3,2,1) or 3!. Similarly we can prove n objects can be ordered n! times.

So a formula for combinations would be nPr/r!

nCr= nPr/n! or n!/(n-r)!r!

Using it for 4 balls out of 10

10!/4!6! = 210