Thursday, August 20, 2015

Today we start reviewing the summary of another book which is fun to read. It is titled Brief by Joseph McCormack.
 But first a coding exercise
# codingexercise
Find the lowest common ancestor of two nodes in a normal tree ( not a binary tree ) where the nodes may not have links to their parents. Space need not be optimized but time has to be optimized.
There are couple of approaches as suggested by the  question
1) we find the least common ancestor as one whose children don't have both the nodes but it has both the nodes in its subtrees ( The HasNode(root, node) has to invoke multiple traversals and visits a node multiple times so this will not meet the second criteria)
2) We maintain two lists for the path to the nodes as we discover them. Then we look for the last node that is common to both the lists.
We implement the second.

Node GetLowestCommonAncestor(Node root, Node one, Node another)
{
If (root == null || one == null || another == null) return null;
var pathToOne = new List<Node>();
var pathToTwo = new List<Node>();
GetPath(root, one, ref pathToOne);
GetPath(root, another, ref pathToTwo);
return LowestCommon(pathToOne, pathToTwo);
}
 bool GetPath(Node root, Node target, ref List<Node> path )
{
if (root == null || target == null || path == null) return;
bool found = false;
path.add(root);
if (root == target) return true;
for (int i = 0; i < root.children.length; i++)
{
   found = GetNodePath(root.children[i], target, ref path)
   if (found) break;
}
if (!found)
    path.RemoveLast(root);
return found;
}
Node LowestCommon(List<Node> p1, List<Node>p2)
{
if (p1.empty() || p2.empty()) return null
int I = 0;
Node ancestor = null;
while (i < p1.length && i < p2.length)
{
if ( p1[i] == p2[i]) { ancestor = p1[i]; }
i++;
}
return ancestor;
}
Returning to the book summary:
This book talks about lean communication arguing that its the pressing need for time that drives to this new paradigm. And how do we go about doing so ? This book provides a step by step approach to getting to the point quickly and more effectively than ever. His proven BRIEF approach is  Background, Reverence, Information, Ending and Followup. The acronym doesn't merely simplifies and clarifies communication but also air tight the space in which decisions are made so that sloppiness does not creep in.
The book recognizes that in some circles a tendency to think brevity is pushing for less and runs the risk of being superficial and lacking substance. However brevity starts with deep expertise. Only with thorough knowledge can you accurately make a summary. Being brief can demonstrate how you have gone through that learning experience.
The steps in this book are described next.

No comments:

Post a Comment