The 8 most common but most error-prone algorithmic questions are almost always tested in interviews. How many can you answer to test?

Preface

There are always some questions, beyond the years, even after the iterations of the new framework, it still exudes an evocative flavor. The following written test questions are often encountered in JAVA interviews. You must keep them in mind, but don't be unable to tell them when you review them. I have suffered this kind of loss, let alone, let's look at the topic.

1. Search in a two-dimensional array

Interview questions

In a two-dimensional array (each one-dimensional array has the same length), each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom. Please complete a function, input such a two-dimensional array and an integer to determine whether the array contains the integer.

Code

public class Test7 {
    
    

	public static void main(String[] args) {
    
    
		int[][] array = new int[][] {
    
    {
    
    1,2},{
    
    2,3},{
    
    3,4}};
		boolean find1 = find(3, array);
		boolean find2 = find(8, array);
		System.out.println(find1); // 输出true
		System.out.println(find2); // 输出 false

	}
	
	/**
	 * @param target
	 * @param array
	 * @return
	 */
	public static boolean find(int target, int [][] array) {
    
    
		int row = 0;
		int col = array[0].length-1;
		
		while(row<array.length && col>=0){
    
    
        if(array[row][col] == target)
            return true;
        else if(array[row][col] > target)
            col-=1;
        else
            row+=1;
		}
		return false;
    }
	

}

2. Linked list questions

Interview questions

Enter a linked list and return an ArrayList in order from the end to the beginning of the linked list

Code

class ListNode {
    
    
    
	int val;        
	ListNode next = null;
	ListNode(int val) {
    
    
	       this.val = val;   
	   }
	}

public class Test8 {
    
    

	public static void main(String[] args) {
    
    
		ArrayList<Integer> printListFromTailToHead = printListFromTailToHead(new ListNode(10));
		System.out.println(printListFromTailToHead.size());
		for (Integer integer : printListFromTailToHead) {
    
    
			System.out.println(integer);
		}

	}
	
	/**
	 * 
	 * @param listNode
	 * @return
	 */
	 public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    
    
		 
	        ArrayList<Integer> arr = new ArrayList<Integer>();
	        ListNode p = listNode;
	        ArrayList<Integer> stack = new ArrayList<Integer>();
	        while(p!=null){
    
    
	            stack.add(p.val);
	            p = p.next;
	        }
	        int n = stack.size();
	        for(int i=n-1;i>=0;i--){
    
    
	            arr.add(stack.get(i));
	        }
	        return arr;
	    }

}

3. Queuing questions

Interview questions

Use two stacks to implement a queue to complete the Push and Pop operations of the queue. The elements in the queue are of type int

Code

public class Test9 {
    
    
	
    static Stack<Integer> stack1 = new Stack<Integer>();
    static Stack<Integer> stack2 = new Stack<Integer>();
    
    public static void main(String[] args) {
    
    
    		push(1);
    		push(2);
    		push(3);
    		System.out.println(stack1.size());
    		System.out.println(stack2.size());
    		pop();
    		System.out.println(stack1.size());
    		System.out.println(stack2.size());
	}
    
    public static void push(int node) {
    
    
         stack1.push(node);
    }
    
    /**
     * pop操作 复杂
     * @return
     */
    public static int pop() {
    
    
    	int temp;
        
    	while(!stack1.empty()){
    
    
            temp = stack1.pop();
            stack2.push(temp);
        }
        
        int res = stack2.pop();
        while(!stack2.empty()){
    
    
            temp = stack2.pop();
            stack1.push(temp);
        }
        return res;
    }}

4. Array Questions

Interview questions

Moving the first few elements of an array to the end of the array is called the rotation of the array. Input a rotation of a non-decreasing sorted array, and output the smallest element of the rotated array.
For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1.

Code

public class Test10 {
    
    
	public static void main(String[] args) {
    
    
		int[] array = new int[] {
    
    1,2,4,3,5,6,0,-1,-100};
		int minNumberInRotateArray = minNumberInRotateArray(array );
		System.out.println(minNumberInRotateArray);
	}
	
	public static int minNumberInRotateArray(int [] array) {
    
    
	    if(array.length==0){
    
    
	            return 0;
	        }
	        for(int i=0;i<array.length-1;i++){
    
    
	            if(array[i] > array[i+1]){
    
    
	                return array[i+1];
	            }
	        }
	        return array[0];
	    }
}

5. Fibonacci sequence problem

Interview questions

Everyone knows the Fibonacci sequence. Now it is required to input an integer n. Please output the nth term of the Fibonacci sequence. [Science] Fibonacci sequence refers to such a sequence of 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368...

Code

public class Test11 {
    
    
	public static void main(String[] args) {
    
    
		int fibonacci = fibonacci(10);
		System.out.println(fibonacci);
	}
	
	public static int fibonacci(int n) {
    
    
			if (n<=0)
            return 0;
			
	        int a=1,b = 1;int temp;
	        for(int i=2;i<n;i++){
    
    
	            temp = a;
	            a = b;
	            b = temp + b;
	        }
	        return b;
	    }
}

6. Frog climbing the stairs

Interview questions

A frog can jump up to one step at a time or up to two steps. Find the total number of jumping methods the frog jumps on an n-level step (different order counts as different results)

Code

public class Test12 {
    
    
	public static void main(String[] args) {
    
    
		int jumpFloor = jumpFloor(18);
		System.out.println(jumpFloor);
	}
	
	public static int jumpFloor(int target) {
    
    
		  	if(target <= 0)
		            return 0;
		        if(target <= 2)
		            return target;
		        int a=1,b=2;
		        int temp;
		        for(int i=3;i<=target;i++){
    
    
		            temp = a;
		            a = b;
		            b += temp;
		        }
		        return b;
		    }
}

7. The problem of abnormal frog jumping

Interview

A frog can jump up to 1 step or 2 steps at a time...it can also jump to n steps. Find the total number of jumping methods the frog jumps on an n-level step.

Code

public class Test13 {
    
    
	public static void main(String[] args) {
    
    
		int jumpFloorII = jumpFloorII(18);
		System.out.println(jumpFloorII);
	}
	
	 public static int jumpFloorII(int target) {
    
    
	        if(target<=0)
	            return 0;
	        int sumPath = 0;
	        int path = 0;
	        for(int i=0;i<target;i++){
    
    
	            path = sumPath + 1;
	            sumPath = sumPath * 2 + 1;
	        }
	        return path;
	    }
}

8. Rectangle coverage problem

Interview questions

We can use 2×1 small rectangles horizontally or vertically to cover larger rectangles. How many ways are there to cover a large 2×n rectangle with n 2*1 small rectangles without overlapping?

Code

public class Test14 {
    
    

	public static void main(String[] args) {
    
    
		int rectCover = rectCover(10);
		System.out.println(rectCover);
	}
	
	public static int rectCover(int target) {
    
    
        if(target <= 0)
           return 0;
       if(target <= 2)
           return target;
       int a=1,b=2;
       int temp;
       for(int i=3;i<=target;i++){
    
    
           temp = a;
           a = b;
           b += temp;
       }
       return b;
   }

}

The above algorithmic questions are very common in interviews and are required questions. It is recommended that students collect them and practice repeatedly.

Friends who like to learn technology can pay attention to my official account: Java Study Guide , private message me into the technology group, we learn together and make progress together.

Insert picture description here

Guess you like

Origin blog.csdn.net/xqnode/article/details/110914154