Data structure and algorithm interview question for java programmers

Question 1 : How to find middle element of linked list in one pass? One of the most popular question from data structures and algorithm, mostly asked on telephonic interview. Since many programmer know that, in order to find length of linked list we need to first traverse through linkedlist till we find last node, which is pointing to null, and then in second pass we can find middle element by traversing only half of length. [Read More]

Memory requirement of a program in java

Consider the class below, and notice the comment telling how much bytes they use. There is a overhead of 16bytes for maintaining class, 1 Byte for byte and boolean, 2Byte for char and short, 4 bytes for integer, 8 bytes for long and double, and some overhead for padding, to make overall memory even, to sum up: Java type Bytes required boolean 1 byte char 2 short int 4 float [Read More]

Count the number of calls of put() and get() for a map in Java

Problem Suppose you are using a map in your program, how would you count the number of times the program calls the put() and get() functions? Solution Method 1 - Implement the map interface with static count field One simple solution is to put count variables for get() and put() methods and, whenever they are called, increment the count. We can also achieve this by extending the existing library map and overriding the get() and put() functions. [Read More]

Object reflection in Java

Problem Explain what object reflection is in Java and why it is useful. Solution The name reflection is used to describe code which is able to inspect other code in the same system (or itself). For example, say you have an object of an unknown type in Java, and you would like to call a ‘doSomething’ method on it if one exists. Java’s static typing system isn’t really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called ‘doSomething’ and then call it if you want to. [Read More]

final, finally and finalize in Java

Problem What is the difference between final, finally, and finalize? Solution The keyword “final” means constant. A final variable is a constant variable. A final method cannot be overridden. A final class cannot be extended. The keyword “finally” is for catching exceptions. When all the specified type of exceptions have been caught, the finally block will be executed to handle the rest unspecified exceptions. The keyword “finalize” is for user-defined garbage collection. [Read More]

Finally block and return statement

Problem In Java, does the finally block gets executed if we insert a return statement inside the try block of a try-catch-finally? Solution The answer is yes, both in java and c#. In java, the only time a finally block will not be executed is when you call exit() OR for some reason JVM crashes, before finally is reached. The exit() call will shutdown the JVM, so no subsequent line of code will be run. [Read More]

Private constructor in Java

Problem In terms of inheritance, what is the effect of keeping a constructor private? Solution Private constructors prevent a class from being explicitly instantiated by its callers i.e. it cannot be instantiated. Additionally, because the constructor is private, the class also cannot be inherited. Benefits of this are many : classes containing only static utility methods classes containing only constants type safe enumerations singletons It is used while implementing the Singleton pattern. [Read More]

“volatile” keyword in C (and Java)

Problem What is the significance of the keyword “volatile” in C? Solution Lets under stand volatile in C Volatile tells the compiler not to optimize anything that has to do with the volatile variable.There is only one reason to use it: When you interface with hardware. Volatile informs the compiler that the value of the variable can change from the outside, without any update done by the code. [Read More]

Determine whether two lines would intersect on a Cartesian plane

Problem Given two lines on a Cartesian plane, determine whether the two lines would intersect. Solution On a Cartesian plane, if two lines do not intersect, they must be parallel with each other. Hence, their slopes must be the same. If their slopes are different, they would intersect. A line is represented as ax+by+c=0 on a Cartesian plane and the slope is given by -\frac{a}{b}. Therefore if -\frac{a_{0}}{b_{0}} \neq -\frac{a_{1}}{b_{1}} for two lines, they will intersect. [Read More]