## Monday, December 24, 2012

### Segment Trees and lazy propagation

In this topic i will explain a very interesting data structure that can be used to solve a specific set of problems. I will start by explaining its definition and the proceeding with an example problem to solve with it.

• What  is segment trees?
• Order of growth of segment trees operations
• Lazy propagation
• Sample problems to try
• References
What is segment trees?

Segment Trees is a Tree data structure for storing intervals, or segments, It allows querying which of the stored segments contain a given point. It is, in principle, a static structure; that is, its content cannot be modified once the structure is built. It only uses O(N lg(N)) storage.

A segment trees has only three operations: build_tree, update_tree, query_tree.

Building tree: To init the tree segments or intervals values
Update tree: To update value of an interval or segment
Query tree: To retrieve the value of an interval or segment

Example Segment Tree:
• The first node will hold the information for the interval [i, j]
• If i<j the left and right son will hold the information for the intervals [i, (i+j)/2] and [(i+j)/2+1, j]
Notice that the height of a segment tree for an interval with N elements is [logN] + 1. Here is how a segment tree for the interval [0, 9] would look like:

Order of growth of segment trees operations
• build_tree: O(N lg(N))
• update_tree: O(lg(N + k))
• query_tree: O(lg(N + k))
K = Number of retrieved intervals or segments

Lazy Propagation

Sometimes a segment tree operation wouldn't survive if the problem constraints is too large, here it come lazy propagation along with the segment tree.

In the current version when we update a range, we branch its childs even if the segment is covered within range. In the lazy version we only mark its child that it needs to be updated and update it when needed.

Sample Problems to try
References

## Wednesday, December 19, 2012

### Lucas' Theorem to solve binomial coefficients

I am happy cause only one problem and i will be done solving interviewstreet mathematical problems. After solving the Binomial Coefficients  problem i think i have something useful to share with you.

In this problem you are given two number N and P, where P is a prime number. You are requested to find how many binomial coefficients of n become to 0 after modulo by P.

So the output should be the number of  $\tbinom nk$s (0<=k<=n)  each of which after modulo operation by P is 0.

I hear you saying its an easy problem, but you won't say that after knowing about the input constraints.

Constraints:
n is less than 10500.
P is less than 109.

For such large number i will use Java to solve this problem, thanks to the BigInteger Class in java. After doing some search i found that there is a theorem called Lucas' Theorem which is as follows:

Given n, k, and prime p, binomial (n,k) is divisible by p if and only if at least one of  the base p digits of k is larger than the corresponding digit of m (also base p digit).

So to solve this problem we need to find the base P representation of n, and counting how many numbers (R) that have all digits (in base P) less than the corresponding digit in n.

1. We have k is between 0 and n, so we have (n+1) numbers to check with n.
2. Now need to find R.

Now if n in base P is as follows: abc...

Then we need to get all numbers that have:

1. First digit is 0, 1, 2, .... a-1
2. Second digit is 0, 1, 2, .... b-1
3. Third digit is 0, 1, 2, ...., c-1

Obviously R = (a+1)*(b+1)*(c+1).

So we have (n+1) total numbers, subtracting  R, then the result would be n+1-R.

## Friday, December 7, 2012

### Hacking linux keyboard

Now again with another interesting kernel hack, in this post i will show you a sample input device that report keyboard events without pressing any keys. In this sample i will report the left arrow key pressed event whenever the user pressing any key.

Actually i was building a new keyboard hardware with its device driver, but as i don't have time to share a video with you now about my new se7so keyboard, i thought about something interesting to share with you :).

As the user will be forced to write from right to left, instead of writing from left to right, I was trying to login my machine and because of the module was loaded i was forced to write my password from right to left :D "what comes around, goes around".

Enjoy the video ;).

## Monday, December 3, 2012

### Apache JMeter along with jsf pages

Two days ago i was allocated a task from my team leader to load test our new web application after deploying it on the production environment. We used lots of open source technologies in this application but the main challenge was to configure JMeter to pass the JSF login page that contains a button posts to a JSF action.

If you don't know about JMeter, you may need to read this topic on my blog.

Step 1: Creating a Thread Group with the following configuration:

Ramp-Up Period (In seconds): 2
Loop count: 2

Step 2: Creating Once Only Controller under the Thread Group with the following configuration:

Step 3: Creating an HTTP Request under the Once Only Controller with the following configuration

Server Name: my.server.name
Port Number: 80
Implementation: Java
Protocol: HTTP
Method: GET

Step 4: Creating an XPath Extractor to extract the view state of the login button

Name: Xpath Extractor
ِApply to: Main sample only
Reference name: myViewState
XPath query: //input[@id='javax.faces.ViewState']/@value

Step 5: Creating an HTTP Request to submit credentials to the jsf login action with the following config

Name: Auth
Server Name: my.server.name
Port Number: 80
Implementation: Java
Protocol: HTTP
Method: POST
Use KeepAlive: True
Parameters:

j_username : admin : encode = true : include equals = true
j_password : password : encode = true : include equals = true
javax.faces.ViewState : \${myViewState} : include equals = true

Step 6: In the same level of the Once Only Controller create an HTTP URL Re-writing modifier to extract the session id with the following configuration

Name: HTTP URL Re-writing Modifier
Session Argument Name: JSESSIONID
Cache Session ID = true

Step 7: Create an HTTP Cookie Manager to save the session id and sending it along with all the requests