Submitting Special characters in your Form

If you will try to submit special characters like chinese or spanish text, you will see some junk boxes being submitted to the server. We need to make sure proper encoding, say UTF-8 is being in place. In your HTML (JSP/PHP etc) page, you will need to let browser know that your page works with UTF-8.

<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>com.mysite.SessionFilter</filter-class>
<init-param>
<param-name>PARAMETER_ENCODING</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

In addition, you might want to enforce the settings onto your server to let it know that you are expecting UTF-8 content in request. For example in JBOSS, we will create a filter and enforce UTF-8 content type

And create the filter class like

public class SessionFilter implements Filter {
private String encoding="";

/**
* destroy method to clean up any activities
*/
public void destroy() {
}

/**
* Override the doFilter method to apply any action, in this case setting request encoding
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException
{
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}

/**
* Override init method to set parameter encoding as provided by web.xml
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
if (filterConfig.getInitParameter("PARAMETER_ENCODING") != null)
{
encoding= filterConfig.getInitParameter("PARAMETER_ENCODING");
System.out.println("encoding "+encoding);

}
}

}

Dust me- clean up unused CSS

Recently came accross a legacy code which had a lot of css code and files, which was written over years and most of it was obsolete. Dust-me, firefox plugin helped to figure out all the unused css code. Though I had to traverse all possible flows in the application, but end result was good as few files were identified which were not being used completely and were removed, saving page load time.

Core Dump files slowing down website

For last few days, or rather weeks, my blog was really slow. Infact the site was throwing resource limit error at times. I contacted my webhosting provider, but they just replied they cannot find anything. So I decided to do some investigation on my own.

First step was to check memory and CPU usage, which turned out to be very high, almost 100%. The fishy thing I figured out was disk space usage, which was way above data I have, so I checked the file system. I figured out hundreds of core.XXXXX files. A little googling showed that these were dump files created by Apache for memory dump, in case some error / crash occured. Deleting these extra files did solve the issue.

More info on the topic

https://wordpress.org/support/topic/hacked-with-strange-core-files

https://wordpress.org/support/topic/arrrg-so-many-core-files

http://en.wikipedia.org/wiki/Core_dump

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.

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.

graph

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.