Tuesday, December 24, 2013

Consider a class with utility methods. In this case instantiating an instance every time we need those methods, does not make sense. So what exactly should be done.

Should we go with abstract class? An abstract class may not make sense as we do not expect derived classes to provide any implementation. We can go with a static class (Here I mean a class with static methods).

So, When do we need to write a static class?
When we have a bunch of utility methods and instantiation of the class seems nonsensical.

So what is the best way to write a static class?1Enforce noninstantiability by using private constructor (from Effective Java, Item 4)

Attempting to enforce noninstantiability by making a class abstract does not work.

A default constructor is generated only if a class contains no explicit constructors, so a class can be made noninstantiable by including a private constructor:

So we need: A class with private constructor and static methods.

As the constructor is private it is not accessible to outside world so there can not be any derived class. Though not needed, we can also make the class final as there is no harm in being explicit.

All static methods (in fact all the methods) and static variables are stored in the PermGem section of the heap, since they are part of the reflection data (class related method and not instance related). The variables (or variable definitions) are store in PermGen section but the objects they refer to get stored somewhere on the heap. More can be found here.

More about garbage collection and memory allocation can be read here and here.

Monday, December 16, 2013

We are given two linked lists that intersect at a point and then become one. The number of nodes in each list before they intersect are unknown and they may be different (m and n say). We need an algorithm to find the intersection point. This can be shown by this image:

The simplest is to go with brute forcewhere
each node of the first is compared with every other node of the other list. The
matching node pointers will lead us to the intersecting node.

Time complexity: O (mn) Space Complexity: O (1)

Can we use Hash Table? Yes.

The approach is:

Select
the list having less number of nodes and store it in hash table.

Traverse
the second list and check whether the same node pointer exists in the hash
table.

If it
does we get intersection point.

Time complexity: Time for creating the hash table + time for
scanning the second list

O (m) + O (n)

Space Complexity: O (n) or O (m)

Can we use stacks to solve it? Yes. The approach is as:

Push
both the lists on stacks. The top of both will have the last element of the
lists.

Compare
the top elements on two stacks.

If they
are same pop them and store them.

If they
are not, it means the common element has just popped out in previous step.

The
value popped out in previous step can then be returned.

Time complexity: O (m
+ n) Space complexity: O (m + n)

Can we use the first repeating number? Yes.

Create
an array and keep all the next pointers of both the lists in the array.

In the
array find the first repeating element in the array. The first repeating number
is the intersection point.

Time complexity: O (m
+ n) Space complexity: O (m + n)

Do we have any other approach? Yes by combining sorting
and searching.

Create
an array and keep all the next pointers of first list in the array.

Sort it
(assume binary sort is used O (logn) ).

Then for
each element in second list, search it in this sorted array.

If we
find it then it is intersection point.

Time complexity: Time
for sorting + Time for searching = O (Max (mlogm, nlogn))

Space complexity: O (Max (m, n))

Do we have any efficient approach? Yes.

The approach is to
compute lengths of the two lists in linear time. Then advance the longer list
by difference in length of two lists and then start comparing.

Saturday, December 14, 2013

So if you do not know what do I mean by partition of a number, then here it is:

A partition of a positive integer n is, a way of writing n as a sum of positive integers. Two sums that differ only in the order of their summands are considered to be the same partition (For example 1+2+3 and 1+3+2 are same). If order matters then the sum becomes a composition.

Partition(4) = 5

4
3 + 1
2 + 2
2 + 1 + 1
1 + 1 + 1 + 1

Then we have another related term Restricted Partition which is partition having some constraints. For example partition having only odd numbers, partition having only 1 and 2 etc.

In number theory, the partition function p(n) represents the number of possible partitions of a natural number n, which is the number of distinct ways of representing n as a sum of natural numbers.

Partitions can also be visualized by Young diagrams or Ferrous diagrams.

Young Diagram makes use of boxes and Ferrous Diagrams make use of dots to represent the same information (as shown below).

The partition 6 + 4 + 3 + 1 of the positive number 14 can be represented by:

So the problem is to write an algorithm that will print all the partitions for a number. There is one very good implementation at Princeton university page under the topic of recursion. Here it is reproduced: