Saturday, April 11, 2015

How to Remove Given Character From String in Java - Recursion

Write a program to remove a given character from String in Java. Your program must remove all occurrences of given character. For example, if given String is "aaaaa" and String to remove is "a" then output should be an empty String. Similarly if input String is "abc" and character to remove is "b" then your program must return "ac" as output. You are not allowed to use any JDK method or third party method which solves this method directly, but you can use basic String manipulation method like indexOf(), toChar() or substring() from java.lang.String class. Most important thing is, you must code the logic to solve the problem by yourself. You should also write the solution using both Iterative and Recursive algorithms. An Iterative algorithm is the one which make use of loops e.g. for loop, while loop or do while loop, which recursive solution should not use any loop. Now let's think how can we solve this problem? Most simple solution which comes in my mind is to iterate over String by converting into character array and check if current character is same as given character to remove or not, if it is then ignore it otherwise add character into StringBuilder. At the end of iteration you will have a StringBuilder with all character except the one which is asked to remove, just convert this StringBuilder to String and your solution is ready. This solution should have space complexity of O(n) because you need an extra buffer of same size as original String and time complexity will also be O(n) because you need to loop over all elements of String. Can you make it better? because this solution will not work for large String, especially if you have memory constraint. Now, let's see how to remove character from String recursively?BTW, this is one of the good coding question and has been asked in companies like Goldman Sachs, Amazon and Microsoft. So if you are preparing for programming job interviews, make sure you include this question in your list as well.

Wednesday, April 8, 2015

3 Ways to Prevent Method Overriding in Java - Private, Static and Final

Every Java programmer knows that final modifier can be used to prevent method overriding in Java because there is no way someone can override final methods; but, apart from final modifier, is there any other way to prevent method overriding in Java? This was the exact question, asked to one of my friend in a recent Java interview at one of the leading Investment bank. I guess, he was lucky to get this question because he already knows those tricks and was able to answer it quickly. When he told me about his experience,  I didn't understand why someone ask this question? What is the purpose? What they were trying to find out? I can understand if they have asked what's the best way to prevent method overriding in Java, or what is the risk of overriding? Anyway, It gives me an opportunity to recap some of the other technique (syntactically) to prevent a method from being overridden and write this blog post to share some mystery tricks from Java world. Some of you might be aware of those non obvious way to prevent method overriding, but if you don't, here is your opportunity to learn.

Monday, April 6, 2015

Error: Could not find or load main class in Java [Solved]

Error: Could not find or load main class HelloWorld comes when you are trying to run your Java program using java command with main class as HelloWorld but Java is not able to find the class. In order to solve this error, you must know how Java find and loads the classes, that's little bit complex topic for beginners, but we will touch same base here. For curious reader, I would suggest to read my post How Classpath works in Java, a must read for beginner. For now you just remember that there is an environment variable called CLASSPATH which includes directories where Java looks for all class files and if it doesn't find your main class there then it throws "Error: Could not find or load main class XXX", where XXX is the name of your main class. Since many Java programmer are now started programming using Eclipse they face this issue when they first try to run their Java program from command line. In Eclipse its easy to compile and run program because Eclipse takes care of all Classpath setup, but when you run your Java program from command line, CLASSPATH environment variable comes in picture. Personally, I don't like this environment variable and doesn't define in my environment variable, because its confusing and source of so many classpath related issue. Instead I use -cp or -classpath option with java command to run my program. This way you always know which JARs are included in your classpath. For beginners, another important thing to understand is difference between PATH and CLASSPATH, you must know that PATH is used locate system executable, commands or .exe, .dll files (in Windows) and .so files (in Linux). It is also used to locate native libraries used by your Java program. While, CLASSPATH is used to locate class file or JAR files. Its Java class loader who looked into CLASSPATH for loading classes. Coming back to problem in hand, if you are a beginner in Java, who are able to run the program from Eclipse but getting "Error: Could not find or load main class HelloWorld" when trying to run the same program from command line then follow the steps given here to solve it.