Android programmers interview, prepare these algorithms, interview questions are accurate, and the annual salary is more than 800000

It's the golden nine silver ten interview season again. Only when you are fully prepared, can the opportunity you want be in your arms.

The following is the main course of data structure and algorithm:

1, Find duplicate numbers in the array

All numbers in an array of length n are in the range of 0~n-1. Find any duplicate number in the array.

Note: if the question is changed to find out the repeated numbers in the array, I need to communicate with the interviewer. Should I find all the repeated numbers or just find one.

Sorting method

First sort the original array, and then traverse the sorted array from beginning to end. It is easy to find duplicate numbers. Sorting an array with length n takes O(nlogn) time.

Hash table method

This problem can be solved by using the hash table. Scan the array from beginning to end to determine whether the scanned number exists in the hash table. If it does not exist, it will be placed in the hash table. If it exists, it will be a duplicate element. The time complexity of this algorithm is O(n), but at the cost of the spatial complexity of O(n).

Exchange method

If there are no duplicate elements, the number I will appear at the subscript i after rearranging the array. If there are duplicate elements, the position of subscript i may be more than one number or there may be no number.

Scan the array from beginning to end. Scan the number with subscript i (represented by m) to see if it is equal to I. if it is, then scan the next number. If not, compare it with the number with subscript m. if it is equal, find a duplicate number. If it is not equal, exchange it with it. Repeat until a duplicate number is found.

Code example:

If there are no duplicate elements, return - 1 (the specific returned value can be communicated with the interviewer)

public int getDuplicateNumber(int[] numbers){

        int len = numbers.length;

        if(numbers == null || len <= 0) return -1;

        for(int i=0;i<len;i++){

            if(numbers[i] < 0 || numbers[i] > len-1)

                return -1;

        }

        

        for(int i=0;i<len;i++){

            

            while(numbers[i] != i){

                if(numbers[i] == numbers[numbers[i]]){

                    return numbers[i];  //Duplicate element found

                }

                else {

                    //Swap the values of numbers[i] and numbers[numbers[i]]

                    int temp = numbers[i];

                    numbers[i] = numbers[temp];

                    numbers[temp] = temp;

                }

            }

            

        }

        return -1;

    } 

2, You cannot modify the array to find duplicate numbers

All numbers in an array of length n+1 are in the range of 1~n. Find any duplicate number in the array. The original array cannot be changed and no auxiliary space larger than O(n) can be used.

dichotomy

Because the length is n+1, the array has at least one duplicate number. You can split by half according to the length. For example, for an array of length 8, divide it into two halves: 1-4 and 5-7. First find the number of numbers in the range of 1-4 in the array. If there are more than 4, the repeated numbers are in 1-4. In this way, the range is narrowed, and then continue to split. Find the number of 1-2 and 3-4 in the array respectively until a duplicate number is found.

public int getDuplicateNumber2(int[] numbers){

        int len = numbers.length;

        if(numbers == null || len <= 0) return -1;

        int start = 1;

        int end = len-1;

        

        while(end >= start){

            int middle = ((end - start) >> 1)+start;

            int count = countRange(numbers,len,start,middle);

            if(end == start){

                if(count > 1) return start;  //Duplicate element found

                else break;

            }

            if(count >(middle-start+1))

                end = middle;

            else

                start = middle+1;

        

        }

        return -1;

    }

    

private int countRange(int[] numbers, int len, int start, int end) {

        if(numbers == null)

            return 0;

        int count = 0;

        for(int i=0;i<len;i++){

            if(numbers[i]>=start && numbers[i]<=end)

                ++count;

        }

        return count;

    } 

Penultimate node

Find the penultimate node in a single lin k ed list

It is easy to think of first traversing the number of linked list nodes n, and the second traversal only needs to find the n-k+1 node.

summary

It is still difficult to ask in this interview. It is required to write and run the code on the spot. It is also very important to investigate the code written by the interviewer
Because Android knowledge system is relatively large and complex, involving all aspects of the field of computer knowledge. Here, some friends and I have specially sorted out a systematic and comprehensive learning material for quickly upgrading to Android senior engineer. It covers the basic learning skills of Android - some necessary learning skills for advanced Android architects.

Attached: We collected 20 sets of real Android interview questions for first and second tier Internet companies (including BAT, Xiaomi, Huawei, meituan and didi) and I sorted out my android review notes (including Android basic knowledge points, Android extended knowledge points, Android source code analysis, design pattern summary, Gradle knowledge points and common algorithm questions.)

It contains self-study programming routes in different directions, interview questions collection / face experience, and a series of technical articles. The resources are constantly updated

CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code

Summary of Gradle knowledge points and common algorithm problems.)
[external chain picture transferring... (img-LGvzrOvt-1631007906883)]
It contains self-study programming routes in different directions, interview questions collection / face experience, and a series of technical articles. The resources are constantly updated

CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code

Tags: Android Design Pattern Algorithm Interview

Posted on Wed, 08 Sep 2021 04:29:21 -0400 by aftabn10