Saturday 30 August 2014

Static program analysis in Java by Findbugs plugin.

Before some days, I was statically analyzing my application code with Findbugs.
During my analysis, I got many warning/bugs/points in my code. From those some are really important which I want to share....
Here are those points.

1.) Bx: Method invokes inefficient Number constructor; use static valueOf instead (DM_NUMBER_CTOR)

Nine(x) ball problem

I have 9 balls, and one ball is heavier than the others, and i have somthing to weight with but we are allowed to use it only twice. How can we know the ball of different weight ?!

Burning Rope/wood puzzle

Question:

I have some number of  non-homogeneous ropes(or woods or fuses), and each one of them needs 1 hr to be burned. Material is non-homogeneous that means if i cut the rope into two equal parts(in the term of length), it is not necessary that both burns in the same time.
(Some may ask about wood(or fuse) instead of rope... but trick is same for solving.)

Q 1.) How can i calculate 45 minutes using those two ropes?!
Q 2.) How can i calculate 15 minutes using only one rope?!

Ans 1:
  1. Fire first rope from both side (front side and rear side) and in parallel start fire in the second rope only from one side.(let say front side)
  2. When one rope will totally burned, on that time start fire in the second rope from rear part. (one rope will totally burn in 30 minutes because as we have double the speed, time taken would be half. It means as one rope needs 1hr to be burn, by doubling the speed so in 30 mins.)
  3. When second rope also totally burn, it would be exactly 45 minutes(because at the end of 30 minutes, it will burn half part, and after that we have double the speed, so at the end of 15 minutes, second rope also totally burned. So total time is 30 minute + 15 minute = 45 minute.)

Frequently asked puzzles during interview

This is a collection of some good interview puzzles which either i have faced or heard from friends.



3.) Crossing the bridge puzzle

4.) 100-coin puzzle

5.) Mislabeled jar - Jelly Beans problem

6.) Red-Blue marble - probability puzzle

7.) Measure "4 liter water" problem

8.) 25 horse, 5 tracks, find 3 fastest horses

9.) Two egg puzzle

10.) Find the celebrity in the party

11.) Find black/white pair of sock

12.) Two door, which path leads to heaven?

13.) 100 door puzzle

14.) Ant and triangle problem

15.) King and wine problem

16.) Gold bar distribution problem

17.) Inverted card puzzle

18.) Pirates profit distribution problem

List updating continuously... :)

Thursday 14 August 2014

Java Collections - HashMap vs Hastable vs Collections.synchronizedMap vs ConcurrentHashMap

This is a very famous interview topic for the experienced persons who have "really" worked in java technologies. By this question, interviewer can verify weather you have really worked with collections or not also your knowledge on synchronization problem with Hash Map (Collections).

This article can help you for the following questions.
1.) How HashMap works in java.
2.) Have you work with collection classes(HashMap) in the multi threaded environment?
     What are the problems you faced in HashMap? What is ConcurrentModificationException?
     What is fail-fast and fail-safe iterator?
     Why do we need Hashtable or Collections.synchronizedMap Or ConcurrentHashMap?
3.) Similarity/difference between Hashtable and Collections.synchronizedMap.
     If functionality of both (Hashtable and Collections.synchronizedMap) is same then why do we need both?
4.) Similarity/difference between ConcurrentHashMap and Hashtable.
     Similarity/difference between ConcurrentHashMap and Collections.synchronizedMap.
5.) When one can use Hashtable, Collections.synchronizedMap and ConcurrentHashMap.



HashMap
Hashtable
Collections.synchronizedMap
ConcurrentHashMap
Legacy class
-
Yes
-
Java 1.5 and above
Allow
null key/ value
One null key and many null values are allowed.
Not allowed

Allowed
Not allowed
(Neither key nor value)
Thread Safe
No
Yes
Yes
Yes
Lock the entire map?

Yes
Yes
No
(Locks some portion(segment) of the map)
Speed

Slow
(Compare to Concurrent HashMap)
Slow
(Compare to Concurrent HashMap)
Fast
(Compare to Hashtable and Collections.synchronizedMap)
Performance

Low
(Compare to Concurrent HashMap)
Low
(Compare to Concurrent HashMap)
High
(Compare to Hashtable and Collections.synchronizedMap)
Multiple threads can access?
(Is synchronized?)

Yes
Yes
Yes
Multiple threads can access (read) without blocking other thread?

No
(At a time, only one thread can read)
No
(At a time, only one thread can read)

Yes
(At a time, more than one thread can read)
Multiple threads can access (write) without blocking other thread?

No
No
May be
(If writers(threads) write on different segments than only)





Scalable

Less scalable then Concurrent HashMap
Less scalable then Concurrent HashMap
More scalable then Hashtable and Collections.synchronizedMap