Interview Questions (1) - Internet Company Interview Questions

Baidu Mobile Games

1. Differences between ArrayList and LinkedList
2. HashMap implementation principle, internal structure, implementation of JDK1.8 3. How
to design the database
4. Why the JVM heap is divided into young generation and old generation and an Integer variable, when will these two variables be recycled

Ali Gord

1. Is LinkedList a singly linked list or a doubly linked list
2. JVM memory model
3. GC algorithm and collector (CMS)
4. Which layers are TCP, UDP, IP, and HTTP at?

Ali

1. JVM memory model
2. ZooKeeper election

IQIYI

1. How to find the problem with 100% CPU 2. The difference between MySQL
's two storage engines (Innodb and MyISAM) 3. The difference between
Redis and Memecache

Weibo

1. How much memory does Redis store in a kv
2. MySQL index problem, clustered index (a, b, c), whether to use the conditions b and c to go to the index when querying
3. How to efficiently translate the string IP into an integer

360 interview
1. How to implement a list, set
2, how to deduplicate a list
3. How to implement threads to execute at the same time
4. Implementation of threads
5. Project demonstration
6. The difference between hashmap and hashtable

blue flood

1.redis
2.netty
3.

Topic explanation

1. Java class loader mechanism
Class loader structure:
Bootstrap ClassLoader (written in native code, not inherited from ClassLoader): Load Java core library
|/
Extended class loader (ExtClassLoader, inherited from ClassLoader, parent== null): Load Java's extension library
|/
system class loader (SystemClassLoader, inherited from ClassLoader, parent==ExtClassLoader): Load Java classes according to the class path (CLASSPATH) of the Java application, which can be obtained through ClassLoader.getSystemClassLoader() it.
Class loading process:
The proxy mechanism is used when loading classes. The current class loader will first let the parent class loader load it. If it cannot be loaded, it will load it by itself. The web class loader mechanism is different, and it is loaded first (except for the Java core class library).
How to develop your own class loader:
Inherit from ClassLoader, rewrite findClass(), and call defineClass() of the parent class after obtaining the bytecode in the method. Not overridable loadClass() (LoadClass implements proxy mechanism logic internally)

The class loading mechanism of the virtual machine: The virtual machine loads the data class file describing the class into the memory, verifies the data, converts, parses and initializes the data, and finally forms a java class that can be directly used by the virtual machine.
The class is loaded into a jvm Need to go through Loading -> Linking -> Initialization
(a) Verification: Check the correctness of the loaded Class file data;
(b) Preparation: Allocate storage space for the static variables of the class;
(c) Analysis: Convert the symbolic reference to direct reference;

2. Garbage collection algorithm
a. Reference counting method
is realized by adding a counter to an object.
Advantages: Simple to implement
Disadvantages : Can't solve the circular reference problem
b. Mark-Clear Algorithm
Through the root node, mark all objects reachable from the root node, and then clear the unmarked objects. ,
Advantages: can solve the problem of circular reference
Disadvantages : cause memory fragmentation
c. Copy algorithm Divide
the original memory into two pieces, use only one piece at a time, and copy the surviving objects in the memory being used to another piece of memory when recycling, Then clear the remaining objects in the previous memory, and swap the roles of the two memory blocks.
Advantages: If there are few surviving objects, there will be few objects that need to be copied, which is very efficient, and the memory will be continuous.
Disadvantages : The memory will be halved
.
One end of the memory can be cleaned up to the external memory.
Advantages: Memory is continuous
Disadvantage : Objects need to be moved, so many reference addresses need to be changed
. e. Incremental algorithm
This algorithm is to solve the Stop-The-World problem, that is, the application needs to be suspended during garbage collection, so in order to reduce the application suspension time, the garbage collection process is performed in batches.
Advantages: Reduce application pause time
Disadvantages : Increase thread context switching time, reduce system throughput
f. Generation
Divide the memory into different blocks, and use different garbage collection algorithms according to the characteristics of different memory block objects. For example: HotSpot divides the memory into young generation and old generation. The young generation is characterized by a large number of objects, but fewer surviving objects. It is suitable to use a more efficient copying algorithm to copy the surviving objects to another piece of memory, which can solve the problem of memory fragmentation. ; and the old age object has a longer life cycle and a larger object, so it is suitable to use the mark-compression algorithm.

3. Garbage collector type
a. New generation serial collector (DefNew)
b. Old generation serial collector (DefNew)
c. Parallel collector (ParNew)
d. New generation parallel collection collector (PSYoungGen)
e. Old S parallel collection collector (PSYoungGen)
f.CMS collector
g.G1 collector

4.
JVM a. JVM memory model: The JVM memory model is divided into five areas: program counter, virtual machine stack, local method stack, heap, and method area
b. Program counter, virtual machine stack and local method stack belong to thread private, while heap And the method area is shared by threads.
c. The program counter saves the bytecode line number executed by the current thread. When the thread executes each method, it will create a stack frame on the virtual machine stack. The stack frame mainly saves the local variable table, operation stack and other information, and the local method stack. is used for native method calls. If the thread is currently executing a native method, the program counter is null.
d. The method area saves Java class information, static variables, JIT compiled native code, etc. The constant pool has been moved to the heap in JDK1.7.
e. The heap holds all the object information created during the running of the program. The heap can also be subdivided into: the young generation and the old generation, and the young generation can also be subdivided into: an eden area and two survivor areas (from and to). Objects are created in the eden area, and large objects may be created directly in the old age.
f. According to the different characteristics of objects in the young generation and the old generation, the garbage collector uses the copy algorithm for the young generation, and the mark-compression algorithm for the old generation. Since there are not many surviving objects and the objects are small in the young generation, it is very efficient to directly use the copy algorithm; in the old generation, because there are many surviving objects and the objects are large, it is better to use the tagging algorithm, and at the same time, in order to avoid the comparison algorithm. Fragments are compressed using a compression algorithm.

5. Class loading mechanism
1) Class loading timing
Actively refer
to a. new objects, access static properties of classes and call static methods of classes
b. When invoking by reflection
c. When initializing a class, you need to initialize the parent class first
d. Start the virtual machine When you need to specify a main class, the virtual machine will initialize the class first
Passive reference
a. When using a subclass to access the static variables of the parent class or call the static method of the parent class, only the parent class will be initialized, and the subclass will not be initialized
b. Define a class The array of the class does not cause the initialization of the class
c. The constant (final static) of the reference class will not cause the initialization of the class, because the constant will be saved to the constant pool, so the reference constant directly refers to the constant pool, not through the class

2) Class loading process
Loading –> Verification –> Preparation –> Analysis –> Initialization
Loading is carried out through the class loader, and the proxy mode
verification is mainly used for security verification of the class content
Preparation is to allocate memory for the class in the method area, Set the class attribute to 0
to resolve and replace the symbolic reference in the string constant pool with a direct reference
Initialization is mainly the initialization of the class, that is, assigning values ​​to static variables, executing static statement blocks
3) Strong references, weak references and soft references

6. File reading process
a. Determine which pages of the file system the requested data is distributed in
b. Allocate enough memory pages in the kernel space to accommodate the determined file system pages
c. Establish between the memory pages and the file system pages Mapping relationship
d. The page is loaded through virtual memory, and the page content is read from the disk.
e. After the page is loaded, the file system parses the original data to obtain the required file content or attribute information

7.NIO
1) File lock:
The file lock is for the process level. If a thread in a JVM has acquired the file lock, another thread will throw the OverlappingFileLockException exception. Neither does requesting two shared locks.
2) The difference between NIO and IO:
The way data is packaged and transmitted is different. The old IO processes data in a stream, while NIO processes data in blocks. Modern operating systems fetch data from disk in blocks, so NIO will be much more efficient in data processing than old IO.

8.Http protocol
1) Request content:
Request line: Method URI Version number POST /reg.jsp HTTP/ (CRLF)
Request header (message header): The request header contains parameters such as Content-Length, Accept, etc.
Request body (message body) : Passed data, such as POST content
2) Response content:
Status line: Server Http version number Status code Status code description
Response header:
Response body:

8. Java code optimization
Ordinary code:
1) Use a collection constructor with a specified initialization capacity when the length of the collection is known
2) Use a non-thread-safe class in a single-threaded case, such as StringBuffer and HashTable
3) Avoid using the exception mechanism incorrectly, unless it is used when it is impossible to judge whether an exception will occur. If the abnormal state can be pre-judged, make a conditional judgment in advance and avoid using the exception mechanism
4) Try to use final constants to avoid excessive Use more static variables (because static variables and methods are difficult to recycle, the Class information needs to be recycled, and the corresponding ClassLoader needs to be recycled)
5) Try to use local variables, because local variables are stored in the stack, and the access speed is higher than the heap
Concurrency :
1) Try to use the thread pool and set the size of the thread pool reasonably, such as CPU+1 for CPU-intensive tasks and 2*CPU for IO-intensive tasks
2) Do not use locks easily, reduce the granularity of locks, and use read-write locks reasonably
3) Use efficient concurrency tool classes provided by JDK, such as concurrent collection classes under JUC, and atomic classes

9. Differences between redis and memcached
1) redis supports a wealth of data types, and memcached only supports a single kv data type
2) redis supports data persistence, such as AOF and RBD, memcached does not support, and all data is lost after restart
3) memcached supports CAS, redis can only implement a very simple transaction mechanism by combining multiple operations together and executing them sequentially

10. The difference between the two storage engines of mysql
InnoDB supports transactions and row-level locks and foreign keys.
MyISAM does not support transactions, row-level locks and foreign keys, but only table locks.

11. How to analyze CPU100%
Use top -H to view the thread ID with the highest CPU usage. After finding the thread ID, you can use the JVM monitoring tool provided by JDK, such as jstack to print out the thread stack information (the decimal thread ID needs to be converted to into hexadecimal).
Generally, the situations that occupy a high CPU are:
1) Infinite loop
2) Intensive computing

12. How to design the database
1. The table name should reflect the meaning of the attribute
2. The dynamic and static separation should separate the changing attributes of a table from the unchanged attributes

13. Database index problems
1. MySQL can only perform efficient searches on the leftmost prefix of the index
For example : index (a,b), select * from table where a = a1 and b = b1 and select * from table where a = a1 The index can be used, but select * from table where b = b1 does not use the index, because there is no guide column for the combined index
2. Pay range query

? and < ? or >? or

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324825699&siteId=291194637