Inventory of Didi Autumn Recruitment C++ Interview Questions and Answers in 2020 (Part 1)

1. C++ realizes thread-safe singleton mode

Slacker mode:

class singleton
{
    
    
protected:
 singleton()
 {
    
    
 pthread_mutex_init(&mutex);
 }
private:
 static singleton* p;
public:
 static pthread_mutex_t mutex;
 static singleton* initance();
};
 
pthread_mutex_t singleton::mutex;
singleton* singleton::p = NULL;
singleton* singleton::initance()
{
    
    
 if (p == NULL)
 {
    
    
 pthread_mutex_lock(&mutex);
 if (p == NULL)
 p = new singleton();
 pthread_mutex_unlock(&mutex);
 }
 return p;
}

2. The number of 1-2n is stored in an array with space n, and the number that appears twice is found. The time complexity is O(n), and the space complexity is O(1)

/*
奇数零次 偶数零次 0
奇数 一次 偶数 零次 -1
奇数 两次 偶数 零次 -2
奇数 零次 偶数 一次-3
奇数 一次 偶数 一次-4
奇数 两次 偶数 一次-5
奇数 零次 偶数 两次-6
奇数一次 偶数 两次-7
奇数两次 偶数两次-8
*/ 
public class Main {
    
    
 public static void main(String[] args) {
    
    
 int[] nums = {
    
    1, 3, 5, 15, 7, 8, 5, 3, 6, 15};
 findNumber(nums, nums.length);
 print(nums);
 }
 
 public static void findNumber(int[] nums, int length) {
    
    
 for (int i = 0; i < length; ) {
    
    
 if (nums[i] <= 0){
    
    
 i++;
 continue;
 }
 int index = nums[i] / 2;
 boolean isOdd = (nums[i] % 2 == 0) ? false : true;
 switch (nums[index]) {
    
    
 case 0:
 if (isOdd == true) {
    
    
 nums[index] = -1;
 } else {
    
    
 nums[index] = -3;
 }
 break;
 case -1:
 if (isOdd == true) {
    
    
 nums[index] = -2;
 } else {
    
    
 nums[index] = -4;
 }
 break;
 case -2:
 nums[index] = -3;
 break;
 case -3:
 if (isOdd == true) {
    
    
 nums[index] = -4;
 } else {
    
    
 nums[index] = -6;
 }
 break;
 case -4:
 if (isOdd == true) {
    
    
 nums[index] = -5;
 } else {
    
    
 nums[index] = -7;
 }
 break;
 case -5:
 nums[index] = -6;
 break;
 case -6:
  nums[index] = -7;
 break;
 case -7:
 nums[index] = -8;
 break;
 default:
 swap(nums, i, index);
 if (isOdd) {
    
    
 nums[index] = -1;
 } else {
    
    
 nums[index] = -3;
 }
 continue;
 }
 nums[i++]=0;
 }
 }
 public static void swap(int[] nums, int i, int j){
    
    
 int temp =nums[i];
 nums[i]=nums[j];
 nums[j]=temp;
 }
 
 public static void print(int[] nums) {
    
    
 for (int i = 0; i < nums.length; i++) {
    
    
 if (nums[i] == -2) {
    
    
 System.out.println(i * 2 + 1);
 } else if (nums[i] == -6) {
    
    
 System.out.println(i * 2);
 } else if (nums[i] == -8) {
    
    
 System.out.println(i * 2);
 System.out.println(i * 2 + 1);
 }
 }
 }
}

First-line Internet companies complete interview questions and answers follow the official account of Zero Sound Academy to receive them for free!
Insert picture description here

3. How does partition work in Kafka?

Kafka cluster partition replication default automatic allocation analysis

The following is an example of 4 Brokers in a Kafka cluster. Creating a topic contains 4 Partitions and 2 Replications; the data producer flow is shown in the figure:

When 2 nodes are added to the cluster and the partition increases to 6, the distribution is as follows:

The logical rules for copy allocation are as follows:

In a Kafka cluster, each Broker has an equal opportunity to allocate Partition Leaders.

In the Broker Partition in the above figure, the arrow points to the copy. Take Partition-0 as an example: partition-0 in broker1 is the leader, and Partition-0 in Broker2 is the copy.

In the above diagram, each Broker (ordered according to BrokerId) allocates the main Partition in turn, and the next Broker is the copy. In this way, iteratively allocates, and multiple copies follow this rule.

The copy allocation algorithm is as follows:

Sort all N Brokers and i Partitions to be allocated.

Assign the i-th Partition to the (i mod n)-th Broker.

Assign the jth copy of the ith Partition to the ((i + j) mod n) Broker.

4. Shell script counts the number of words in the file

method one:

(1)cat file|sed ‘s/[,.:;/!?]/ /g’|awk '{for(i=1;i<=NF;i++)array[$i]++;}

END{for(i in array) print i,array[i]}’

#File is the file to be operated, and there is a space between // in sed.

(2)sed ‘s/[,.:;/!?]/ /g’ file|awk '{for(i=1;i<=NF;i++)array[$i]++;}

END{for(i in array) print i,array[i]}’

#(1) and (2) have the same effect.

Method Two:

(1)awk 'BEGIN{RS="[,.:;/!?]"}{for(i=1;i<=NF;i++)array[$i]++;}

END{for(i in array) print i,array[i]}’ file

5. What are the IPC under linux

①Anonymous pipe (PIPE) and famous pipe (FIFO): the simplest

②Signal (SIGNAL): the system has the least overhead

③Shared mapping area (MMAP): can communicate between unrelated processes

④Local socket (SOCKET): the most stable (but more complicated)

  1. What are the architectural patterns of Redis?
    One, Redis stand-alone mode

Features: Simple, just run redis-server redis.conf directly (pay attention to the location of your redis-server file and redis.conf file).

Disadvantages: 1. Limited memory capacity 2. Limited processing power 3. No high availability.

Two, Redis master-slave replication mode

The replication function of Redis allows users to create any number of replicas of the server based on a Redis server. The replicated server is the master server, and the server replica created by replication is the slave server ( slave). As long as the network connection between the master and slave servers is normal, the master and slave servers will have the same data, and the master server will always synchronize the data updates that happen to it to the slave server, so as to ensure that the data of the master and slave servers are the same.

Features:

1. The role of master/slave

2. The master/slave data is the same

3. Reduce the pressure of master reading while transferring to slave library

Disadvantages:

1. High availability cannot be guaranteed

2. Did not solve the pressure of master writing

Three, Redis sentinel (Sentinel) mode

3.1 Overview of Sentinel Mode

Redis sentinel is a distributed system that monitors the redis master and slave servers, and automatically fails over when the master server goes offline.

Three of the characteristics:

Monitoring: Sentinel will constantly check whether your master server and slave server are operating normally.

Notification: When a monitored Redis server has a problem, Sentinel can send notifications to administrators or other applications through the API.

Automatic failover: When a primary server fails to work normally, Sentinel will start an automatic failover operation.

Features:

1. Ensure high availability

2. Monitor each node

3. Automatic fault migration

Disadvantages:

Master-slave mode, switching takes time to lose data

Did not solve the pressure of master writing

4. Cluster (proxy type) mode

Twem proxy is a fast/lightweight proxy server for redis and memcache open sourced by Twitter; Twemproxy is a fast single-threaded proxy program that supports Memcached ASCII protocol and redis protocol.

Features:

1. Multiple hash algorithms: MD5, CRC16, CRC32, CRC32a, hsieh, murmur, Jenkins

2. Support automatic deletion of failed nodes

3. The back-end Sharding sharding logic is transparent to the business, and the business side's read and write methods are consistent with the operation of a single Redis

Disadvantages:

1. A new proxy is added, and its high availability needs to be maintained

2. The failover logic needs to be implemented by itself, and it cannot support the automatic transfer of failures. The scalability is poor, and manual intervention is required for expansion and contraction.

5. Cluster (direct connection) mode

Versions after redis 3.0 support redis-cluster clusters. Redis-Cluster adopts a non-centralized structure. Each node saves data and the entire cluster state, and each node is in harmony.

All other nodes are connected.

Features:

1. No central architecture (no node affects the performance bottleneck), and the proxy layer is missing.

2. Data is stored in multiple nodes according to the slot, and data is shared between nodes, and the data distribution can be dynamically adjusted.

3. Scalability, linear expansion to 1000 nodes, nodes can be added or deleted dynamically.

4. High availability. When some nodes are unavailable, the cluster is still available. Make backup data copy by adding Slave

5. Realize automatic failover of failures, exchange status information between nodes through the gossip protocol, and use the voting mechanism to complete the role upgrade from Slave to Master.

Disadvantages:

1. Resource isolation is poor, and mutual influence is prone to occur.

2. Data is replicated asynchronously, and strong data consistency is not guaranteed.

Guess you like

Origin blog.csdn.net/lingshengxueyuan/article/details/108295857