Meituan Autumn Recruitment

Meituan Autumn Recruitment

Record the problems encountered during the interview, I hope it can help everyone!

 

Batch processing batch processes several sql under one sql. How to improve the speed? If sharding is required, how to shard it?

1. Use the batch processing function of the database to execute multiple SQL statements. This reduces the communication overhead of each SQL statement. addBatch()The and methods in JDBC executeBatch()can be used to perform batch operations.

At the beginning of the program, disable automatic transaction submission, add all SQL statements to preparedStatement, and roll back the database transaction if an error occurs during program execution.

    public static void main(String[] args) {
        String jdbcUrl = "jdbc:mysql://localhost:3306/your_database";
        String username = "your_username";
        String password = "your_password";

        try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
            //Turn off autocommit and enable transactions
            connection.setAutoCommit(false);

            String sql = "INSERT INTO your_table (column1, column2) VALUES (?, ?)";
            try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
                //Add multiple SQL statements to the batch
                for (int i = 0; i < 1000; i++) {
                    preparedStatement.setInt(1, i);
                    preparedStatement.setString(2, "Value " + i);
                    preparedStatement.addBatch();
                }

                //Execute batch processing
                int[] updateCounts = preparedStatement.executeBatch();

                // Submit transaction
                connection.commit();
            } catch (SQLException e) {
                //Rollback transaction
                connection.rollback();
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

 

2. Use multi-thread processing, create a thread pool with five threads, submit all sql statements one by one, and execute the sql statements in the run method of the thread.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MultiThreadSQLExecution {
    public static void main(String[] args) {
        String jdbcUrl = "jdbc:mysql://localhost:3306/your_database";
        String username = "your_username";
        String password = "your_password";
        String sqlFilePath = "path_to_your_sql_file.sql";

        int numThreads = 5; //Specify the number of threads

        ExecutorService executorService = Executors.newFixedThreadPool(numThreads);

        try {
            Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
            BufferedReader reader = new BufferedReader(new FileReader(sqlFilePath));
            String line;
            
            while ((line = reader.readLine()) != null) {
                // Submit each SQL statement to the thread pool
                executorService.submit(new SQLExecutionTask(connection, line));
            }

            // Wait for all threads to complete
            executorService.shutdown();
            while (!executorService.isTerminated()) {
                Thread.sleep(100);
            }

            reader.close();
            connection.close();
        } catch (SQLException | IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    static class SQLExecutionTask implements Runnable {
        private Connection connection;
        private String sql;

        SQLExecutionTask(Connection connection, String sql) {
            this.connection = connection;
            this.sql = sql;
        }

        @Override
        public void run() {
            try (Statement statement = connection.createStatement()) {
                statement.execute(sql);
                System.out.println("Executed SQL: " + sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 

Network error code 500, 403

1xx - Informational status code (Informational)

  • 100 Continue: The client should continue its request.
  • 101 Switching Protocols: The server requires the client to switch protocols.

2xx - Successful status code (Successful)

  • 200 OK: The request was successful.
  • 201 Created: The request has created a new resource.
  • 204 No Content: The request was successfully processed, but no response body was returned.

3xx - Redirection status code (Redirection)

  • 301 Moved Permanently: The requested resource has been permanently moved to a new location.
  • 302 Found: The requested resource was temporarily moved to a new location.
  • 304 Not Modified: The resource has not been modified and can be cached.

4xx – Client Errors

  • 400 Bad Request: The request is incorrect and the server does not understand or cannot process the request.
  • 401 Unauthorized: Authentication is required to access the resource.
  • 403 Forbidden: Access to the resource is denied.
  • 404 Not Found: The requested resource was not found.

5xx - Server Errors

  • 500 Internal Server Error: The server encountered an unexpected error.
  • 502 Bad Gateway: The server, acting as a gateway or proxy, received an invalid response from the upstream server.
  • 503 Service Unavailable: The server is currently unable to process the request, usually temporarily.
  • 504 Gateway Timeout: The server, acting as a gateway or proxy, did not receive a response from the upstream server in time.

 

Algorithm: Return the nth value from the last in the linked list

Using the double pointer method, the fast pointer moves n steps ahead of the slow pointer, and then the fast and slow pointers start at the same time. Finally, when the fast pointer reaches the end, the slow pointer is the answer.

public class ListNode {
    int val;
    ListNode next;

    ListNode(int val) {
        this.val = val;
    }
}

public int findNthFromEnd(ListNode head, int n) {
    ListNode slow = head;
    ListNode fast = head;

    // Move the fast pointer forward n nodes
    for (int i = 0; i < n; i++) {
        if (fast == null) {
            return -1; // Handle the situation where n is greater than the length of the linked list
        }
        fast = fast.next;
    }

    //Move the slow and fast pointers at the same time until the fast pointer reaches the end of the linked list
    while (fast != null) {
        slow = slow.next;
        fast = fast.next;
    }

    // At this time, slow points to the nth node from the bottom
    return slow.val;
}

 

 

 

What is the difference between post and get requests? What is the content of the request body of post?

HTTP (Hypertext Transfer Protocol) is a protocol used to transfer data, which defines the communication rules between the client and the server. In HTTP, GET and POST are two common request methods, and they have some differences in usage and request body content.

GET request:

  1. Usage scenario: GET request is used to obtain data from the server, usually to obtain resources, query data, etc. It is idempotent, that is, executing the same GET request multiple times should have the same result and should not have side effects on the server.
  2. Request body: GET requests usually do not contain a request body. All parameters and data will be appended to the query string of the URL and appear at the end of the URL in the form of key-value pairs, for example: http://example.com/resource?param1 =value1¶m2=value2
  3. Data transmission: The data in the GET request is transmitted through the URL, so it can be seen directly in the browser, there is a length limit, and it is usually used to transmit small amounts of data.
  4. Security: GET requests have lower data security requirements because the data in the request can be easily viewed and modified, so they are not suitable for the transmission of sensitive information.

POST request:

  1. Usage scenarios: POST requests are usually used to submit data to the server, such as submitting forms, uploading files, etc. It is not idempotent, that is, executing the same POST request multiple times may produce different results and may have side effects on the server.
  2. Request body: The POST request contains a request body, which stores the data to be transmitted, which can be form data, JSON data, XML data, etc. There is no data length limit.
  3. Data transmission: The data in the POST request will not be displayed in the URL, but will be placed in the request body, so it is more suitable for transmitting large amounts of data, and it is also safer because the data will not be directly exposed in the URL.
  4. Security: POST requests have high data security requirements, because the data in the request body is not easy to view and modify, and is suitable for transmitting sensitive information.

Deep understanding

GET and POST are both http request methods, and the bottom layer is TCP/IP protocol; usually GET generates one TCP data packet; POST generates two TCP data packets (but Firefox sends one data packet). For GET request, the browser The http header and data will be sent together, and the server will respond with 200

(Return data) indicates success; for POST, the browser first sends the header, the server responds with 100, and then the browser continues to send data, and the server

The server responds with 200 (return data).

 

Can post request transmit data through url?

POST requests usually transmit data in the request body, rather than appending the data to the URL like GET requests. This is a typical use of POST requests, because POST requests are usually used to transmit larger amounts of data, such as form submissions, file uploads, etc. While you could theoretically append data to the URL, this is not standard practice for POST requests and may result in missing or incomplete data due to URL length restrictions.

 

How redis and mysql achieve data consistency

Setting the cache expiration time is the key point

1. All write operations are subject to the database. As long as the cache expiration time is reached, the cache will be deleted.

2. If there is another read request later, the new value will be read from the database and the cache will be backfilled.

 

Delayed double deletion strategy

Perform the redis.del(key) operation before and after writing the database, and set a reasonable timeout.

1. Delete the cache first 2. Then write to the database 3. Sleep for xxx milliseconds (depending on the specific business time) 4. Delete the cache again

 

Asynchronous update cache (based on Mysql binlog synchronization mechanism)

1. For updated data operations, use Mysql binlog for incremental subscription consumption.

2. Send the message to the message queue

3. Update incremental data to Redis through message queue consumption

4. Operation status

 

How to design redis cache

  1. Data storage strategy: Data structure selection: Choose an appropriate Redis data structure based on the characteristics of the data, such as strings, hash tables, lists, sets or ordered sets. Data partitioning: If you need to store a large amount of data, you can consider partitioning the data on multiple Redis nodes to expand storage capacity and performance.
  2. Cache update policy: Write policy: Determines when data is written to the cache. Common strategies include update when reading and writing, periodic synchronization, publish/subscribe model, etc. Data synchronization: Ensure that the data in the cache is consistent with the persistent data source (such as a database). Double writing, periodic synchronization or message queue can be used.
  3. Cache invalidation policy: Expiration time: Set an appropriate expiration time for the cache to ensure that data does not exist in the cache forever. Manual invalidation: When the data changes, the data in the cache is marked as invalid in time so that it can be obtained from the source data the next time it is read.
  4. Cache high availability: Master-slave replication: Use Redis's master-slave replication mechanism to ensure data backup and high availability. Sentinel mode: Use Redis Sentinel to monitor and manage Redis instances to automatically switch to the backup node when the primary node fails. Cluster mode: Use Redis Cluster to achieve data sharding and high availability.
  5. Cache penetration and avalanche prevention: Bloom filter: Use Bloom filter to prevent cache penetration, that is, querying non-existent data. Random expiration time: Add randomness when setting cache expiration time to avoid cache avalanche.
  6. Security: Restrict external access: Restrict external access to the Redis service through firewalls or other means to protect data security. Password authentication: Set a Redis password to ensure that only authorized users can access Redis.
  7. Backup and recovery: Regular backup: Back up Redis data regularly to deal with data loss or damage. Disaster recovery plan: Develop a disaster recovery plan to ensure that data can be quickly restored in the event of a serious failure.

Why does Linux need to set up user mode and kernel mode?

Among CPU instructions, some are very dangerous and will cause the system to crash if used incorrectly, such as clearing memory, setting the clock, etc. Therefore, the CPU divides instructions into privileged instructions and non-privileged instructions. For dangerous instructions, only the operating system and its related modules are allowed to be used, and ordinary applications can only use non-dangerous instructions.

  • Intel's CPU divides the privilege level into 4 levels: Ring0~Ring3, while Linux uses Ring3 level to run user mode and Ring0 as kernel mode.
  • When the operating system starts, the memory is divided. The data of the operating system is stored in the kernel space, and the data of the user process is stored in the user space. Programs at the user mode level can only access user space, while programs at the kernel mode level can access both user space and kernel space.
  • When a process executes a system call and is trapped in kernel code, we say that the process is in kernel mode.

 

 

What is the difference between cookies and sessions?

Storage location: Cookies: Cookies are small text files stored in the user's browser. Whenever a user visits a website, the server can send one or more Cookies to the user's browser, and the browser then saves these Cookies on the user's local computer. Session: Session data is usually stored on the server side. The server creates a unique identifier for each session (usually corresponding to an access session of a user), and then stores the relevant data in the server's memory or persists it to a database.

Security: Cookies: Cookies are stored in the user's browser and therefore can be modified or deleted by the user. Although encryption technology can be used to enhance the security of cookies, they are still vulnerable to attacks such as cross-site scripting (XSS). Session: Session data is stored on the server and cannot be directly accessed or modified by users. This makes Sessions generally more secure than Cookies, but server-side security is still critical.

Storage capacity: Cookie: Browsers have limitations on the storage capacity of cookies. Generally, the size of each cookie should not exceed a few KB. This limits the amount of data a cookie can store. Session: Server-side storage is usually larger and can store more data without being restricted by the browser.

Validity period: Cookie: You can set the expiration time of Cookie. Can be a session cookie (expires after the browser is closed) or a persistent cookie (expires after a certain period of time). Session: Session data usually expires automatically when the user closes the browser, but its validity can also be extended by setting an expiration time.

Purpose: Cookie: Mainly used to track user identity, record user preferences, shopping cart information, etc. They are typically used for client state management. Session: Used to maintain the user's session state on the server side, usually used for server-side state management such as authentication, authorization, and shopping cart management.

Cross-page delivery: Cookies: Data can be passed between different pages because they are stored in the browser and can be read by JavaScript code on different pages. Session: Session data is usually stored on the server and can be shared between different pages on the server, but needs to be accessed through a session identifier.

 

lock mechanism

Specifically, file locks are generally divided into two types:

  1. Shared Lock: Multiple processes or threads can acquire shared locks on files at the same time for read operations. Shared locks do not block read operations by other processes or threads, but they do block write operations.
  2. Exclusive Lock: Only one process or thread can obtain an exclusive lock on a file for write operations. Exclusive locks block read and write operations from other processes or threads.

 

http and http protocol

Security: HTTP: HTTP is an insecure protocol. All data is transmitted in clear text and can easily be intercepted by man-in-the-middle attacks and steal sensitive information. HTTPS: HTTPS adds security features to the HTTP protocol and uses the SSL/TLS protocol to encrypt and authenticate data. This means that in HTTPS communication, the data is encrypted during transmission and cannot be easily stolen or tampered with, thus providing higher security.

Encryption: HTTP: HTTP does not provide data encryption, so all data is transmitted in clear text. HTTPS: HTTPS uses the SSL/TLS protocol to encrypt communication data and ensure that data remains confidential during transmission.

Authentication: HTTP: HTTP does not provide a server authentication mechanism, making it difficult to ensure that the connecting endpoint is connected to the intended server. HTTPS: HTTPS uses a digital certificate to verify the identity of the server and ensure that you are connecting to the correct server. This prevents man-in-the-middle attacks.

 

Why should we divide processes and threads?

  1. Resource isolation: Process: Each process has its own independent memory space, so the data between processes is isolated from each other. This means that a bug or crash in one process usually does not affect other processes. Threads: Threads share the memory space of the same process, so they can share data and communicate more easily. But because of this, an error in one thread may affect other threads in the same process.
  2. Creation and destruction overhead: Processes: Creating and destroying processes is usually time-consuming because each process needs to allocate independent memory space and system resources. Threads: The overhead of creating and destroying threads is relatively small because they share the same process resources.
  3. Concurrency: Process: Concurrency between processes is low because process switching involves context switching and resource reallocation. Threads: Concurrency between threads is higher because they can share data and communicate more easily without switching processes.
  4. Multi-Core Utilization: Processes: Multi-core processors can execute multiple processes in parallel, thereby improving the performance of the system. Threads: Multi-core processors can execute multiple threads in parallel, making more efficient use of multi-core resources.
  5. Task distribution: Process: usually used to perform independent tasks and does not need to share data frequently. Threads: Usually used to perform subtasks related to the main task and need to share data frequently.

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_51118755/article/details/135307403