Java, Java Interview Questions

Find duplicate characters in a string in Java

Write a program to find out duplicate or repeated characters in a string, and calculate the count of repeatation.


	private static void findDuplicateCharsInString(String str) {
		
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		Map<Character, Integer> duplicates = new HashMap<Character, Integer>();
		
		for(int i=0; i< str.length(); i++) {
			
			Character ch = (Character) str.charAt(i);
			Integer freq = (Integer) map.get(ch);
			
			if(freq == null) {
				map.put(ch, 1);
			} else {
				map.put(ch, freq+1);
				duplicates.put(ch, freq+1);
			}
		}
		
		for(Map.Entry<Character, Integer> entry : duplicates.entrySet()) {
			System.out.println(entry.getKey() + " " +entry.getValue());
		}
	}
Java, Java Interview Questions

FizzBuzz in Java

Write a Java program that prints the numbers from 1 to 50. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”

	private static void FizzBuzz() {
		
		for(int i=1; i<=50; i++) {
			
			if((i%3 == 0) && (i%5 == 0)) {
				System.out.println("FizzBuzz");
			} else if(i%3 == 0) {
				System.out.println("Fizz");
			} else if(i%5 == 0) {
				System.out.println("Buzz");
			} else {
				System.out.println(i);
			}
		}
	}
Java, Java Interview Questions

Factorial in Java

Using Loop:

	private static void factorial(int number) {
		
		int factorial = 1;
		
		for(int i=number; i > 0; i--) {
			factorial = factorial * i;
		}
		
		System.out.println(factorial);
	}

Using Recursion:

	public static int factorial(int n) {

		if (n == 0) {
			return 1;
		} else {
			return n * factorial(n - 1);
		}
	}
Java, Java Interview Questions

Reverse a string in Java

Sample code to reverse a String in Java:


	public static void reverseString(String str) {
		
		char[] chars = str.toCharArray();
		String reverseStr = "";
		
		for(int i= chars.length-1; i>=0; i--) {
			reverseStr += chars[i];
		}
		
		System.out.println(reverseStr);
	}

Using StringBuilder:


	private static void reverseUsingStringBuilder(String str) {
		
		System.out.println(new StringBuilder(str).reverse().toString());
	}
Java, Java Interview Questions

Find the number of occurrences of each integer in an array in Java


	private static void occurences(int[] arr) {
		
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		
		for(int i=0; i< arr.length; i++) {
			
			Integer freq = map.get(arr[i]);
			
			if(freq == null) {
				map.put(arr[i], 1);
			} else {
				map.put(arr[i], freq+1);
			}
		}
		
		for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
			
			System.out.println(entry.getKey() + " : " + entry.getValue());
		}
	}

Java, Java Interview Questions

Find first non repeating character in a string in Java

For instance, given the string “aabbccdddeff”, the first character that appears only once is “e”.


	private static Character firstNonRepeatingCharacter(String str) {
		
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		List<Character> list = new ArrayList<Character>();
		
		for(int i=0; i< str.length(); i++) {
			
			Character ch = (Character) str.charAt(i);
			Integer freq = map.get(ch);
			
			if(freq == null) {
				map.put(ch, 1);
				list.add(ch);
			} else {
				map.put(ch, freq+1);
				list.remove(ch);
			}
		}
		
		if(list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}