Ask a big factory for an offer: List interview questions

Preface

The text has been included in my GitHub featured article, welcome to Star : https://github.com/ZhongFuCheng3y/3y

Starting today, I, Sanwai, officially started writing the interview series. I gave this interview series a name called "Ask a big factory for an offer"

The last one was called "Ask a big factory for an offer: How to write a resume"

So this article is called " Ask a big company for an offer: List interview questions "

Let's start next.

This article has a supporting video viewing: https://www.bilibili.com/video/BV1nT4y1L71r/ Welcome to Sanlian .

Interview site

Interviewer: "Please introduce yourself briefly"

Sanwai : "My name is Sanwai . I currently maintain a public account called Java3y . In the past few years, I have written 300+ original technical articles, nearly 1,000 pages of original e-books and mind maps with multiple knowledge points. My vision is: As long as you pay attention to me and the third consecutive classmates can get Dachang offer . My..."

Interviewer: "Stop, stop, stop blowing, let's officially start."

Interviewer: "Let’s talk about Java List. How much do you know about List?"

Three crooks: "List is an interface in Java. Common implementation classes are ArrayList and LinkedList. ArrayList is the most used in development."

Interviewer: "You can talk about the difference between ArrayList and LinkedList separately"

Three crooks: "The underlying data structure of ArrayList is an array, and the underlying data structure of LinkedList is a linked list."

Interviewer: "Then we already have arrays, why use ArrayList?"

Three crooks: "A native array has a characteristic: you must create a size for it when you use it, and ArrayList does not use it"

Interviewer: "Then tell me how ArrayList is implemented. Why doesn't ArrayList need to create a size?"

Three crooked: "In fact, this is the case. I have seen the source code . When we new ArrayList()are, there will be an empty Objectarray by default with a size of 0. When we add data for the firstadd time, we will initialize this array with a size, this The default size is 10"

Interviewer: "Yeah"

Three crooked: "The other is, the size of the array is fixed, but the size of the ArrayList is variable"

Interviewer: "How do you understand fixed and variable? Tell me about it."

Three crooked: "Assuming that the size of our given array is 10, to fill the array with elements, we can only add 10 elements. Unlike ArrayList, we can add 20 or 30 to ArrayList when we use it. And even more elements"

Three crooked: "Because ArrayList is dynamically expanded"

Interviewer: "Then how is it achieved?"

Three crooks: "Every time addyou use ArrayList , it will first calculate whether the array has enough space. If the space is enough, just add it directly. If it is not enough, then you have to expand."

Interviewer: "Then how to expand? How much at a time?"

Three crooked: "In the source code, there is a growway to expand the original time each time 1.5. For example, the initialized value is 10. Now I am going to enter the 11th element, and found that the space of this array is not enough, so I will expand To 15"

Three crooked: "After the space is expanded, it will be called arraycopyto copy the array"

Interviewer: "Oh, yes. Then why did you mention earlier that ArrayList is the most used in daily development?"

Three crooks: "It is determined by the underlying data structure. In daily development, the need for traversal is more than additions and deletions. Even if additions and deletions are often added at the end of the List, it is OK. Like adding elements to the end, the time of ArrayList the complexity of it O(1). "

Three crooked: "The other thing is that the underlying calls for the addition and deletion of ArrayList have copyOf()been optimized. Modern CPUs can block memory operations , and the addition and deletion of ArrayList is not slower than LinkedList at all."

Interviewer: "Do you understand Vector?"

Three crooked: "Well, the underlying structure of Vector is an array. Generally, we rarely use it now. Compared to ArrayList, it is thread-safe. When expanding, it is directly expanded twice, for example, there are now 10 elements. , When you want to expand, the size of the array will be increased to 20"

Interviewer: "Well, if we don't use Vector, what else is thread-safe List?"

Three crooked: "First of all, we can also use Collections to wrap ArrayList to make it thread-safe. But this is definitely not what you want to hear, right. java.util.concurrentThere is also a class under the package called CopyOnWriteArrayList"

Interviewer: "Well, you continue to say"

Three crooked: "Before I want to talk about CopyOnWriteArrayList, I still want to talk about copy-on-writethis meaning, I will abbreviate it below cow. For example, in Linux, we know that all initprocesses forkcome out of processes, except for the process number fork. The default is exactly the same as the parent process. Before forkthat exec, the two processes use the same memory space, which means that the code segment, data segment, and stack of the child process point to the physical space of the parent process."

Interviewer: "Yeah"

Three crooks: "When there is a changed behavior in the parent-child process, the corresponding physical space is allocated to the child process. The advantage of this is that it waits until the actual modification occurs before allocating resources, which can reduce the allocation or copy a large amount of resources. The instant delay caused . Simply put, it can be understood as our lazy loading, or the lazy man-style of the singleton mode. It will be allocated when it is really used."

Interviewer: "Yeah"

Three crooked: "in the file system, in fact, there cow. The mechanism of the file system cowis when modifying data, not directly in the original location of the data on the operation, but a place to re-modify example: To modify the data The content of block A, first read out A and write it into block B. If the power is cut off at this time, the original content of A is still there. The advantage of this is that it can ensure the integrity of the data, and it can be easily restored when it is suspended in an instant .

Three crooks: "Let’s look back at CopyOnWriteArrayList. CopyOnWriteArrayList is a thread-safe List. The bottom layer is implemented by copying arrays .

Three crooked: "Let me talk add()about the realization of its method"

Interviewer: "Good"

Three crooked: "In the add()method in fact, he would add locka lock, then copied out a new array to the new array inside addthe real elements of the array is the last point to be changed to the new array."

Three crooked: "In fact, the get()method or size()method is just to get the element or size of the array pointed to by array. Reading is not locked, writing is locked"

Three crooked: "What can be found is that CopyOnWriteArrayList is very similar to the COW mechanism of the file system"

Interviewer: "Then can you talk about the disadvantages of CopyOnWriteArrayList?"

Three crooked: "Obviously, CopyOnWriteArrayList is very memory consuming, and set()/add()an array is copied every time . In addition, CopyOnWriteArrayList can only guarantee the final consistency of the data, but cannot guarantee the real-time consistency of the data. Suppose two threads, thread A goes Reading the data of CopyOnWriteArrayList has not finished reading. Now thread B has emptied this List, thread A can still read the remaining data at this time."

Interviewer: "Well, it's okay. Today's interview is over. Do you have anything to ask me?"

Sanwai: "What do you think of my performance today?"

Interviewer: "Today's performance is okay. If you don't have 100 likes this time , HR will not contact you again. If more than 100 likes , perform well in the second round."

Interviewer: "I will reveal to you, the Map collection can be prepared, and the next round will examine the knowledge of Map "

Digression

List collection is not too difficult in general, but CopyOnWriteArrayList may not be known to many students yet.

This article has a supporting video viewing: https://www.bilibili.com/video/BV1nT4y1L71r/ Welcome to Sanlian .

Open source projects covering all the knowledge points of the Java backend (10K+ stars already):

I am Sanwai, a man who wants to become stronger. Thank you for your likes, collections and reposts. See you next time. Give Sanwai a like, it is really important to Sanwai!

Guess you like

Origin blog.csdn.net/Java_3y/article/details/108140474
Recommended