Home > Java > JavaInterview questions > SF Technology Interview

SF Technology Interview

Release: 2023-08-15 15:52:17
forward
1278 people have browsed it

I originally scheduled an interview at three o'clock, but the interviewer came online early and saw that I was online, so he told me to start early.

Look at the questions first

  1. Introduce yourself
  2. Tell me about a project that you think is challenging
  3. How to learn Java
  4. Tell me about abstract classes and interfaces
  5. Let’s talk about HashMap and Hashtable
  6. The process of adding an element to HashMap
  7. What is a red-black tree and what are its characteristics?
  8. The characteristics of B tree, how many layers, how many pieces of data can be stored at maximum
  9. Why use the index of MySQL B-tree without using skip list?
  10. RedisWhy use a skip table instead of a B-tree or binary tree?
  11. What should you pay attention to when creating an index?
  12. If the amount of data in a single table exceeds tens of millions, how to optimize it?
  13. A table a with 5 million pieces of data and a table b with 3 million pieces of data are related through the foreign key tid. How can we quickly query the 50000th to 50200th data that meet the conditions? 200 data records?
  14. Talk about the JVM memory model
  15. Why do you need the Survivor area?
  16. What do you have? Want to ask me?

Analysis of test questions

Self-introduction

Rookie’s answer:

Hello interviewer, my name is Zhang San, from Henan. I graduated from XX University. I have been engaged in java development since I graduated in XX. It's been 3 years. Come to your company for an interview and seek a java development job.

A few points to introduce yourself: Who are you and what are your strengths? What have you done all these years? What awards did you win at school? What technologies have you conducted in-depth research on? Is there a design for a highly concurrent system? Have you participated in any large-scale projects?

In short, show off all your assets and let others know which areas you are relatively strong in.

Tell me about a project that you think is challenging

This question actually varies from person to person. For those who are just getting started, call him Building a project feels very challenging.

For Daniel, the “challenge” is no longer technology. What's more, how to package the project to persuade the boss, and how to squeeze one's subordinates are the challenging aspects of the project.

In the interview process, "challenging" is a standard for interviewers. If the interviewer has never been exposed to the technical aspects of the project business and it sounds difficult, then this is a "challenging" project.

If the interviewer is familiar with the challenge project you mentioned, it may be an opportunity and a challenge for you at this time. Answer the questions that the interviewer has not encountered and how you have solved them. Then the interviewer I completely admire you. On the contrary, if the interviewer asks you questions about this project that he knows and you can't answer, then the interview will be greatly compromised.

For example: Five or six years ago, your technology stack included dubbo and Spring Boot, which were very popular, but now they are standard.

However, there are still few developers with experience in big data, high concurrency, and architecture transformation, because most companies cannot develop into large companies. but. This is something that cannot be changed no matter how software engineering develops.

So challenging projects have the following characteristics:

1. Large data volume

2. High concurrency

3. Architecture transformation

As long as your project can have something to do with these things, then your project level will be at least one level higher.

Here, I will give you an answer template:

1. I am responsible for this xxx business project, and this business is for xxx.

2. In order to quickly trial and error and respond to the market quickly, a simple xxxx plan was used in the early stage.

3. With the development of business, this plan has xxx technical problems in xxx.

4. In order to solve these technical difficulties, we finally used the xxx solution, and then introduced these solutions and how these solutions solved these technical problems.

How do you usually learn Java

Just tell the truth about your learning process, but be careful, learning should reflect your Actively, in addition, there is a good habit of marking yourself: taking notes, several times is not as good as a bad pen.

It is recommended to read the official website, read books, and watch videos.

In the learning process, keep practicing, constantly reflecting, and constantly summarizing.

Let’s talk about abstract classes and interfaces

  • Abstract classes must be inherited by subclasses, and interfaces must be implemented by subclasses.
  • Abstract classes can have constructors, but interfaces cannot have constructors.
  • There can be ordinary member variables in an abstract class, but there are no ordinary member variables in an interface. Its variables can only be public static constants
  • A class can implement multiple interfaces, but can only inherit one parent class, which can be an abstract class.
  • Method declarations and method implementations can be made in abstract classes. Method declarations can be made in interfaces, and default methods can also be defined.
  • Abstract level (from high to low):Interface>Abstract class>Implementation class.
  • Abstract classes are mainly used to abstract categories, and interfaces are mainly used to abstract method functions.
  • The keyword of abstract class is abstract, and the keyword of interface is interface

Let’s talk about HashMap and Hashtable

We can answer from five aspects:

  1. Is the thread safe: HashMap is non-thread safe, HashTable is thread safe . Because the internal methods of HashTable are basically modified by synchronized. (If you want to ensure thread safety, use ConcurrentHashMap);

  2. Efficiency: Because of thread safety issues, HashMap is slightly more efficient than HashTable. In addition, HashTable is basically eliminated, do not use it in your code;

  3. Support for Null key and Null value: HashMap can store null keys and values. But there can only be one null as a key, and there can be multiple nulls as a value; HashTable does not allow null keys and null values, otherwise a NullPointerException will be thrown.

  4. The difference between the initial capacity and the capacity expansion each time:

    ① If the initial value of the capacity is not specified when creating it, the default initial size of the Hashtable is 11. After each expansion, the capacity becomes the original 2n 1. The default initialization size of HashMap is 16. Each subsequent expansion will double the capacity.

    ② If the initial value of capacity is given when creating, Hashtable will directly use the size you gave, and HashMap will expand it to a power of 2 (tableSizeFor( in HashMap) )method guarantee).

  5. Underlying data structure: HashMap after JDK1.8 has undergone major changes in resolving hash conflicts. When the length of the linked list is greater than the threshold (default is 8 ) (it will be judged before converting the linked list into a red-black tree. If the length of the current array is greater than 64, it will choose to expand the array first instead of converting it to a red-black tree). When converting the linked list into a red-black tree, the linked list will be converted into a red-black tree to reduce Search time. Hashtable has no such mechanism.

Although it is an ordinary interview question, many students still do not answer it well during the interview. The nth power of 2 is mentioned in the answer. The interviewer is likely to continue to ask related questions. If it is still unclear, it is recommended to study HashMap systematically.

I have posted two articles before on my blog:

HashMapThe process of adding an element

HashMapThe process of adding elements in put can be divided into the following 9 steps:

  • 1. When using the put() method, directly call the putVal() method
  • 2. Judge first when putting Whether the array is empty, if it is empty, perform the resize operation
  • 3. Use the length of the hash index array -1 to perform an AND operation with the hash value of the key to obtain the index in the array , if the position specified by the index is empty, it means that it can be inserted. Directly insert a new node
  • 4. Determine whether the current key exists, and replace it if it exists. If the replacement is successful, Then return the old value
  • 5. If the key does not exist, determine whether the current node is a tree type. If it is a tree type, follow the tree operation to append the new node content
  • 6. If the node where the hash conflict occurs is not a tree type, it means that the current collision is in the linked list, and then the loop processing logic will be entered at this time
  • 7. After entering the loop logic, first determine whether the next node of the collided node is empty. If it is empty, put the new node into it
  • 8. After placing it, determine the current node Whether the linked list exceeds the maximum allowed linked list length of 8. If it exceeds, it will be converted to a red-black tree for insertion
  • 9. If the index table of the map is empty or the current index table length is less than 64 (maximum (to the length of the index array table of the red-black tree), then just perform the resize operation; otherwise, if the collided node is not empty, then add the node and insert it along the tree of the collided node

You can read the blog post on my blog: Three years of essential HashMap source code: http://www.woaijava.cc/blog/211

What is a red-black tree and what are its characteristics?

Red Black Tree (Red Black Tree) is a specialized AVL tree (balanced binary tree), which maintains the binary value through specific operations during insertion and deletion operations. Balance the search tree to achieve higher search performance.

There are 5 characteristics of red-black trees:

  • The nodes are red or black.

  • The root node is black.

  • All leaves are black (leaves are NIL nodes)

  • The two child nodes of each red node are black. (There cannot be two consecutive red nodes on all paths from each leaf to the root)

  • From any node to each of its leaves All paths contain the same number of black nodes.

#In fact, this question is not difficult. It is rare that some interviewers may ask about the operation of red-black trees, rotating left and right..., I have interviewed There are hundreds of people, but only a few can speak out.

#The characteristics of the B tree include how many layers it has and the maximum number of pieces of data it can store.

The B tree has two characteristics:

  • 1. Non-leaf nodes] only have an indexing function, that is to say, non-leaf nodes can only store Key, but not value
  • 2. All leaf nodes of the tree form an ordered linked list, and all data can be traversed in order of key sorting.

B Trees are generally 1 lane and 3 layers.

InnoDB page sizeThe default is 16KB:

  • Assuming that the size of a record is 1KB, then 16 pieces of data can be stored in one data page ( Ignore other data structures in the page)
  • Assuming the primary key is int and the pointer size is 6B, then an index page can store 16KB/(4B 6B)≈1638 indexes

So, a two-layer B-tree can store: 16*1638=26208 pieces of data; a three-layer B-tree can store: 16*1638*1638=42928704 pieces of data.

Why does the index of MySQL use B-tree instead of skip table?

B tree is a multi-tree structure. Each node is a 16k data page, which can store more index information, so fans The output is very high. Three layers can store about 2kw of data. That is to say, if you query the data once, if these data pages are all on the disk, you need to query the disk IO at most three times.

Jump list is a linked list structure, one piece of data is one node. If the bottom layer is to store 2kw data, and each query must be able to achieve binary search## The effect of #, 2kw is about 2 to the 24th power , so the approximate height of the jump table is around 24th layer . In the worst case, these 24 layers of data will be scattered in different data pages, which means that one data query will require 24 disk IO.

Therefore, when storing data of the same magnitude, the height of the B-tree is less than that of the skip table. If it is placed on a MySQL database, it means that

the number of disk IOs is fewer, so the B-tree query is faster.

For

write operations, the B-tree needs to split and merge index data pages, and the jump table is inserted independently, and the number of layers is determined based on a random function. There is no overhead of rotation and balance maintenance, so The writing performance of skip table will be better than B-tree.

In fact, MySQL's

storage engine can be changed. In the past, mysql 5.5 was myisam, and later it was innodb. Their underlying indexes all use B trees. In other words, you can completely build a storage engine with a skip table index and install it in MySQL. In fact, facebook built a rocksDB storage engine, which uses jump table. Speaking directly to the conclusion, its writing performance is indeed better than innodb, but reading performance is indeed much worse than innodb. If you are interested, you can see their performance comparison data in References at the end of the article.

RedisWhy use a skip table instead of a B-tree or binary tree?

Because the principle of B-tree is that leaf nodes store data and non-leaf nodes store indexes. Each node of B-tree can store multiple keywords, and it sets the node size to disk pages. size, making full use of the disk read-ahead function. Each time a disk page is read, an entire node is read. Each leaf node also has pointers to the previous and next nodes, in order to minimize disk IO. Because the time it takes to read data in memory is one millionth that of reading IO from disk, and Redis operates data in memory and does not involve IO, so a skip table is used;

What should you pay attention to when creating an index?

#This question can also be used when asking what SQL optimizations you know.

  • The most suitable columns for indexing are the columns that appear in the WHERE clause, or the columns in the join clause, but not the columns that appear in the SELECT The column after the keyword.
  • The larger the cardinality of the index column, the better the indexing effect.
  • Create a composite index according to the situation. The composite index can improve query efficiency.
  • Avoid creating too many indexes, which will occupy additional disk space and reduce the efficiency of write operations.
  • Select a shorter data type for the primary key as much as possible, which can effectively reduce the disk usage of the index and improve query efficiency.
  • To index a string, a prefix length should be customized, which can save a lot of index space.

#If the amount of data in a single table exceeds tens of millions, how to optimize it?

1. When designing the database and creating the table, consider performance issues. For example, a single table should not have too many fields. It is recommended that it be within 20. The more indexes, the better. It should be based on The query is created in a targeted manner. Consider indexing the columns involved in the WHERE and ORDER BY commands. You can use EXPLAIN to check whether an index or a full table scan is used, select the appropriate data type, select the appropriate index type, etc.

2. You need to pay attention when writing SQL. For example: do not use the entire table for list data. Use LIMIT to paginate. The number of pages should not be too large. You can find slower SQL by turning on the slow query log. , avoid select *, list the fields that need to be found, etc.

3. Storage engine selection, MyISAM is suitable for SELECT-intensive tables, while InnoDB is suitable for INSERT and UPDATE-intensive tables.

4. Sub-database and table, for example: Sub-database Divide a database into multiple ones. It is recommended to separate reading and writing. Really doing sub-database will also bring a lot of trouble. Development costs are not worth the gain! It is not recommended to use, Table splitting is to optimize a large table according to the above process, but the query is still stuck, then divide the table into multiple tables, divide one query into multiple queries, and then The resulting combination is returned to the user. Table splitting is divided into vertical splitting and horizontal splitting, and a certain field is usually used as the splitting item. For example, split into 100 tables based on the id field: the table name is tableName_id 0. However: sub-tables require modification of the source program code, which will bring a lot of work to development and greatly increase development costs. Therefore: it is only suitable for considering the existence of a large amount of data in the early stages of development and doing a good job in sub-table processing, and is not suitable for applications. It would be too expensive to make modifications after it goes online.

5. Hardware upgrade is the simplest method, but the relative cost is also high, so the boss is not willing to do it.

6. Database upgrade, for example: replace the MySQL database with a big data engine to process data, or replace it with Alibaba Cloud POLARDB. POLARDB is a next-generation relational distributed cloud native database developed by Alibaba Cloud. It is 100% compatible with MySQL and storage. The capacity can reach up to 100T, and the performance can be increased up to 6 times that of MySQL.

A table a with 5 million pieces of data and a table b with 3 million pieces of data are related through the foreign key tid. How can we quickly query the 50000th to 50200th data that meet the conditions? 200 data records?

Method 1: If the tid of table a is self-increasing and continuous, the id of table b is the index. The SQL statement is as follows.

select * from a,b where a.tid = b.id and a.tid>500000 limit 200;
Copy after login

Method 2: If the tid of table a is not continuous, then you need to use a covering index. tid is either the primary key or an auxiliary index, and the id of table b also needs to have an index. The SQL statement is as follows.

select * from b, (select tid from a limit 50000,200) a where b.id = a.tid;
Copy after login

Talk about the memory model of JVM

JVM memory structures include: program counter, heap memory, method area and stack (java virtual machine stack and native method stack).

The program counter (Program Counter Register) is a small memory space. Its function can be regarded as a line number indicator of the bytecode executed by the current thread. In the conceptual model of the virtual machine (only a conceptual model, various virtual machines may be implemented in more efficient ways), the bytecode interpreter works by changing the value of this counter to select the next step that needs to be executed. Basic functions such as bytecode instructions, branches, loops, jumps, exception handling, and thread recovery all rely on this counter to complete.

The heap memory is the largest piece of JVM and consists of the young generation and the old generation. The young generation memory is divided into three parts, Eden space, From Survivor space, and To Survivor space. By default, the young generation follows the 8:1:1 is allocated in the ratio;

The method area stores class information, constants, static variables and other data. It is an area shared by threads. To distinguish it from the Java heap, the method area also An alias is Non-Heap (non-heap); the stack is divided into a Java virtual machine stack and a local method stack, which are mainly used for method execution. The method area can be understood as a specification, and its implementation is such as the permanent generation and metaspace.

Java Virtual Machine Stacks (Java Virtual Machine Stacks) are also thread-private, its life cycle is the same as that of the thread. The virtual machine stack describes the memory model of Java method execution: When each method is executed, a stack frame (Stack Frame) will be created at the same time to store information such as local variable tables, operation stacks, dynamic links, method exits, etc. . The process from each method being called until execution is completed corresponds to the process of a stack frame being pushed from the stack to being popped out of the stack in the virtual machine stack.

Native Method Stacks (Native Method Stacks) and the virtual machine stack play very similar roles. The only difference is that the virtual machine stack serves the virtual machine to execute Java methods (that is, bytecode), and The local method stack serves the Native methods used by the virtual machine. The virtual machine specification does not mandate the language, usage, and data structure of methods in the local method stack, so specific virtual machines can implement it freely.

Why is the Survivor area needed?

If there is no Survivor, every time the Eden area performs Minor GC, and there is no age limit, the surviving objects will be Sent to the old age. As a result, the old generation is quickly filled up, triggering Major GC (because Major GC is usually accompanied by Minor GC, it can also be regarded as triggering Full GC). The memory space of the old generation is much larger than that of the new generation, and a Full GC takes much longer than a Minor GC.

The interviewer may ask: What are the disadvantages of long execution time?

Frequent Full GC consumes a long time and will affect the execution and response of large programs. speed.

If you increase the old generation space, more surviving objects can fill the old generation. Although the Full GC frequency is reduced, as the old generation space increases, once a Full GC occurs, it will take longer to execute.

If the old generation space is reduced, although the time required for Full GC is reduced, the old generation is quickly filled with surviving objects and the frequency of Full GC increases.

So the purpose of Survivor is to reduce the number of objects sent to the old generation, thereby reducing the occurrence of Full GC. Survivor's pre-screening ensures that only objects that have experienced 16 Minor GCs can survive in the new generation. , will be sent to the old generation.

Do you have anything to ask me?

Some interviewers ask this question politely and don't pay much attention to your answer, because the interview is probably cool at this time.

However, if there is a chance to ask a question, most people still think you are good, and the probability of being admitted is very high, so you still have to answer carefully

No matter what his mentality is, let's behave well. That’s it.

This question may seem dispensable, but it is actually very important. Generally, interviewers do not like people who say "No problem" because they pay great attention to the personality and innovation of employees. ability. Companies don't like job seekers asking questions about personal benefits. If someone asks: Does your company have any training programs for new employees? Can I participate? Or what is your company’s promotion mechanism? The company will welcome you because it shows your passion for learning and your loyalty to the company, as well as your ambition.

The above is the detailed content of SF Technology Interview. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Java后端技术全栈
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template