Posts

Showing posts with the label Eclipse

Using Java Hashmap to Find the Biggest Number in an Array which Occurs the Same times as the Number Itself

Image
  Today we'll touch-base on an Interview Question that involves multiple important Java concepts. You deal with Arrays, For loops, If statements, While Statement, Iterators, Java Collections and Function re-usability.  So, with one single question, your Interviewer can actually assess your expertise in all these areas! Let's check it out! Today we'll try to find the Biggest Number in an Array, which is occurring the Same number of times as the number itself.  So, just being the biggest number is not sufficient! If that was what is asked, you can find the max and/or min number of an array with this article. But this is not the question. In order to qualify for an answer, the number should occur the same number of times as the number itself and be the biggest such number. Quite complicated, we should say! If you follow our blog, we have already published one article on the usage of HashMaps to find if two strings are Anagrams . We'll reuse the Hashmap concept here to seg...

Java Program to Sort an Integer Array in either Ascending or Descending Order using Java For Loops and a Function Parameter

Image
  When you're learning Java to prepare for an Interview, Arrays is a really important concept. Sorting of an array using for loops is basic concept which everyone should understand.  We've written an article earlier to teach you how to use inbuilt Java functions to sort the characters of a string . But, if you're asked to write an algorithm yourself that can achieve this, you're at the right place! We'll show how to do the sorting, and for an icing on the cake, we'll also show how to control the sorting order i.e. ascending or descending! This makes our function reusable for both purposes. So, let's get started! In order to sort an Array, we'll have to traverse through it twice. We'll do it using for loops.  The first loop will traverse the total length of the array for ( int i = 0 ; i < a . length ; i + + ) And, the second for loop will be incrementally traversing the array for the sorting for ( int j = i + 1 ; j < a . length ; j + + ) See ...

Find All the Factors of a Number using Java For and If loops

Image
What are the factors of a Number? A factor is a Number that does not leave a remainder when it divides the number.  So Factors of 4 can be 1, 2, 4. Easy right? What about 20? 1,2,4,5,10,20. Now, What about factors of 859? That's difficult right? You might have to do a lot of calculations to find that one.  That's why its an easy but popular Interview Question. We are going to write some simple Java code today to find out all the factors of a number.  It's really easy if you grasped the basic concept correctly.  The Factor of a Number, is a Number which does not leave a remainder. Which Operator can help us to find the Remainder? Simple its the % operator. (Number % factor) will give us a Remainder.  So, the heart of our code is going to be: //if remainder is 0, means the number is a factor if ( num % i = = 0 ) We will be checking the Remainder for all Numbers starting from 1 to the Number itself and store the factors (where Remainder is 0).  ArrayList is ...

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 = "" ; //...