Interview Programs

11. Program to check whether a string is a palindrome or not.
public class P11_Palindrome {

	public static void main(String[] args) {
		String input = "level";
		String reverse = "";
		int flag = 0;

		// Approach 1
		for (int j = input.length() - 1; j >= 0; j--) {
			reverse = reverse + input.charAt(j);
		}
		if (input.equals(reverse)) {
			System.out.println("String : " + input + " is a palindrome");
		} else {
			System.out.println("String : " + input + " is not a palindrome");
		}

		// Approach 2
		for (int i = 0; i < input.length(); i++) {
			if (input.charAt(i) != input.charAt(input.length() - 1 - i)) {
				flag++;
			}
		}
		if (flag == 0) {
			System.out.println("String : " + input + " is a palindrome");
		} else {
			System.out.println("String : " + input + " is not a palindrome");
		}
	}
}
12. Program to check whether an integer is a palindrome or not.
public class P12_IntegerPalindrome {

	public static void main(String[] args) {
		int input = 4552554;
		int num = input;
		int reversedNum = 0;
		int lastDigit = 0;
		
		while(num > 0) {
			lastDigit = num%10;
			num = num/10;
			reversedNum = reversedNum * 10 + lastDigit;
		}
		
		if (input == reversedNum) {
			System.out.println("Number : " + input + " is a palindrome");
		} else {
			System.out.println("Number : " + input + " is not a palindrome");
		}
	}
}
13. Program to check whether two strings are string anagram or not.
import java.util.Arrays;

public class P13_StringAnagram {

	public static void main(String[] args) {
		
		String sInput1 = "Triangle";
		String sInput2 = "Integral";
		
		//check for string length
		if(sInput1.length() != sInput2.length()) {
			System.out.println(sInput1 + " and " + sInput2 + " are not anagrams");
		} else {
			
			//converting string to lowercase for comparision as anagrams are not case sensitive
			//converting it later to char array
			char[] sArray1 = sInput1.toLowerCase().toCharArray();
			char[] sArray2 = sInput2.toLowerCase().toCharArray();
			
			//sort the arrays so its arranged from a-z sequentially
			Arrays.sort(sArray1);
			Arrays.sort(sArray2);
			
			//equals will check for length of two array
			//It will also check if arrays are equal i.e, each character is same or not
			if(Arrays.equals(sArray1, sArray2)) {
				System.out.println(sInput1 + " and " + sInput2 + " are anagrams");
			} else {
				System.out.println(sInput1 + " and " + sInput2 + " are not anagrams");
			}
		}
	}
}
14. Program to show how a string is immutable in nature.
public class P14_StringIsImmutable {

	public static void main(String[] args) {

		// New object is created with string "Two" as assignment is done
		// input1 is assigned the address of new object that it is pointing to correctly
		// existing object does not change
		String input1 = "One";
		input1 = "Two";
		System.out.print("Output 1 : ");
		System.out.println(input1);
	
		// New object is created with string "An Apple a day, keeps doctor away!"
		// Since assignment is not done, it is still pointing to the old object
		// existing object does not change
		String input2 = "An Apple";
		input2.concat(" a day, keeps doctor away!");
		System.out.print("Output 2 : ");
		System.out.println(input2);

		// New object is not created, it picks up existing object with string "An Apple a day, keeps doctor away!" 
		// input1 is assigned the address of new object that it is pointing to correctly
		// existing object does not change
		String input3 = "An Apple";
		input3 = input3.concat(" a day, keeps doctor away!");
		System.out.print("Output 3 : ");
		System.out.println(input3);

		// New object is created with string "Winter is here!" as
		// assignment is done
		// input1 is assigned the address of new object that it is pointing to correctly
		// existing object does not change
		String input4 = "Winter";
		input4 = input4 + " is here!";
		System.out.print("Output 4 : ");
		System.out.println(input4);
	}
}
15. Program to find duplicate characters in a string.
public class P15_DuplicateInAString {


	public static void main(String[] args) {
		
		String inputString = "Love your self";
		
		char[] inputArray = inputString.toCharArray();
		char[] outputArray = new char[inputString.length()];
		
		int flag = 0;
		int index = 0;
		
		outer: for(char c_outer: inputArray) {
			
			//To check for every character of outer array
			flag = 0;
			
			for(char c_inner: inputArray) {
				
				//check if it matches a character and its not space
				//set flag to indicate duplicate exists
				if(c_outer == c_inner && c_outer != ' ') {
					flag++;
				}
			}
			if(flag > 1) {
				//duplicate exists
				
				//check if value is already present in output array
				for(int i=0; i<outputArray.length; i++) {
					
					if(c_outer == outputArray[i]) {
						continue outer;
					}
				}
				//set duplicate entry in output array
				outputArray[index] = c_outer;
				index++;
				
			}
		}
		System.out.println("Duplicates in String '" + inputString + "' are - ");
		for(int i = 0; i < outputArray.length; i++) {
			
			//As length of output array is equal to input array, so remaining values will be zero
			if(outputArray[i] != 0) {
				System.out.println(outputArray[i]);
			}
		}
	}
}
16. Program to find duplicate characters in a string using a HashMap.
import java.util.HashMap;
import java.util.Map;

public class P16_DuplicateInAStringUsingMap {

	public static void main(String[] args) {
		String input = "Programming";
		Map<Character, Integer> output = new HashMap<Character, Integer>();
		
		char[] characters = input.toCharArray();
		
		for(char c: characters) {
			if(output.containsKey(c)) {
				output.put(c, output.get(c)+1);
			} else {
				output.put(c, 1);
			}
		}
		for(char c_o : output.keySet()) {
			if(output.get(c_o)>1) {
				System.out.println("Character: " + c_o + " Count: " + output.get(c_o));
			}
		}
	}
}
17. Program to remove duplicates from an ArrayList.
import java.util.ArrayList;
import java.util.LinkedHashSet;

public class P17_RemoveDuplicateFromArrayList {

	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<String>();
		list.add("Two");
		list.add("One");
		list.add("Three");
		list.add("Four");
		list.add("Three");
		list.add("Two");
		list.add("Five");
		list.add("Two");
		
		LinkedHashSet<String> newList = new LinkedHashSet<String>(list);
		list.clear();
		list.addAll(newList);
		
		System.out.println(list);
	}
}
18. Program to print a triangle of stars.
public class P18_PrintTriangleOfStars {

	public static void main(String[] args) {
		int n = 5;
		for(int i=0; i< n; i++) {
			for(int j=0;j<i+1;j++) {
				System.out.print("*");
			}
			System.out.println("");
		}
	}
}
19. Program to print a pyramid of stars.
public class P19_PrintPyramidOfStars {

	public static void main(String[] args) {
		int lines = 5;
		int spaces = lines - 1;
		int stars = 1;

		for(int i=0; i< lines; i++) {
			
			stars = (i*2)+1;
			//print spaces
			for(int k=spaces; k>0;k--) {
				System.out.print(" ");
			}
			
			//print stars
			for(int j=0;j<stars;j++) {
				System.out.print("*");
			}
			
			System.out.println("");
			spaces--;
		}
	}
}
20. Program to find the square root of a number.
public class P20_SquareRootOfNumber {

	public static void main(String[] args) {
		SquareRootOfNumber sqrt = new SquareRootOfNumber();
		int num = 16;
		
		//default method
		System.out.println(Math.sqrt(num));
		
		//custom approach 
		//prints only if perfect sqrt present
		for(int i=1; i< num/2; i++) {
			if(num/i == i && num%i == 0) {
				System.out.println(i);
				break;
			}
		}
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top