Thursday, July 17, 2014

9 Difference between TCP and UDP Protocol - Java Network Interview Question

TCP and UDP are two transport layer protocols, which are extensively used in internet for transmitting data between one host to another. Good knowledge of how TCP and UDP works is essential for any programmer. That's why difference between TCP and UDP is one of the most popular programming interview question. I have seen this question many times on various Java interviews , especially for server side Java developer positions. Since FIX (Financial Information Exchange) protocol is also a TCP based protocol, several investment banks, hedge funds, and exchange solution provider looks for Java developer with good knowledge of TCP and UDP. Writing fix engines and server side components for high speed electronic trading platforms needs capable developers with solid understanding of fundamentals including data structure, algorithms and networking. By the way, use of TCP and UDP is not limited to one area, its at the heart of internet. The protocol which is core of internet, HTTP is based on TCP. One more reason, why Java developer should understand these two protocol in detail is that Java is extensively used to write multi-threaded, concurrent and scalable servers. Java also provides rich Socket programming API for both TCP and UDP based communication. In this article, we will learn key differences between TCP and UDP protocol, which is useful to every Java programmers. To start with, TCP stands for Transmission Control Protocol and UDP stands for User Datagram Protocol, and both are used extensively to build Internet applications.

Monday, July 14, 2014

How to Find Largest of Three Integers in Java - Algorithm, Logic Example

One of the classical program to build programming logic is, write a program to find largest of three numbers. I am sure many of you have already done this exercise in variety of languages including C, C++, C#, JavaScript, Perl, Ruby, PHP etc. This time we will do it in Java. We will first learn the logic by understanding flowchart of largest of three numbers and then we will implement solution using ternary operator in Java. I love this program for its sheer simplicity and how it can help beginners to build logic. As always, you are not allowed to use any library function which can solve this problem directly, your main task is to build logic using primitive language tools e.g. operators. In Java, this problem is also used to teach how ternary operator works, as one of the popular version of this require you to find largest of three numbers using ternary operator. This problem is in similar category as how to determine if number is prime. This Java program finds largest of three numbers and then prints it. If the entered numbers are unequal then one version of this program returns Integer.MIN_VALUE, while other return the number itself. By the way, the method used here is not general, and doesn't scale well for many numbers. For example, If you want to find out largest of a list of numbers say 10 integers then using above approach is not easy, instead you can use array data structure, and keep track of largest number while comparing with other numbers. We will also see how we can use ternary operator in Java to find biggest of three integers. I have made both method static, because they are actually utility method and only operates on their arguments, and I can call them from main method directly, without creating object of this class.

Thursday, July 10, 2014

Write a Program to Find all Armstrong number in the range of 0 and 9999 - Example

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number, since 1**3 + 5**3 + 3**3 = 153, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. Along with usual beginner exercises e.g. calculating factorial, reversing string or calculating prime numbers, this is a good exercise to build programming logic. It teaches you basic programming technique of how to use operator for something which is not obvious, for example, to solve this programming challenge, we first need to check if a number is Armstrong or  not, and to do this we need individual digits of the number. how do we do that? well there is a programming technique, which you might have learned while doing number palindrome exercise. If you modulo an integer number by 10, you will get last digit, for example 656%10 will give you 6, which is last digit of 656. Similarly to reduce the input after each iteration, you can use division operator, as 656/10 will return 65, which is without last digit. If you know this trick, it's very easy to solve any programming problem, which requires examination of individual digits.  This Java program uses same technique and compute all Armstrong numbers in the range of 0 and 999. By the way this program has different variations as well e.g. how do you find Armstrong number of four digit, as this program only calculates three digit Armstrong number. Well, for that you need to remember general definition of Armstrong number which is, An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits. So a four digit Armstrong number will be equal to sum of power four of individual digits of that number. Another interesting challenge is to write  a program which uses recursion to find if number is Armstrong or not.