Posts

Showing posts from June, 2021

Java Program to Find the Smallest and Largest number in an Array of Integers

Image
  Array operations on int arrays is a common Interview Question in Java. Finding the minimum number or the maximum number (smallest or largest number) in an array requires traversing the whole array and giving the output.  We'll try to solve both problems in this post. You can go through the logic and utilize it as per your requirement.  The logic is simple, we'll go through each element in a array, and compare it to a minimum or maximum number.  We've initialized the minimum/maximum number as the first element of the array. int min , max ; min = max = num [ 0 ] ; Now, if the next element in the array is lesser/greater than the stored minimum/maximum number then we'll change the mimimum/maximum number to match it.  Once we're done with the whole array, we get the minimum/maximum number of the array.  Below is the complete code for reference: /**   * Find the Largest and Smallest number in a Array of Integers   * @author computengine.com ...

Java Collections (HashMap) - Anagram Check of Two Strings

Image
  Continuing our streak with String related Java Interview Questions, we'll try to solve one of popular Interview questions for Java - find if two Strings are Anagrams . We'll be utilizing the Java Collections framework to solve this problem.  So, lets' begin! If you've not already checked Anagram definition from Wikipedia, Anagram is a Word or Phrase which is formed by re-arranging the letters of another word.  eg. GAINLY and LAYING are Anagrams, and so are FRIED and FIRED. So, if two Strings are Anagrams, the core concept is that, the letters that these Strings contain will be exactly the same, and will occur the same number of times! In order to solve this problem, we'll basically have to find out the unique letters that are occurring in a String, and on top of that, we'll have to find the number of occurrences of these letters.  Java Collections has a perfect answer to solve this problem -> Hashmaps We'll break down this problem into two parts. First...

Java Code to Check if String having Spaces is a Palindrome

Image
In one of our previous articles, we have written code to verify if a String is a Palindrome . We utilized the magic of For loops to Reverse a String and verify if its Palindrome.  Now, if the Interviewer decides to make the question a bit more difficult, the next step can be to verify that the Input String is having some Spaces, or special characters is a Palindrome or not, and still your code should be able to handle that. (Another Important Java Interview Question!) Its sounds difficult, but if you carefully look at the question, there is a simple trick to solve it. If we remove the extra characters and then compare the input string, we would be able to identify if the string is a Palindrome.  So let's get started! This is our base code to if a  String is a Palindrome .  Reversing the String is achieved in another function  Reverse a String . /**   * Check if String is palindrome   *   * @author computengine.com   * @param i...

Java Program to check Palindrome

Image
  Strings is such a favorite in Java Interviews, that you can expect at least one question on this topic. One of the important concept is Reversing a String , which we have covered earlier.  Logic can be applied using existing knowledge to solve a variety of questions. We'll see the same today.  A Palindrome is a String which if read in reverse order, is still the same. Eg. Radar, kayak etc.  See where we are coming at? So if you're asked to check if a String is a Palidrome, your knowledge of reversing a String can come in handy! So, we'll simply reverse a String, and compare if it matches our current String. That's it! For revision, our Reversal function was: /**   * Reverse a String using for loop   * @author computengine.com   * @param input   * @return  */ public String reverseString(String input) { System . out . println ( "Input String: " + input ) ; //Create a String with no data String output = "" ; //...

Create a Maven Project in Eclipse utilizing Java

Image
What is Maven? Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.  We can manage java project builds very easily using maven. Maven can help you to minimize your project and build management time and efforts. Why to use Maven? It makes project build process easy. It provides uniform builds. Managing project dependencies is easier and common for all developers in a team Facilitate easy and transparent migration to new features. It allows to build project using project object model (POM). It downloads required dependency's jar files automatically from Maven central repositories. How to use Maven in a Java Project? Java should be installed on system. IDE eg. Eclipse/Intellij should be installed on system Create project using maven-archetype-quickstart template Include the required dependency You can get more i...

Java Program to Reverse a String using For Loop

Image
  However odd it may sound, but the basic logic of reversing a String continues to be one of the most asked Interview questions. You'll find this question in various forms, with slight twists in a lot of interviews that require coding. Its an important Interview question for Java. Why? Because, it involves two very important concepts of Java, the use of Strings, and the use of a for loop.  If a recruiter wants to check your basic knowledge of coding, then String and loops are his best bet. Today we'll see how to solve this problem of reversing a String using for loop. There can be many approaches to solving this problem.  We'll try to solve it using No Extra Libraries , and our basic knowledge of For loop and String functions . We'll declare a function to take the String as an Input.  public String reverseString ( String input ) Next, we'll declare another String where we are going to store the reversed string.  //Create a String with no data String output...

The Most Comprehensive Guide to Setup a Blogger (Blogspot) blog with Custom Domain and Cloudflare CDN

Image
Blogger is one of the most popular free blogging platforms available on the web. Did I say its free? I can't emphasize it enough.  So setting up your own blog does not cost a dime, and you can keep it as long as you like. One of my blogs which I completely forgotten about, is still going strong after 7 years! I recently applied a new blogger theme to it, and it looks just like new! In case you're interested, its a blog about Shayari (poetry).  So, you have finally decided to create your own blog. That's good news! However, be aware that having your blog on a custom domain increases its credibility and ranking. Also, keep in mind that Facebook, which is the world's biggest social networking giant, does not allow you to post links for blogger blogposts.  What to do in this case? Its simple really. There is no need to pay for costly hosting services if you just want a trial of your idea, or its not going to be a business website, hobbies should be free (or really easy on t...