Tuesday, July 31, 2018

Let us take a break to discuss web application routes. In Java Spring application, we can use the @Path("/resource") identifier to define the routes. These paths become exceedingly easy to use since they are defined only once.  The http/https traffic coming to the server may need to be routed to the service via nginx or other web configurations. Setting up traffic to the paths therefore easy. However, setting up access requires a filter to be invoked. This filter takes the username and the role and raises exception if the looked up role does not match the expected role. Typically this is the same criteria for all of the paths. Therefore applying the filter to the base class from which all other services derive may be sufficient for securing access to all the endpoints. There are annotations we can apply to individual paths to bless the role for that path
#codingexercise
Check if a number has the same count of set and unset bits.
boolean hasEqualSetAndUnsetBits(int padded)
{
    int set = 0;
    int len = 32; // or suitable fixed-length
    while (padded)
    {
        if (padded & 0x1) {
             set++;
        }
        padded = padded >> 1;
    }
    return (set == (len-set));
}
#codingexercise
Find the number of subsets with sum divisible by m in an array of positive integers.
Solution: We maintain a dp table of size sum x n where n is the number of elements in the array.
We initialize the dp array with 1 for sum 0 since it is always possible. For each of the elements by itself, we increment the dp table with the sum as that element. For all possible values from 1 to sum, if the current element had its dp value incremented for this value of sum, we increment the dp entry for the next element as well at this sum value. Similarly we also increment the dp value for the next element at the cumulated value of the sum and the current element. The dp table increment for the sum plus the element may need to be determined to be within the bounds.
When we fill the dp table this way, we just have to cumulate all those dp entries where the sum value is divisible by m.

No comments:

Post a Comment