Thursday, July 31, 2014

21 Frequently Asked Java Interview Questions and Answers

If you have been to couple of Java interviews then you know that there are some questions which keep repeating e.g. difference between == and equals() method and may of it's popular cousins like HashMap vs Hashtable, ArrayList vs LinkedList, difference between equals() and hashCode(), or difference between Comparator and Comparable in Java. I call them frequently asked Java interview questions, and I suggest every Java developer to make a list of them for their own reference and revision. I am sure many Java programmer already has such list of questions handy, if not this is a good time to find and make your own list. These are the questions which you simply can't afford to miss, especially at freshers level. They appear at various stage of Java interviews. Most likely you will see them on telephonic round, where Interviewer just want to filter candidates between who knows Java and who doesn't. Good thing about them is that they are so common that everyone knows about it. Though for freshers it could be little difficult. but as your experience grows these frequently asked questions become much easier to answer. Some programmers also prefer to collect frequently asked Java questions based upon topics e.g. common questions from threads, strings, collections and other popular Java interview topics, some of them are already shared by me. In this list, I am sharing some of the most frequently asked questions from Java interviews. By the way, when you look the list, you will see some of the classics are missing e.g. difference between String and StringBuffer, but there are many like that, and that is task for you to collect as many as possible and keep them handy to avoid searching from them just before interview. I will also add some more questions on this list but for now let's start with these 21 questions.

Tuesday, July 15, 2014

QuickSort Example in Java using Recursion - Sorting Algorithm Implementation

Quicksort is one of the very popular sorting algorithm in programming, often used to sort large list of numbers. Though their are numerous algorithm available to sort list of objects, including integer, string and floating point number, quicksort is best for general purpose. It's a divide and conquer algorithm, where we divide the given array with respect to a particular element, known as 'pivot' such that the lower partition of the array are less than the pivot and upper partition elements of the array are higher than the pivot. Quicksort is also one of the best example of recursion. It's naturally recursive, because it sort the large list by dividing into smaller sub-list and then applying same algorithm on those. Base case of recursion is when list contains either one or zero element, in that case they are sorted. Quicksort is well ahead with primitive sorting algorithms e.g. insertion sort, selection sort and bubble sort. Average time complexity of quicksort is O(nlogn), while in worst case it's performance is similar to bubble sort i.e. O(n^2). Apparently worst case of quicksort is the best case of insertion sort, where they have to sort an already sorted list. In this article, we will learn how to implement quicksort algorithm in Java using recursion. We will also learn how quicksort works, and how it sorts large list of unsorted number. In last section, we will see some important things about quicksort.

Tuesday, June 24, 2014

Write a Program to Find Sum of Digits in Java

One of the common programming practice question thrown to beginners is to write a program to calculate sum of digits in a integral number. For example, if input is 123456 then output or sum of digit is (1+2+3+4+5+6) = 21. Additional condition is you can not use any third party or library method to solve this problem. This program is not as simple as it looks and that's why its a good exercise, you must know some basic programming techniques e.g. loops, operators, and logic formation to solve this problem. Let's see how we can solve this problem using Java programming language. In order to calculate sum of digits we must get digits as numbers. So your first challenge is how do you get the digits as numbers?  How do we extract 6 out of 123456? If you have done exercises like palindrome check or reversing number, then you should know that there is very old technique of getting last digit from a number by using modulus operator. If we do 123456%10 then we will get 6, which is last digit. In order to get all digits we can use a loop, something like while loop. Now our next challenge is how do we reduce number in each iteration so that our loop will finish as soon as we are done with all digits of number? Now coming from same palindrome problem, you can use technique of dividing number by 10 to get rid of last digit or reduce it by factor of 10. For example 123456/10 will give you 12345, which is one digit less than original number. So you got your end condition for while loop, check until number is not equal to zero. These two techniques are very important and can be used in variety of problem, so always remember these.

Java67 Headline Animator