Wednesday, April 23, 2014

CS1: Given an array of integers return top-n most frequent integers in the array.

Language: Java

Data structures:
Map<K, V>
Map.Entry<K, V>
MinMaxPriorityQueue<E> // Used as a min-heap.

Algo idea O(n):
Input: Integer[] inputValue;

1. From the given array of values create a map of (value, freq)

    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (int i = 0; i < inputValue.length; i++) {
      Integer value = array[i];
      Integer freq = map.get(value); // get() returns null if a key does not exist.
      map.put(value, freq == null ? 1 : freq + 1);
    }

2. Populate the priorityQueue (ordered based on entry.getValue() i.e., freq) with at most n elements

    MaxMinPriorityQueue<Map.Entry<Integer, Interger>> priorityQueue =
        MaxMinPriorityQueue.create(); // You may need to wrap Map.Entry into a Comparable.
   
    for (Map.Entry<Integer, Interger> entry : map.entrySet()) {
      if (priorityQueue.size() < n) {
        priorityQueue.add(entry);
      } else {
        Map.Entry<Integer, Interger> minEntry = priorityQueue.peekFirst();
        if (entry.getValue() > minEntry.getValue() ) {  // compare frequencies.
          // Replace min element with current element.
          priorityQueue.removeFirst();
          priorityQueue.add(entry);
      }
    }

3. Output the result:

    for (Map.Entry<Integer, Interger> entry : priorityQueue.iterator()) { output(entry.getKey()); }

Monday, January 27, 2014

How much people in the world

We all talk about population explosion. But wait a second. Do we have any idea on how much people are there in the world if we put them all together. Here is a quick back of the envelope calculation:

A fair packing can be 3 people / 1 meter
i.e., 9 people / 1 sq meter
= 10 people / 1 sq meter
= 10 people / (10^-3 x 10^-3) sq km
= 10 people / 10^-6 sq km
= 1 people / 10^-7 sq km

Total population of world
= 7 billion people
= 7 x 10^9 people
= 7 x 10^9 people * 10^-7 sq km / 1 people
= 700 sq km
= (27 km) square
= Size of city of Mumbai !!

Monday, January 13, 2014

Latest realizations about my core questions: Who am I, Why am I living, What is the purpose of this creation?

Acknowledgements

The following ideas are extracted from collective human knowledge. Thanks to every thoughtful person of all time. Btw, my friends Shaan and Tapas are worth explicit mentions for all the discussions and motivations culminating into this write up.

Base Observations

  • Theories are created to fit facts (they can change if facts suggest something else).
  • Language is about expressing relationships: in spatial or temporal axis.
  • Mathematics is just another language. Each mathematical equation is a relation between pertinent quantities. Quantities are any measurable physical aspect. Math's emphasis on measurement, makes communication precise and portable.
  • Science is about observation, measurement, and expressing those findings in language. The goal is to communicate the knowledge and manage the nature and natural resources.
  • Stuffs start with observation and some mental model with assumptions to fit the observations (assumptions are sometimes difficult to express e.g., think about Newton's law and ask what is mass, what is force). 

The idea

Assumptions: Any scientific study starts with some observation followed by some mental model with assumptions. For the current context the observation is our life and assumptions are "Soul" and that there is a purpose for each soul.

Soul: We are souls. Souls are birth-less, death-less, time-less, eternal, imperishable, indestructible, cannot be cut, cannot be burnt, cannot be wet, cannot be dried. They are impersonal, inconceivable, unchangeable, always happy, full of energy, immune to pain, do not need sleep, do not feel hunger.

Body: The body is an instrument given to the soul to interact with the nature. To experience - to express its desire, to taste, to feel, to see, to move. Without this material body, soul will not be able to interact with this material world. Body is built out of material nature, it's a temple of the soul. It has many cravings (hunger, sleep, etc). If we can take proper care of it, it can last longer. But like any other material stuff, over time it will wear-n-tear and at some point of time will come to the end of usable life.

Analogy: Think about a multi-player computer game system. To interact with the system we need the input/output devices (keyboard, mouse, monitor). Body is like an input/output device, the gamer (we) is the soul.

Purpose: Soul has some purpose. To satisfy that purpose it needs flexibility. To gain flexibility it needs exercise.

Gym: This material universe is the gym for the souls. Souls are given positions based on their level of flexibility or maturity. The Supreme Design makes sure that each and every soul gets required exercise. In gym we build our body, and though we can not build body for others still we can create a blissful infectious environment that motivates others around us to exercise. Similarly in life, the best thing we can do, is to perform our duties and at the same time create a powerful field around us that radiates joy to everyone. Btw, if in one life we can not get enough exercise, no worries, we will come back. But each birth-death has associated friction, so better if we can maximize our experience in one life.

Hence in this material world

  • As long as we are in the material body "Work is GOD", work is how our soul gain experience. Let us perform our duties, our responsibilities the best we can. Action reduces fear, so ACT.
  • Make sure we create a positive happy environment around us that fills everybody with joy and motivates people to work.
  • Always try to keep our head high (as much as possible): in postures we assume, in jobs we perform, in responsibilities we commit.
  • Take proper care of our health, the body will run longer smoothly and we can concentrate more on the exercise of the soul rather than spending time in fixing the body.

Some tips for good health

  • Drink 3 to 4 liters of water everyday, say 500ml, 1 hour before and 1 hour after each major food. Water helps to drain toxins out of the body. Add 1 tsp organic apple cider vinegar (with mother) in the morning.
  • If possible drink bone soup (or similar stuff rich in minerals, collagen and gelatin) couple of times a week. Its a super food for healthy gut and body.
  • Double the heart rate, at least for 15 minutes everyday (walk, push ups, sit ups). That will force blood to rush impenetrable corners in our body, flushing toxins and promoting healing.
  • Brush and floss teeth every night.
  • Be thrift in spending the procreative vital energy in you.