In today’s computing world, efficiency is key. One of the critical components that can make your application more efficient is caching. Among various caching strategies, the Least Recently Used (LRU) cache is a popular one. In this blog, we’ll delve into what an LRU cache is, why it is useful, and how you can implement … Continue reading “LRU Cache”
Category: Interview Programs
Anagram In Java
Any word or phrase that exactly reproduces the letters in another order is an anagram. Example: cat – act, study – dusty, gainly – laying etc., Logic for anagram is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Arrays; public class Anagram { public static void main(String[] args) { System.out.println(" Is cat,act Anagram: " + isAnagram("cat", "act")); System.out.println(" Is study,dusty Anagram: " + isAnagram("study", "dusty")); System.out.println(" Is gainly,laying Anagram: " + isAnagram("gainly", "laying")); } public static boolean isAnagram(String inputStr1, String inputStr2) { if(inputStr1==null || inputStr2 == null || (inputStr1.length()!=inputStr2.length())) { return false; } char[] letters1 = inputStr1.toUpperCase().toCharArray(); char[] letters2 = inputStr2.toUpperCase().toCharArray(); Arrays.sort(letters1); Arrays.sort(letters2); return Arrays.equals(letters1, letters2); } } |
Output:
Armstrong Number In Java
Armstrong Number is a number that is the sum of its own digits each raised to the power of the number of digits. Example: 13+53+33=153 which is same as given number 153., and 3 is the number of digits. 14+64+34+44=1634 which is same as given number 1634., and 4 is the number of digits.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package my.test; public class ArmstrongNumber { public static void main(String[] args) { System.out.println("Is 153 ArmsStrong Number: " + isArmstrongNumber(153)); System.out.println("Is 1634 ArmsStrong Number: " + isArmstrongNumber(1634)); System.out.println("Is 93084 ArmsStrong Number: " + isArmstrongNumber(93084)); System.out.println("Is 9800817 ArmsStrong Number: " + isArmstrongNumber(9800817)); System.out.println("Is 407 ArmsStrong Number: " + isArmstrongNumber(407)); System.out.println("Is 406 ArmsStrong Number: " + isArmstrongNumber(406)); } public static boolean isArmstrongNumber(int number) { if(number>0) { // Find number of digits in the given number.. int temp = number; int numberOfDigits = 0; while(temp>0) { temp =temp/10; numberOfDigits++; } //Power each digits with the number of digits.. int temp2 = number; int calcNumber = 0; while(temp2>0) { calcNumber = (int) (calcNumber + Math.pow((temp2%10), numberOfDigits)); temp2 = temp2/10; } if(calcNumber == number) { return true; } } return false; } } |
Palindrome String In Java
What is Palindrome? A Palidrome string is a string where if we reverse the string, the word will be same. A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward. Example: madam, refer, redivider The logic in Java for implementation is very simple.,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package my.test; public class Palindrome { public static void main(String[] args) { String string1 = "abcdcba"; String string2 = "abcde"; String string3 = "madam"; String string4 = "refer"; String string5 = "hello"; System.out.println("Is String 1 Palindrome : " + isPalindrome(string1)); System.out.println("Is String 2 Palindrome : " + isPalindrome(string2)); System.out.println("Is String 3 Palindrome : " + isPalindrome(string3)); System.out.println("Is String 4 Palindrome : " + isPalindrome(string4)); System.out.println("Is String 5 Palindrome : " + isPalindrome(string5)); } public static boolean isPalindrome(String str) { String reverseString = ""; if(str!=null && str.length()>0) { char[] charArray = str.toCharArray(); for(int i=charArray.length-1;i>=0;i--){ reverseString = reverseString + charArray[i]; } }else { return false; } System.out.println("Input String: " + str + " , Reversed String: " +reverseString); if(str.equalsIgnoreCase(reverseString)) { return true; } else{ return false; } } } |
Output: Input String: abcdcba … Continue reading “Palindrome String In Java“
Fibonacci series
Fibonacci Series is one of the basic questions in Java., to check the programmer has a basic understanding of logic implementation in java.In Fibonacci series, the current number is always the sum of previous two numbers.Fibonacci series is initialized with 0 & 1.0, 1, (0+1)2, (1+2)3, (2+3)5, (3+5)8, (5+8)13….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Fibonocci { public static void main(String args[]) { int previousNumber = 0; int recentNumber = 1; int nextNumber; int count = 20; System.out.print(previousNumber + " " + recentNumber); for (int i = 2; i < count; ++i) nextNumber = previousNumber + recentNumber; System.out.print(" " + nextNumber); previousNumber = recentNumber; recentNumber = nextNumber; } } } |
Output 0 1 1 2 … Continue reading “Fibonacci series“