Posts

Showing posts from 2016

Management lesson from The Bridge Riddle

Most of us come across the Bridge Riddle sometime before, it is all about optimizing time to cross the bridge. Here is the riddle You, an assistance, a jenitor and a scientist have to cross a wooden bridge in 17 minutes . The bridge is weak and only able to carry the weight of two of them at a time. Because they are in a rush and the light is fading they must cross in the minimum time possible and must carry a torch (flashlight,) on each crossing. They only have one torch and it can’t be thrown. Because of their different fitness levels and some minor injuries they can all cross at different speeds. You can cross in 1 minute, assistance in 2 minutes, the janitor in 5 minutes and the scientist in 10 minutes. Below TED Ed video explains and solve this riddle https://www.youtube.com/watch?v=7yDmGnA8Hw0 As explained in the video the key decision is to reply on the assistant who is comparatively less efficient than “You”, the assistant takes double time t

All about Java stream collect offering

Lets start with a problem statement “Provided collection of employees, we need to find average, max and min age of employee by county” public static void avgMaxMinEmployeeAgeByCounter() { String AVG = "AVG"; String MAX = "MAX"; String MIN = "MIN"; String SUM = "SUM"; String COUNT = "COUNT"; List<Employee> employees = Employee.EMPLOYEES;//LIst of all employees, pojos // Mapping country name to average, max, min age of employee Map<String, Map<String, Double>> summary = new HashMap<>(); for(Employee e: employees) { if(summary.containsKey(e.getCountry())){ Map<String, Double> map = summary.get(e.getCountry()); map.put(SUM, (map.get(SUM) + e.getAge())); map.put(COUNT, map.get(COUNT) +1); map.put(MAX, map.get(MAX) < e.getAge() ? e.getAge():map.get(MAX)); map.put(MIN, map.get(MIN) > e.getAge() ? e.ge

Utilizing ES6 Generator

I am going through Mozilla documentation about the Iterator and Generator concept introduced in ECMAScript6. Here is the overview as per Mozilla JS guide Processing each of the items in a collection is a very common operation. JavaScript provides a number of ways of iterating over a collection, from simple for loops to map() and filter(). Iterators and Generators bring the concept of iteration directly into the core language and provide a mechanism for customizing the behavior of for…of loops. This post is not about how it works, it is better explained at Mozilla JS guide . I have tried to apply the JS generator for one of the our day to day code. I think it is better suitable for lazy traversing and evaluating each item and proceed further if required. A common example comes in my mind is the tree traversing. I have implemented Depth First Search ( pre-order) using generator. Consider the scenario where you need to traverse the tree node, evaluate/process t