Remember an android interview

Android intern interviews, there are four rounds of technical aspects (the last side is the CTO interview, basically asking some situations), first record some of my deep memory questions, and then finally receive the offer. The overall feeling is that the algorithm foundation must be solid. In particular, there are more questions on Leetcode, such as linked lists, which are required. Even if some knowledge points are not answered, there is hope as long as the algorithm passes;

table of Contents

table of Contents

first round:

Handler principle

Handler source introduction

The correct way to write Handler:

second round:

The difference between ListView and RecyclerView

Is the data structure of hashMap thread safe, and why is it thread safe?

Understanding of the http protocol, why https is safe

The principle of SharePreferences, the difference between apply and commit

Throwing eggs problem (LeetCode887)

Third round

Hamming weight

Stone throwing problem

Fourth side

The angle between the hour and second hands



first round:

Handler principle

The role of Handler is to switch a task to a specified thread. The operation of Handler needs the support of the underlying MessageQueue and Looper;

MessageQueue is a message queue, which stores a set of messages in memory and provides insertion and deletion operations in the form of a queue. The internal storage structure is actually not a queue, but a singly linked list;

The function of Looper is to use the message loop. Looper will query whether there are new messages in an infinite loop, and process the new messages if there are any, otherwise it will wait forever;

There is a ThreadLocal in Looper, whose function is to store data in each thread. ThreadLocal can store and provide data in different threads without interfering with each other. Through ThreadLocal, the Looper of each thread can be easily obtained;

It should be noted that the thread does not have a Looper by default. When we need to use a Handler, we must create a Looper for the thread. The main thread we often mention is the ActivityThread, which will initialize the Looper when it is created. This is the default in the main thread. The reason why Handler can be used;

The following two are the knowledge points I recorded by the way

Handler source introduction

The Handler can send a Runnable to the Looper inside the Handler through the post method for processing, or send a message through the Handler's send method. This message will also be processed in the Looper. In fact, the post method is also processed through send;

When the send of Handler is called, the enqueueMessage of MessageQueue will be called to put the message into the message queue. The method of reading is next. If there is a western message, it will notify Looper, Looper receives the message and starts processing, and finally Looper hands it over to Handler. Blow, that is, the dispatchMessage method of Handler will be called; handlerMessage will be called at the end in dispatchMessage to process the message

Looper.prepare() can create a Looper for the current thread, and open the message loop through Looper.loop();

Looper exit: quit will exit directly, quitSafely just sets an exit indication, and then completely exits after processing the existing messages in the message queue; in the child thread, if Looper is manually created, it should be called at the end quit to terminate the message loop

The correct way to write Handler:

 Use static inner classes and weak references to prevent memory leaks;

private static class InnerHandler extends Handler {
        WeakReference<DetailActivity> mWeakReference;

        public InnerHandler(DetailActivity detailActivity) {
            mWeakReference = new WeakReference<DetailActivity>(detailActivity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (mWeakReference.get() != null) {
                if (msg.what == 1) {
                    //todo
                }
            }
        }
    }
mInnerHandler = new InnerHandler(this);
        mInnerHandler.sendEmptyMessage(1);

 Finally in onDestroy

mInnerHandler.removeCallbacksAndMessages(null);

second round:

The difference between ListView and RecyclerView

1. In terms of use effect: RecyclerView can provide linear layout, grid layout, waterfall flow layout, and can also control horizontal and vertical scrolling

2. How to use:

ListView needs to inherit and rewrite BaseAdapter; customize ViewHolder and convertView together to complete the reuse optimization work;

RecyclerView inherits and overrides RecyclerView.Adapter and RecyclerView.ViewHolder; sets the layout manager to control the layout effect;

RecyclerView provides notifyItemChanged for refreshing a single item;

RecyclerView provides item animation effects;

3. Cache mechanism:

RecyclerView has two levels of cache than ListView, supports developers' custom cache processing logic, and supports all RecyclerViews to share the same RecyclerViewPool (buffer pool);

1). RecyclerView caches RecyclerView.ViewHolder, abstraction can be understood as:

View + ViewHolder (avoid calling findViewById every time createView) + flag (identification status);

When mCacheViews (off-screen) in RecyclerView obtains the cache, it obtains the cache of the target location by matching pos. The advantage of this is that when the data source data is unchanged, there is no need to rebindView:

2). ListView cache View. And the same is off-screen cache, ListView gets the corresponding cache from mScrapViews according to pos, but it is not used directly, but re-getView (that is, it will definitely re-bindView)

Is the data structure of hashMap thread safe, and why is it thread safe?

It is an Entry array, and each Entry contains a key-value key-value pair;
Entry is a static internal class
in HashMap ; in simple terms, HashMap is composed of array + linked list, the main body of HashMap is the array, and the linked list is for the purpose of If there is a conflict, if the location of the array location does not contain a linked list, then the search, add and other operations are very fast, otherwise for the addition operation, the time complexity is O(n).
When the capacity is expanded, the capacity defaults to 16, and the expansion is 2 times the
ratio of java8 The data structure of red-black tree is added on the basis of java7

Why is HashMap unsafe?
1. When put, the multi-threaded data is inconsistent. For example, there are two threads A and B. First, A wants to insert a key_value into the HashMap. First, calculate the index coordinates of the bucket to be dropped, and then get the The linked list head node of the bucket. At this time, the time slice of thread A is used up, thread B executes, and B inserts the record into the bucket. Assuming that the two indexes are the same, then thread A is calling, which overwrites the record of thread B. , Resulting in inconsistent data;
 

Understanding of the http protocol, why https is safe

  1. Connectionless, stateless, application layer protocol based on request and response mode
  2. Simple and fast: simple protocol and fast communication speed;
  3. Flexible: Successfully transmit any type of data object, marked by Content-Type;
  4. No connection: each time a request is processed, it will be disconnected when the processing is completed;
  5. Stateless: no memory function for processing things;
  6. http is the application layer protocol, and the bottom layer is based on the TCP/IP protocol

   Why it's safe: 

     An additional layer of TLS/SSL encryption suite is added between the application layer http and the transport layer tcp.https is the application layer sending data to TLS/SSL, then encrypting the data, and then sending it to TCP for transmission;

The principle of SharePreferences, the difference between apply and commit

The use of SharedPreferences is very simple and can easily store and read data. SharedPreferences can only save simple types of data, such as String, int, etc. Generally, complex types of data are converted into Base64 encoding, and then the converted data is saved in an XML file in the form of a string, and then saved with SharedPreferences.

The steps to save key-value pairs using SharedPreferences are as follows:

  (1) Use the getSharedPreferences method of the Activity class to obtain the SharedPreferences object, where the name of the file storing the key-value is specified by the first parameter of the getSharedPreferences method.

  (2) Use the edit of the SharedPreferences interface to obtain the SharedPreferences.Editor object.

  (3) Save the key-value pair through the putXxx method of the SharedPreferences.Editor interface. Where Xxx indicates different data types. For example: string type value needs to use putString method.

  (4) Save the key-value pair through the commit method of the SharedPreferences.Editor interface. The commit method is equivalent to the commit operation in a database transaction.

 

apply has no return value, commit has return value

Commit efficiency is lower than apply when data is concurrent, apply is recommended

Throwing eggs problem (LeetCode887)

You will get K eggs and can use a building with N floors from 1 to N.

The function of each egg is the same. If an egg is broken, you can't drop it again.

You know that there is a floor F. If 0 <= F <= N, any egg that falls from a floor higher than F will be broken, and an egg that falls from floor F or a floor lower than it will not be broken.

For each throw, you can take an egg (if you have a complete egg) and drop it from any floor X (satisfying 1 <= X <= N).

Your goal is to know exactly what the value of F is.

Regardless of the initial value of F, what is the minimum number of throws you determine the value of F?

 

The meaning of the question: It is to find the floor where your egg was thrown and just broke, and the number of times in the worst case should be considered

Problem solution: Because this problem limits the number of eggs, it cannot be solved simply by dichotomy. This problem uses dynamic programming, that is to say, for example, when you have two eggs and throw them in the x layer, the function at this time is dp(2,x), and then throw it down and broken, it means that you have one egg left, and you only need to verify all floors under x, that is, dp(1,x-1); and if there is no broken, it means Only need to verify the floor above x, that is, dp(2,Nx);

Because of the monotonicity of these two functions, to find the desired value, you need to find their intersection point, you can use the dichotomy to find the bright spots on both sides of the intersection point, and finally find the minimum

class Solution {
    public int superEggDrop(int K, int N) {
        return dp(K, N);
    }

    Map<Integer, Integer> memo = new HashMap();

    public int dp(int K, int N) {
        if (!memo.containsKey(N * 100 + K)) {//判断之前是否计算过
            int res ;
            if (N == 0) { //在第0层楼
                res = 0;
            } else if (K == 1) { //只剩一个鸡蛋
                res = N;
            } else {//使用二分法找交点
                int low = 1, high = N;
                while (low + 1 < high) {
                    int x = (low + high) / 2;
                    int t1 = dp(K - 1, x - 1);//碎了,单调递增
                    int t2 = dp(K, N - x);//没碎,单调递减
                    //求两函数交点
                    if (t1 < t2) {
                        low = x;
                    } else if (t1 > t2) {
                        high = x;
                    } else {
                        low = high = x;
                    }
                }
                //退出循环说明在low和high在交点两侧
                res = 1 + Math.min(
                        Math.max(dp(K - 1, low - 1), dp(K, N - low)),
                        Math.max(dp(K - 1, high - 1), dp(K, N - high))
                );
            }
            memo.put(N * 100 + K, res);//防止重复计算
        }

        return memo.get(N * 100 + K);
    }
}

Third round

Hamming weight

Write a function, the input is an unsigned integer, return the number of digits in the binary expression is '1' (also known as Hamming weight )

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int res = 0;
        while (n != 0) {
            res++;
            n &= n-1; //把最后一个1变为0
        }
        return res;
    }
}

Stone throwing problem

You and your friend, two people play Nim games together:

There is a pile of stones on the table.
You take turns in your own rounds, and you take the lead.
In each round, the person in his turn removes 1-3 stones.
The one who removes the last stone is the winner.
j Assuming that every step of yours is the optimal solution. Please write a function to determine whether you can win the game given the number of stones n. If you can win, return true; otherwise, return false.

Problem solution: This problem is that as long as there are 4 left, then you can win how the opponent takes it, so the multiple of 4 is the judgment condition for whether you can win;

class Solution {

    public boolean canWinNim(int n) {

        return n % 4 != 0;

    }

}

Fourth side

The angle between the hour and second hands

Question meaning: For example, the angle between the hour and second hands at 00:00 is 0°, and the angle at 00:01 is 6°-0.5° = 5.5°. Ask when the angle is 6°

class Solution {
    public static void main(String[] args) {
        for (int i = 0; i <= 23; i++) {
            for (int j = 0; j <= 59; j++) {
                if (getDegree(i, j) == 6*10 || getDegree(i, j) == -6*10) {
                    System.out.println(i + " " + j);
                }
            }
        }
    }

    private static int getDegree(int hour, int minute) {
        int hourDegree = 0;
        int minDegree = 0;
        int res = 0;
        if (hour >= 12) hour -= 12;
        hourDegree += hour * 30 * 10;
        minDegree += minute * 6 * 10;
        hourDegree += minute * 0.5 * 10;
        res = hourDegree - minDegree;
        return res;
    }
}

 

Guess you like

Origin blog.csdn.net/qq873044564/article/details/109323144