Top Easy Interview Coding Programs

take it easy, without having to worry, bolster-3842473.jpg

Introduction

Top Easy Interview Coding Programs

Reverse String

	private static void reverseString() {
		String reverse = "abcd";
		StringBuilder builder = new StringBuilder(reverse);
		builder = builder.reverse();
		System.out.println(builder);
	}

Reverse String Without In buildAPI

	private static void reverseStringWithoutInbuildAPI() {
		String reverse = "abcd";
		char[] array = reverse.toCharArray();
		for (int i = array.length -1; i >= 0; i--) {
			System.out.print(array[i]);
		}
	}

Swap Without Third Variable

	private static void swapTwoNumbersWithoutVariable() {
		int x =10;
		int y = 2;
		x = x + y;
		y = x - y;
		x = x - y;
		System.out.println(" x=" + x + ",y=" + y);
	}

Find Repeating Words in a Sentence

	private static void findRepeatingWords() {
		String repeatingString = "next time there won't be a next time";
		String[] str = repeatingString.split(" ");
		HashMap<String, Integer> hashMap = new HashMap<>();
		for (int i = 0; i < str.length; i++) {
			if (hashMap.containsKey(str[i])) {
				int count = hashMap.get(str[i]);
				hashMap.put(str[i], count + 1);
			} else {
				hashMap.put(str[i], 1);
			}
		}
		System.out.println(hashMap);	
	}

Check Prime Number

	private static void checkPrimeNumber() {
		boolean prime = true;
		int number = 30;
		for (int i=2; i <= number / 2 ; i++) {
			int rem = number % i;
			if (rem == 0) {
				prime = false;
				break;
			}
		}
		System.out.println(prime);
	}

Check Fibonacci Number

	private static void checkFibonacciNumber() {
		int number = 20;
		int a = 0, b = 0, c = 1;
		for (int i = 0; i < number; i++) {
			a = b;
			b = c;
			c = a + b;
			System.out.println(a);
		}
	}

Check Armstrong Number

	private static void checkArmstrongNumber() {
		int number = 153;
		int temp = number;
		int sum = 0;
		while (number > 0) {
			int rem = number % 10;
			number = number / 10;
			sum = sum + (rem * rem * rem);
		}
		if (sum == temp) {
			System.out.println("Armstrong");
		} else {
			System.out.println("Not Armstrong");
		}
	}

Check Palindrome Number

	private static void checkPalindromeNumber() {
		int number = 1221;
		int temp = number;
		int sum = 0;
		while (number > 0) {
			int rem = number % 10;
			number = number / 10;
			sum = sum * 10 + rem;
		}
		if (sum == temp) {
			System.out.println("Palindrome");
		} else {
			System.out.println(" Not Palindrome");
		}
	}

Check Anagram String

	private static void checkAnagramNumber() {
		String string1 = "listen";
		String string2 = "silent";
		char[] charArray1 = string1.toCharArray();
		char[] charArray2 = string2.toCharArray();
		Arrays.sort(charArray1);
		Arrays.sort(charArray2);
		System.out.println(Arrays.equals(charArray1, charArray2));
	}

Generate Factorial Number

	private static int generateFactorialNumber(int number) {
		if (number == 1) {
			return number;
		}
		return number * generateFactorialNumber(number - 1);
	}

Print Triangle

	private static void printTriangle() {
		for (int i = 0; i < 5; i++) {
			for (int k = 0; k < 5 - i; k++) {
				System.out.print(" ");
			}
			for (int j = 0; j <= i; j++) {
				System.out.print("* ");
			}
			System.out.println();
		}
	}

Selection Sort

	private static void selectionSort() {
		int[] numberArray = { 4, 6, 2, 8, 22, 34, 12 };
		ArrayList<Integer> intList = IntStream.of(numberArray).boxed().collect(Collectors.toCollection(ArrayList::new));
		for (int i = 0; i < intList.size(); i++) {
			for (int j = i + 1; j < intList.size(); j++) {
				if (intList.get(i) > intList.get(j)) {
					int temp = intList.get(i);
					intList.set(i, intList.get(j));
					intList.set(j, temp);
				}
			}
		}
		intList.stream().forEach(System.out::println);
	}

Leave a Comment

Your email address will not be published.

error: Content is protected !!
Scroll to Top