preface
Only a bald head can be strong.
Star: github.com/ZhongFuChen…
Starting today, I, three crooked, officially start writing the interview series. I have a name for this interview series, “Please Give me an Offer.”
My last post was called “Please Give me an Offer: How to Write a Resume”.
So this article is called “Please Give me an Offer: List interview Questions.”
So let’s get started.
This article has a matching video to watch: www.bilibili.com/video/BV1nT… Welcome company three.
The interview scene
Interviewer: “Can you tell me a little about yourself?”
Sancrooked: “My name is Sancrooked, and I currently maintain a public account called Java3y. In recent years, I have written 300+ original technical articles, nearly 1,000 pages of original e-books and many knowledge points of mind mapping. My vision is: as long as the students who pay attention to me can get offer from Dachang. I…”
Interviewer: “Stop, stop, stop. Let’s get started.”
Interviewer: “Let’s talk about lists in Java. What do you know about lists?”
List is an interface in Java. The common implementation classes are ArrayList and LinkedList. ArrayList is the most commonly used in development.
Interviewer: “Explain the difference between ArrayList and LinkedList again.”
Three skew: “the underlying data structure of ArrayList is an array, and the underlying data structure of LinkedList is a LinkedList.”
Interviewer: “Why use an ArrayList when we already have arrays?”
“One of the things about a native array is that you have to create a size for it when you use it, whereas an ArrayList doesn’t.”
Interviewer: “Why don’t you tell me how ArrayList is implemented? Why doesn’t ArrayList create a size?”
Three crooked: “in fact is such, I have seen the source code. When we new ArrayList(), we have an empty Object array of size 0 by default. When we first add data, we initialize the array to a size that defaults to 10.”
Interviewer: “HMM.”
The size of an array is fixed, whereas the size of an ArrayList is variable.
Interviewer: “What about fixed and variable? Tell me.”
“Suppose we have an array of size 10, and to fill the array, we can only add 10 elements. With ArrayList, we can add 20, 30, or more elements to it when we use it.”
“ArrayList is dynamically scalable”
Interviewer: “So how does it work?”
Every time you add an ArrayList, it evaluates whether the array has enough space. If it does, it simply appends to it. If not, you have to expand.”
Interviewer: “How do you expand that? How much at a time?”
In the source code, there is a method grow, each time 1.5 times the original. For example, the initialization value is 10. Now I’m going to enter the 11th element, and I’m running out of space in this array, so I’m going to expand it to 15.”
Arraycopy is called to copy the array after the space is expanded.
Interviewer: “Oh, yes. So why did you mention earlier that arrayLists are used the most in daily development?”
“It is determined by the underlying data structure. In daily development, traversal needs more than additions and deletions. Even additions and deletions are often added at the end of the List. Like adding elements to the tail, the ArrayList has O(1) time.”
On the other hand, ArrayList’s copyOf() is optimized to block memory on modern cpus, and ArrayList can add and delete no slower than LinkedList.
Interviewer: “Do you know Vector?”
“Well, Vector is the underlying structure of an array, which we don’t use much anymore. Compared to an ArrayList, it’s thread-safe, and it doubles the size of the array when you expand it, so you have 10 elements, and when you expand it, you increase the array to 20.”
Interviewer: “Well, if we don’t use Vector, what else is thread-safe List?”
Three skew: “First, we could also wrap ArrayList around Collections to make it thread-safe. But that’s not what you want to hear, is it? There is also a class under the java.util.concurrent package called CopyOnWriteArrayList.”
Interviewer: “Well, go on.”
Before I get to CopyOnWriteArrayList, I want to say copy-on-write, which I’m going to call cow for short. In Linux, for example, we know that all processes are forked by the init process, and the fork process, except for the process number, is the same as the parent process by default. Before fork and exec, both processes use the same memory space, which means that the child’s code, data, and stack refer to the parent’s physical space.”
Interviewer: “HMM.”
Three skew: “When there is a change of behavior in the parent process, the child process is allocated physical space. This has the advantage of waiting until the actual changes are made to allocate resources, which reduces the instant latency associated with allocating or copying large amounts of resources. In simple terms, it can be understood as our lazy loading, or slacker singleton pattern. And then distribute it when you really need it.”
Interviewer: “HMM.”
In file systems, there is cow. Cow is used to modify data in a new location rather than in the original location. For example, if you want to modify the contents of data block A, read A first and write it to block B. If the power goes out at this time, the original A content is still there. The advantage of this is to ensure data integrity, instant failure is easy to recover.
Back to CopyOnWriteArrayList, CopyOnWriteArrayList is a thread-safe List that is implemented by copying an array.
“Let me talk about the implementation of its add() method.”
Interviewer: “Ok”
“In the add() method, it will lock, copy a new array, add real elements to the new array, and change the array pointer to the new array.”
Get () or size() simply gets the element or size of the array to which the array is pointing. Read unlocked, write locked”
CopyOnWriteArrayList is very similar to COW for file systems.
Interviewer: “Can you tell me about the downside of CopyOnWriteArrayList?”
Obviously, CopyOnWriteArrayList is memory expensive, copying an array every time a set()/add() is created. CopyOnWriteArrayList only guarantees final consistency, not real-time consistency. Let’s say two threads, thread A, read the CopyOnWriteArrayList, haven’t read it yet, and now thread B has cleared the List, thread A can still read the rest of the data.”
Interviewer: “Well, that’s all for today’s interview. Do you have any questions for me?”
Three crooked: “how do you think my performance today?”
Interviewer: “That was a good day, but if you don’t get 100 likes this time, HR will probably never contact you again. If it gets more than 100 likes, do well in the second round.”
Interviewer: “Just for your information, the Map collection can be well prepared. The next round will examine the knowledge of maps.”
digression
List collections in general are not too difficult, but CopyOnWriteArrayList is a class that many of you probably don’t know exists.
Possible for this interview you want to know more details on the List, for example the ArrayList/LinkedList/CopyOnWriteArrayList source and COW mechanism mentioned above, you can reply “List” can get out of the public, before I write the original article.
Students who need to preview or get e-books can get them by replying “888” under the official account.
This article has a matching video to watch: www.bilibili.com/video/BV1nT… Welcome company three.
Summary of various knowledge points
The following articles have the corresponding original exquisite PDF, in the continuous update, you can come to me to urge more ~
- The 92 – page Mybatis
- Multithreading on page 129
- 141 pages of the Servlet
- The JSP page 158
- A collection of 76 pages
- 64 pages of JDBC
- 105 pages of data structures and algorithms
- The Spring of 142 pages
- Filters and listeners on page 58
- 30 pages of HTTP
- For SpringMVC on page 42
- Hibernate
- AJAX
- Redis
- .
Open source project (10K+ STAR) :
- GitHub
- Gitee access is faster
If you want to follow my updated articles and shared dry goods in real time, you can search Java3y on wechat.
The content of the PDF document is typed by hand. If you don’t understand anything, you can ask me directly (the official account has my contact information).
I am three crooked, a man who wants to become stronger, thanks for your likes, favorites and forwarding, see you next time. Give a thumbs-up to Three crooked, for three crooked is really very important!