2021 autumn recruitment interview - meituan

Posted by PHPsites on Tue, 07 Sep 2021 04:41:49 +0200

Part I - self introduction

Part II - basic knowledge of Java

1. Differences between interfaces and abstract classes

abstract class

  1. Abstract modified classes are abstract classes, and modified methods are abstract methods.
  2. Abstract classes can have no abstract methods, but classes with abstract methods must be declared as abstract classes.
  3. Abstract classes cannot use new to create objects. It is used to let subclasses inherit.
  4. Abstract methods have only method declarations and no implementation. Let their subclasses implement them.
  5. A subclass inherits an abstract class and must implement all methods of the abstract class, otherwise the subclass must also be declared as an abstract class.
//Abstract abstract class: class extensions: single inheritance (interfaces can inherit more than one)
public abstract class Action {
    //Constraint ~ someone helped us achieve it~
    //Abstract, abstract method, only method name, no method implementation!
    public abstract void doSomething();

    //1. A new abstract class cannot be used. It can only be implemented by subclasses as a constraint
    //2. Abstract methods can only appear in abstract classes. Abstract classes can have ordinary methods
    //Abstract: Constraints
    //3. Abstract classes have constructors that can generate subclasses
    //4. Significance of abstract classes: constraints to improve development efficiency. However, classes can only inherit, so they are limited and not used much
    
}
//All methods of an abstract class that inherit its subclasses must implement its methods

public class A extends Action{
    @Override
    public void doSomething() {
    }
}

Interface

  • Common class: only concrete implementation
  • Abstract classes: concrete implementations and specifications (abstract methods) are available
  • Interface: only specification, no method implementation, professional constraints! Separation of constraints and Implementation: interface oriented programming~
  • An interface is a specification, which defines a set of rules, "what are you... What must be done...".
  • The essence of an interface is a constraint. Just like human laws, it is formulated and observed by everyone.
//The keyword defined by interface and the interface need to have an implementation class
public interface UserService {
    //All definitions in the interface are actually Abstract public abstract s
     void add(String name);
     void delete(String name);
     void update(String name);
     void query(String name);
}
//Class can implement the implements interface
//To implement the class of the interface, you need to rewrite the methods in the interface
public class UserServiceImpl implements UserService{

    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }
}

effect:
1. Restraint
2. Define some different methods for different people's implementation
3,public abstact
4,public static final
5. The interface has no constructor and cannot be instantiated
6. The implementation class must override the methods in the interface
7. Implementation classes can implement multiple interfaces

2. New features after Java 1.8

Lambda expression

A lambda expression is essentially an anonymous inner class or a piece of code that can be passed

package com.kuang.lambda;
//lambda expressions 
public class TestLambda1 {
    //3. Static inner class
    static class Like2 implements ILike{
        @Override
        public void lambda() {
            System.out.println("I like lambda2");
        }
    }
    public static void main(String[] args) {
        ILike like = new Like();
        like.lambda();
        like = new Like2();
        like.lambda();

        //4. Local inner class
        class Like3 implements ILike{
            @Override
            public void lambda() {
                System.out.println("I like lambda3");
            }
        }
        like = new Like3();
        like.lambda();
        //5. Anonymous inner class. There is no class name. You must use the interface or parent class
        like = new ILike() {
            @Override
            public void lambda() {
                System.out.println("I like lambda4");
            }
        };
        like.lambda();

        //6. Simplify with lambda. No interfaces or methods are needed
        like = ()->{
            System.out.println("I like lambda5");
        };
        like.lambda();

    }

}
//1. Define a functional interface
//Any interface that contains only one abstract method is a functional interface
//The interface defaults to abstract methods
interface ILike{
    void lambda();
}
//2. Implementation class
class Like implements ILike{
    @Override
    public void lambda() {
        System.out.println("I like lambda");
    }
}

Multiple parameters

package com.kuang.lambda;

public class TestLambda2 {
    public static void main(String[] args) {
        ILove love = new Love();
        love.love("bl");

        love = new ILove() {
            @Override
            public void love(String a) {
                System.out.println("I love you-->"+a);
            }
        };
        love.love("bdf");
        //lambda expressions 
        love = (String a)->{
            System.out.println("I love you-->"+a);
        };
        love.love("yiyeri");
        //Simplify 1. Remove parameter types
        love = (a)->{
            System.out.println("I love you-->"+a);
        };
        love.love("520");
        //Simplified 2. Simplified parentheses
        love = a ->{
            System.out.println("I love you-->"+a);
        };
        love.love("521");
        //Simplify 3. Simplify curly braces
        love = a ->
            System.out.println("I love you-->"+a);
        love.love("aaaaaaa");

        //Summary:
        /*
        1.lambda The expression can only be simplified into one line if there is only one line of code. If there are multiple lines, wrap them with code blocks
        2.The premise is that the interface is functional
        3.Parameter types can also be removed from multiple parameters. To remove them, they must be removed and parentheses must be added
         */
    }
    
}
interface ILove{
    void love(String a);
}
class Love implements ILove{
    @Override
    public void love(String a) {
        System.out.println("I love you-->"+a);
    }
}

2. New feature after Java 1.8 - hashmap

Optimize the data structure of map sets such as hashMap in jdk1.8.
Optimization of hashMap data structure

Original hashMap

  • The data structure adopted is hash table (array + linked list),
  • The default size of hashMap is 16, an array with 0-15 indexes,
    3. How to store elements inside,
    (1) First, call the hashcode method of the element to calculate the hash code value,
    (2) The index value of the array is calculated by the hash algorithm,
    (3) If there are no elements in the corresponding index, they are stored directly. If there are objects, their equals methods are compared to compare the contents
    If the content is the same, the latter value will overwrite the value of the previous value. If it is different,
  • In 1.7, the added ones are placed in front to form a linked list and form a collision. In some cases, if the linked list goes on indefinitely, the efficiency is very low and the collision can not be avoided
    Loading factor: 0.75. When the array capacity reaches 75% of the total capacity, it will be expanded, but collision cannot be avoided
  • After 1.8, hashmap is implemented in array + linked list + red black tree. When the number of collision elements is greater than 8 & the total capacity is greater than 64, red black tree will be introduced
    In addition to adding, the efficiency is higher than that of the linked list. After 1.8, new elements in the linked list are added to the end. ConcurrentHashMap (lock segmentation mechanism), concurrentlevel and JDK1.8 adopt CAS algorithm (no lock algorithm, lock segmentation is no longer used), and the use of red black tree is also introduced in array + linked list

Hash conflict: storage address conflict
When we hash an element to get a storage address, and then insert it, we find that it has been occupied by other elements

3. Deadlock

It refers to a blocking phenomenon caused by two or more threads competing for resources or communicating with each other in the process of execution. If there is no external force, they will not be able to move forward. At this time, the system is in a deadlock state or the system has a deadlock. These processes that are always waiting for each other become deadlock processes.

For example, there are two processes a and B. A holds a resource, a waits for B resource, B holds a resource, and B waits for a resource. Both processes wait for another resource without releasing the resource, forming a deadlock.

The causes of deadlock mainly include:

  1. Insufficient system resources;
  2. There is a problem with the sequence of program execution;
  3. Improper allocation of resources, etc.

Four necessary conditions for deadlock formation:

(1) Mutex condition: a resource can only be used by one process at a time.

(2) Request and hold condition: when a process is blocked by requesting resources, it will hold on to the obtained resources.

(3) Conditions of non deprivation: the resources obtained by the process cannot be forcibly deprived until they are used up at the end of the year.

(4) Circular waiting condition: a circular waiting resource relationship is formed between several processes.

How do I handle deadlocks?

  • Deadlock prevention: break one or more of the four necessary conditions to prevent deadlock
  • Deadlock avoidance: in the process of dynamic resource allocation, the system is prevented from entering an unsafe state in some way.
  • Deadlock detection: deadlock is generated during operation, which can be found and thought in time to free the program.
  • Release Deadlock: after a deadlock occurs, the process is revoked, resources are recovered, and allocated to the process in the blocking state.

Methods to prevent deadlock:

  • Destruction request and retention conditions:

1. Apply for all resources at one time. After that, you will not apply for resources. If the resource conditions are not met, you will not get resource allocation.
2. Only the initial resource operation is obtained, and then the running resources are released to request new resources.

  • Conditions for destruction and non preemption:

When a process obtains some non preemptible resources, it puts forward a new resource application. If it cannot meet the requirements, it will release all resources and reapply again if necessary in the future.

  • Failure cycle waiting conditions:

Arrange the number of resources and request resources in the order of increasing sequence number.
If a process obtains a resource with a high sequence number and wants to obtain a resource with a low sequence number, it needs to release the resource with a high sequence number first.

Deadlock release method:

1. Seize resources. Preempt a sufficient number of resources from one or more processes and allocate them to the deadlock process to release the deadlock state.

2. Terminate (undo) process: terminate (undo) one or more deadlock processes until the loop is broken, freeing the system from the deadlock state.

Topics: Java Interview unit testing