Thursday, October 12, 2017

#puzzle
 A rearrangement of the letters of a word has no fixed letters if, when the rearrangement is placed directly below the word, no column has the same letter repeated. For instance, the blocks of letters below show that ESARET is a rearrangement with no fixed letters of T E R E S A, but R E A S T E is not.
T E R E S A              T E R E S A
E S A R E T              R E A S T E
How many distinguishable rearrangements with no fixed letters does T E R E S A have? (The two E’s are considered identical.)
Since we have been given a small search word, we could easily enumerate the combinations. For example, we can place the 2 E's in different position as follows:
E ? E ? ? ?
E ? ? ? E ?
E ? ? ? ? E
? ? E ? E ?
? ? E ? ? E
? ? ? ? E E

This yields 6 ways to do so. One way to think about this is that we are moving each of the E's to the remaining four positions and therefore there are 4-Choose-2 ways = 6 ways.
Taking one of the six arrangements above, we can also enumerate the possible arrangements of the remaining letters. Let us say E now occupies new positions X and Y. Neither X nor Y can be in their original position no matter where we put them.  So we have to place the last two letters which we call M and N and there are only 4 positions to choose from.  M can either be moved to where N was or it can be moved to one of the original positions of E. In the former case there are three positions remaining for N so there are 3 ways to do so. In the latter case there are two E's so there are two ways each for M and for N. There fore there are a total of 3 + 2 x 2 = 7 ways to place the last two letters. Moreover X and Y can also be interchanged so we have 7 times 2 equals 14 ways. Since we are doing this for each of the 6 ways to place E's to start with, we have a total of 6 x 14 = 84 ways. It is possible to enumerate all these 14 ways for a given starting arrangement of E and see that we can repeat the same for any of the remaining  arrangements of E.

Wednesday, October 11, 2017

codingexercise
Find the sub-tree with maximum  color difference in  a 2 colored tree
We define color difference as the magnitude of the number of vertices of one color minus the number of other color vertices where each number is a positive count.
Since it is not convenient to return all the subtrees and exhaust the search space,
We will keep track of minimum and maximum color difference of two subtrees and we will color 1 or –1 so that the equal number of both balance each other out.
When we perform a depth first search, we can find the maximum of any color by assigning  1 to that color. Since depth first traversal finds subtrees we have the maximum available for a sub tree.
When we invert the color, we can find the maximum of the opposite color with a similar depth first search.
During the depth first search for every node as the starting point, to find
1) max sum sub tree, we take a node if its value is greater than 0 => sum(parent) += max(0, sum(child))
2) min sum sub tree, we take a node if its value is lesser than 0 => sum(parent) += min(0, sum(child))
Thus for each node we have a color count array . Previously visited nodes already have this counted so this is a form of overlapping subproblems which we alleviate by memoization
After each depth first search we now have a color count array of a sub-tree for which we can find the maximum
When we get the maximum of both colors this way from the color count array, we know that the maximum color count difference is between this high number and nothing. Therefore, we have the color difference we are looking for.

int maxDiff(Node root, List<int> colors, int N)
{
   var counts = new List<int>();
   dfs(root, colors, ref counts);
   int max = counts.Max();
   colors.ForEach (x => Invert(x));
   dfs(root, colors, ref counts);
   max = Math.max(max, counts.Max());
   return max;
}

Tuesday, October 10, 2017

An essay in the system design of SignIn workflows. 
Most web applications require to authenticate a user via signing in. Such applications of all sizes usually delegate the task to a membership provider which takes different forms. We talk about some of these manifestations and their pros and cons. The task inherently may involve something as simple as looking up a user account in the database, or looking it up in the Active Directory or looking it up using API calls for industry accepted authentication and authorization protocols
Fundamentally the account is needed for a session on the application so that the user can seamlessly visit one or more pages without having to authenticate on each page.  When the identity is linked to an account that spans one or more business, then the notion of a single user account is lost and we involve a pool of accounts all pertaining to the same user.
User account when stored and indexed is easy to lookup and results in faster retrieval. Social engineering applications use a mix of relational as well as NoSQL stores for the purposes of storing accounts and their resources respectively. Typically they have a sql connector over their NoSQL so the querying can be expressed in the same language. They need to store user resources such as chat, images and logs in big data because the volume is large and data processing is batch oriented. With user accounts, even the logs are interesting for audit purposes and consequently important to store.
SignIn Applications can be categorized as front-end oriented, middle-tier motivated or back-end performance driven although they may use all three to some degree. When these applications are front-end oriented, we often see the use of Model-View-Controller kind of patterns that evolve to include viewmodels. Since there are re-usable parts of workflows for signin across applications like registering, login, password retrieval, using one-time passcodes, verifying with the help of email or phone and many  more, they often take the shape of widgets where workflows are described in the form of widgets which can be composed or standalone.  Front-end heavy applications pay special attention to consistency and user experience across different use cases.
Middletier motivated applications can be recognized by their dependencies on services or service oriented architecture to be precise. Every task small or big is now a service call away and may exist even behind a gateway or an information bus. These applications were earlier designed with a hierarchy of individual services organized in object-oriented terms while each retaining its independence with its own address binding and contract definitions. More recently, this organization has become flat, fine grained and with the notion of microservices and perhaps serverless computing. When services are involved, applications tend to bring standard enterprise practices such as those describe by the enterprise application blocks where logging and metrics are part of the service implementations. Services also enable independence from data store concerns which brings us to the third category.
Indeed some of the sign-in applications especially for web-applications implemented using well known frameworks make use of data stores in the form of membership providers where the store itself performs search and retrieval using a standard interface that web-application frameworks expect. This has made it convenient for the application to reduce its footprint of signin logic so that the data is kept organized and there is little code involved. Such stores come with the benefit of being the single source of truth at any time.
Thus we see that there are different forms of SignInApplications and their implementations.



#puzzle
How many positive five-digit integers are there consisting of the digits 1, 2, 3, 4, 5, 6, 7, 8, 9, in which one digit appears once and two digits appear twice? For example, 41174 is one such number, while 75355 is not.
If one number appears once, it can be chosen in 9Choose1 ways.
The other two digits that appear twice can be chosen in 8Choose2 ways.
The number of permutations of these selections is given by
permutations based on number of digits  divided by permutations based on repetitions

= 5! / (2!2!1!) = 30

Therefore the total number of numbers is 9Choose1 x 8Choose2 x 30 = 7560 possible numbers that satisfy the given criteria

Monday, October 9, 2017

#puzzle
The integer 72 is the first of three consecutive integers 72, 73, and 74, that can each be expressed as the sum of the squares of two positive integers. The integers 72, 288, and 800 are the first three members of an infinite increasing sequence of integers with the above property. Find a function that generates the sequence and give the next three members.  
We notice that 72 is 6^2 + 6^2 
288 is 12 ^12 
800 is 20^2 + 20^2 
We also note that the next to next number for 72 is merely (6-1)^2 + (6+1)^2 because that will add two 
Similarly the next to next number for 288 is (12-1)^2+(12+1)^2 because that will add two 
And the next to next number for 800 is (20-1)^2+(20+1)^2 
So the middle number must also have a pattern because the function exists. Given that we have three integers to see the pattern, we can generalize it with n 
Consequently the function can be expressed with patterns for all three terms. 
Let us take a closer look at the middle number
73 = 3^2+8^2 = (6-3)^2+(6+2)^2
289 = 8^2 + 15^2 = (12-4)^2+(12+3)^2
801 = 15^2 + 24^2 = (20-5)^2 + (20+4)^2

Therefore given 2n^2 as the first number, the second number is (n-k-2)^2+(n+k+1)^2 and the third number is (n-1)^2 and (n+1)^2 where k is the count of sequence printed starting with 1.
Since the second number is the first number plus 1 we have an equation in n and k
as 2n^2+1 = (n-k-2)^2+(n+k+1)^2
   n = k^2+3k+2
Given k = 4, 5, 6 the next three numbers are 1800, 3528, 6272

Sunday, October 8, 2017

Today we look at another puzzle. Let f(n) be the number of ones that occur in the decimal representations of all the numbers from 1 to n. For example, this gives f(8) = 1, f(9) = 1, f(10) = 2, f(11) = 4,  and f(12) = 5. Determine the value of f(10 to the power 100) written as f(10^100).
Since the number of 1s are easy to enumerate, we simply list the pattern we see:
f(10^1) = 2 = 1 + 1 ( we have to add 1 for the numbers that start with 1 and are followed by consecutive zeros as the last number for the given range)
f(10^2) = 21 = 20 + 1 ( 9 horizontal 1 and nine vertical 1 + both together + 1 which we calculate as
                                        1 times 2 Choose 1 times 9^1+
                                         2 times 2 Choose 2 times 9^0) +
                                         1 for the number 1 and trailing zeros.
f(10^3) = 301 = 300 + 1 (Out of three digits, numbers with only 1 is  1 times 3 Choose 1 times 9^2)
                                        (Out of three digits, numbers with two ones is 2 times 3 Choose 2 times 9^1)
                                        (Out of three digits, numbers with three ones is 3 times 3 Choose 3 times 9^0)
                                         +1 for the number 1 and trailing zeros.
Therefore we can start writing the combinatorics for 10 power 100
f(10^100) = 1 times 100Choose1 times 9Power99 +
                         2 times 100Choose2 times 9Power98 +
                          3 times 100Choose3 times 9Power97 +

                          99 times 100Choose99 times 9Power1 +
                          100 times 100Choose100 times 9Power0 +
                           1 for the number 1 and all trailing zeros
                 =

We can express this as 1+Sum(i times n-Choose-i) times Power(9, n-i)
               



Saturday, October 7, 2017

Today we look at a new puzzle:
 A transposition of a vector is created by switching exactly two entries of the vector. For example, (1,4,3,4,2,6,7) is a transposition of (1,2,3,4,5,6,7). Find the vector X if S = (0,0,1,1,0,1,1), T =(0,0,1,1,1,1,0), U=(1,0,1,0,1,1,0) and V=(1,1,10,1,0,1,0) are all transpostions of X Describe your method of finding X.
Intuitively many of the positions in a vector will remain unchanged if only two of the positions are exchanged in a transposition. If we are given a set of transpositions, a majority of them will share the common positions. Thus if a position has three or four occurrences of 1 in the given four transpositions, it is highly likely that the X also has 1 in those positions. Similarly a position with one occurrence among all the transpositions indicates a zero in the corresponding position with X.
We therefore start with X as follows:
0011011
0011110
1010110
1101010
?011?10 - X
With this candidate as X and switching the position of ? with the occurrence of extra 1 in a given transposition we eliminate the occurrence of ? and replace it with 1 or 0. In this case, it results in X as (1,0,1,1,0,1,0)
Alternatively we can also work through the problem in a systematic manner.
We enumerate all possible transpositions of S, T, U, and V in four columns by exchanging only two elements from their corresponding vector. Then we look for the vector that is common to all the possible enumerated transpositions. The nice thing about this method is that we can reduce the search space as we move from the transpositions in one column to the next and the number of candidates reduces to finally one in the last column.  Given that there are 7 positions, each column may have 14 entries using the value of 0 or 1 for each of those seven positions. Candidates found in the first column will reduce to candidates found in the second column as the criteria that S and T will share the same transposition is restrictive. This further reduces as we include U with S and T and finally to the solution with S, T, U and V.

#explain ethernet protocol
The Exponential backoff algorithm is an example of the ethernet protocol
When a collision first occurs, a jamming signal prevents further data
a frame is sent again immediately or after 51.2 micro-seconds called an interval
If it fails, the frame is sent again in multiples of that interval
If it still fails, it is resent after a multiple that is in an integer between 0 and 7 (one less than a power of two)
If it fails after nth attempt, the frame is sent again at a multiple k where k is between 0 and one less than the nth power of 2

Friday, October 6, 2017

Today we review technical aspects of Gigya. It is a socialization platform for companies. It provides websites and apps with a complete social infrastructure. It integrates three critical functionalities for the end-user:
1) a framework for social login which is their signature social identity management.
2) shares of media by end users to socialize
3) a plugin to socialize

Their framework includes a gamification layer and an analytics layer. The former is used to reward users and build loyalty.

When a company implements Gigya, it makes the following considerations:
1) provisioning social login to enable users to easily login across sites, apps and devices via a single social identity
2) provisioning identity storage which provides a unified view of consumer identity across channels that can be seamlessly integrated into 3rd party platforms.
3) enabling comments, chats and reactions - that builds an interactive community of engagement.
4) activating a gamification system that promotes valuable user actions and rewards participation and loyalty.

By integrating social login, Gigya has shown that customers spend more time online at the site than with standard login and much more than when they are not logged in. Social logins are provided by Facebook, Yahoo, Google, Twitter and LinkedIn sites in that order of popularity with Facebook being the single most dominant provider.

From their plugin interaction, they found that the average time spent on-site was most when users were leaving comments, followed closely by when they were using newsfeeds and shares. The number of page views was also similarly higher to about 11 for comments, shares and newsfeeds in general.

The tenets of the CIAM solution as per Gigya remain as follows:
1) a specialized cloud based system to manage customer identities is necessary as identity is a resource unlike any other
2) Identity may have a notion of pool for the same customer to access different sites.
3) businesses adapting to the digital marketplace are opting to hand off CIAM functionality
4) CIAM platforms are built on user experience as identity belongs to the user.
5) CIAM platforms must have flexible implementations as users and sites may choose different ways to interact and login.
6) CIAM platform must be scalable where millions of logins for cloud based retail sites is not uncommon. As a consolidator of services specializing is in something as critical as identity, we cannot ignore performance.
7) CIAM platform must secure their APIs as it is the most visible and customer facing APIs for a retail site.
8) Finally, a specialized CIAM solution gets you to market in a fraction of the time of custom deployments
#puzzle
Given a set of points in a plane, we make perpendicular bisectors of the line segments for connecting every pair of those points.Then we count the number of points in which the distinct perpendicular bisectors constructed this way intersect each other. For twelve points on the plane, the maximum possible number of intersection points is 1705. What is the maximum possible number of intersection points if we start with thirteen points ?
If we have n points, the maximum number of lines is nChoose2  = n(n-1)/2
Each of these lines has a perpendicular bisector and we have m lines
The maximum number of intersection points for  m lines is mChoose2 = m(m-1)/2
But three points form a triangle and in a triangle the perpendicular bisectors intersect at a common point. Therefore we must have to reduce the redundant 2 times nChoose3 from the total nChoose2 points.
Therefore the maximum number for m lines is mChoose2-2(mChoose3) where m = n(n-1)/2
For 13 this gives 2431.