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.

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.

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 the 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 a little bit complex topic for beginners, but we will touch the same base here. For the curious reader, I would suggest reading my post How Classpath works in Java, a must read for a 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.

FizzBuzz Solution in Java 8

FizzBuzz is one of the most famous programming question from interviews, which is generally used to weed out programmers who can't program. The problem is deceptively simple but you can't solve it if you don't know how to build programming logic. I love it just for its simplicity. Now coming back to second part of this article, Java 8. Its been more than a year, I think it was March 18 last year when Java 8 was first released and till date the most common question I have got is, how to learn new features of Java 8? e.g. Optional, lambda expression or Stream. I think best way to learn Java 8 new features is the same as best way to learn any new programming language i.e. solving common coding problem, implementing data structure, common algorithms and implementing famous design patterns. I particularly find solving coding problem and excellent way to learn and master Java 8 Streams. If you can solve problems like Fibonacci series and couple of others using lambda expression and streams, you will progress quickly. Remember, Stream is a new way to write code without using for loop. Now coming back to our FizzBuzz problem, here is the problem statement and you need to solve it using Java 8.