java written test handwriting algorithm interview questions with answers
1. Count the number of words in an English article.
public class WordCounting { public static void main(String[] args) { try(FileReader fr = new FileReader("a.txt")) { int counter = 0; boolean state = false; int currentChar; while((currentChar= fr.read()) != -1) { if(currentChar== ' ' || currentChar == '\n' || currentChar == '\t' || currentChar == '\r') { state = false; } else if(!state) { state = true; counter++; } } System.out.println(counter); } catch(Exception e) { e.printStackTrace(); } } }
Add: there may be many ways to write this program. Here we choose the code given by Dennis M. Ritchie and Brian W. Kernighan in their immortal book The C Programming Language to pay tribute to the two teachers. The same is true for the following code.
2. Enter the month, year and day to calculate the day of the year.
public class DayCounting { public static void main(String[] args) { int[][] data = { {31,28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31,29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; Scanner sc = new Scanner(System.in); System.out.print("Please enter month, year and day(1980 11 28): "); int year = sc.nextInt(); int month = sc.nextInt(); int date = sc.nextInt(); int[] daysOfMonth = data[(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)?1 : 0]; int sum = 0; for(int i = 0; i < month -1; i++) { sum += daysOfMonth[i]; } sum += date; System.out.println(sum); sc.close(); } }
3. Palindrome Prime: the so-called palindrome number is the same number read along and backwards (e.g. 111211991...). Palindrome prime is a number that is both palindrome number and prime number (a number that can only be divided by 1 and itself). Program to find the palindrome prime number between 11 and 9999.
public class PalindromicPrimeNumber { public static void main(String[] args) { for(int i = 11; i <= 9999; i++) { if(isPrime(i) && isPalindromic(i)) { System.out.println(i); } } } public static boolean isPrime(int n) { for(int i = 2; i <= Math.sqrt(n); i++) { if(n % i == 0) { return false; } } return true; } public static boolean isPalindromic(int n) { int temp = n; int sum = 0; while(temp > 0) { sum= sum * 10 + temp % 10; temp/= 10; } return sum == n; } }
4. Full Permutation: give all permutations of the five numbers 12345.
public class FullPermutation { public static void perm(int[] list) { perm(list,0); } private static void perm(int[] list, int k) { if (k == list.length) { for (int i = 0; i < list.length; i++) { System.out.print(list[i]); } System.out.println(); }else{ for (int i = k; i < list.length; i++) { swap(list, k, i); perm(list, k + 1); swap(list, k, i); } } } private static void swap(int[] list, int pos1, int pos2) { int temp = list[pos1]; list[pos1] = list[pos2]; list[pos2] = temp; } public static void main(String[] args) { int[] x = {1, 2, 3, 4, 5}; perm(x); } }
5. For a one-dimensional array with N integer elements, find the maximum value of the sum of its sub arrays (an array composed of elements with continuous subscripts in the array).
Several examples are given below (the largest subarray is shown in BOLD):
- Array: {1, - 2, 3,5, - 3, 2}, the result is: 8
- Array: {0, - 2, 3, 5, - 1, 2}, the result is: 9
- Array: {- 9, - 2, - 3, - 5, - 3}, the result is: - 2
It can be solved using the idea of dynamic programming:
public class MaxSum { private static int max(int x, int y) { return x > y? x: y; } public static int maxSum(int[] array) { int n = array.length; int[] start = new int[n]; int[] all = new int[n]; all[n - 1] = start[n - 1] = array[n - 1]; for(int i = n - 2; i >= 0;i--) { start[i] = max(array[i], array[i] + start[i + 1]); all[i] = max(start[i], all[i + 1]); } return all[0]; } public static void main(String[] args) { int[] x1 = { 1, -2, 3, 5,-3, 2 }; int[] x2 = { 0, -2, 3, 5,-1, 2 }; int[] x3 = { -9, -2, -3,-5, -3 }; System.out.println(maxSum(x1)); // 8 System.out.println(maxSum(x2)); // 9 System.out.println(maxSum(x3)); //-2 } }
6. Using recursion to realize string inversion
public class StringReverse { public static String reverse(String originStr) { if(originStr == null || originStr.length()== 1) { return originStr; } return reverse(originStr.substring(1))+ originStr.charAt(0); } public static void main(String[] args) { System.out.println(reverse("hello")); } }
7. Enter a positive integer and decompose it into the product of prime numbers.
public class DecomposeInteger { private static List<Integer> list = new ArrayList<Integer>(); public static void main(String[] args) { System.out.print("Please enter a number: "); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); decomposeNumber(n); System.out.print(n + " = "); for(int i = 0; i < list.size() - 1; i++) { System.out.print(list.get(i) + " * "); } System.out.println(list.get(list.size() - 1)); } public static void decomposeNumber(int n) { if(isPrime(n)) { list.add(n); list.add(1); } else { doIt(n, (int)Math.sqrt(n)); } } public static void doIt(int n, int div) { if(isPrime(div) && n % div == 0) { list.add(div); decomposeNumber(n / div); } else { doIt(n, div - 1); } } public static boolean isPrime(int n) { for(int i = 2; i <= Math.sqrt(n);i++) { if(n % i == 0) { return false; } } return true; } }
8. A step with N steps can take 1, 2 or 3 steps at a time. Ask how many walking methods there are after taking n steps.
public class GoSteps { public static int countWays(int n) { if(n < 0) { return 0; } else if(n == 0) { return 1; } else { return countWays(n - 1) + countWays(n - 2) + countWays(n -3); } } public static void main(String[] args) { System.out.println(countWays(5)); // 13 } }
9. Write an algorithm to determine whether all letters of an English word are different (case insensitive)
public class AllNotTheSame { public static boolean judge(String str) { String temp = str.toLowerCase(); int[] letterCounter = new int[26]; for(int i = 0; i <temp.length(); i++) { int index = temp.charAt(i)- 'a'; letterCounter[index]++; if(letterCounter[index] > 1) { return false; } } return true; } public static void main(String[] args) { System.out.println(judge("hello")); System.out.print(judge("smile")); } }
10. There is an ordered integer array with duplicate elements. Please delete the duplicate elements
For example: A= [1, 1, 2, 2, 3], the processed array should be A= [1, 2, 3].
public class RemoveDuplication { public static int[] removeDuplicates(int a[]) { if(a.length <= 1) { return a; } int index = 0; for(int i = 1; i < a.length; i++) { if(a[index] != a[i]) { a[++index] = a[i]; } } int[] b = new int[index + 1]; System.arraycopy(a, 0, b, 0, b.length); return b; } public static void main(String[] args) { int[] a = {1, 1, 2, 2, 3}; a = removeDuplicates(a); System.out.println(Arrays.toString(a)); } }
11. Give an array with a repeating element accounting for more than half. Find this element.
public class FindMost { public static <T> T find(T[] x){ T temp = null; for(int i = 0, nTimes = 0; i< x.length;i++) { if(nTimes == 0) { temp= x[i]; nTimes= 1; } else { if(x[i].equals(temp)) { nTimes++; } else { nTimes--; } } } return temp; } public static void main(String[] args) { String[]strs = {"hello","kiss","hello","hello","maybe"}; System.out.println(find(strs)); } }
12. Write a method to find the byte length of a string?
public int getWordCount(String s){ int length = 0; for(int i = 0; i < s.length(); i++) { int ascii = Character.codePointAt(s, i); if(ascii >= 0 && ascii <=255) length++; else length += 2; } return length; }
If you have any questions or suggestions about the above contents and need video files, you can also communicate with me. Gongzhong No.: programming field