ByteDance Backend Internship - 2019

ByteDance Backend Internship - 2019

one side

algorithm problem

The title above "Swordsman Offer":

Question: Find the number of occurrences of a number in an array that is more than half the length of the array. For example, enter an array of length 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears in the array 5 times, which is more than half the length of the array, output 2.

The interviewer asked me to tear the code by hand. I wrote a way to implement it with map, which is to traverse it from the beginning to the end, and then count the number with the most occurrences, and keep the maximum value during the statistical process. In this case, the time complexity is O ( n ) O(n)O ( n ) .

But after finishing it, I felt that the interviewer was not very satisfied. Of course, after I went back, I searched for the answer, probably using a PK method to achieve it. The number of times a certain number appeared more than half, indicating that it is more than the sum of all other numbers. If both are larger, in the process of traversing the array, keep two variables: the counter cnt and the current number cur. This is similar to a competition in the ring. If you have your own appearance, you will add a vote. If you are not your own, you will see a vote. If the current number is the same as the previous number, add 1 to cnt; otherwise, check the value of cnt, if it is 0 , then replace cur and add 1 to cnt, otherwise, subtract 1 to cnt, so the code is as follows:

int findNumHalf(int* arr, int len) {
    
    
  	if (arr == NULL || len <= 0) {
    
    
    	return -1// Invalid.
  	}
  
  	int cur = arr[0];
  	int cnt = 0;
  
  	for (int i = 1; i < len; i++) {
    
    
    	if (cur == arr[i]) {
    
    
      		cnt++;
    	}
    	else if (cnt == 0) {
    
    
      		cur = arr[i];
      		cnt++;
    	}
    	else {
    
    
      		cnt--;
    	}
  	}	
  
  return cur;
}

operating system

  1. First, I asked about LRU and LFU in the page scheduling algorithm, and then gave some examples to explain which page should be adjusted recently, simple.
  2. C++ program memory structure
    • .text: store the source code
    • .rodata: store constants
    • .data: store initialized global variables and static variables
    • .bss: Stores uninitialized global variables and static variables
    • .heap: store variables controlled by functions such as malloc, realloc, free, etc.
    • .stack: The stack is used to save the function scene when the function is called, and the local variables are also stored in the stack
  3. Ways of inter-process communication: semaphores, pipes...
  4. Memory paging and how to deal with memory fragmentation
  5. First-fit,Worst-fit,Best-fit

computer network

  1. Asked about the details of the three-way handshake and four-wave wave:

    • Complete process, related packages and status

    • Why use four waves

    • What is the state of the hand wave?

    • Why does the client wait for a while after receiving the FIN from the server, and how long does it wait?

    • What attacks will be encountered during the three-way handshake

  2. Have you ever heard of Dos attacks?

  3. Ever heard of CSRF (no)

  4. Have you ever heard of XSS (understood, but forgot)

  5. Difference Between HTTP and HTTPs

  6. What is the encryption algorithm of HTTPs?

  7. The meaning of the status code:

    • 200: success
    • 403: The server received the request, but refused to serve
    • 404: The requested page does not exist
    • 503: The server is currently unable to complete the client's request, but after a period of time, the server may return to normal

database

  1. What is the underlying structure of the index (B+ tree, hash)
  2. What is the structure of B+ tree (drawing explanation)
  3. what is a compound index (forgot)
  4. Given several tables, ask which columns to build the index on
  5. How to optimize the database
  6. What are the engines of the database (MyISAM, InnoDB), what are the similarities and differences
  7. How to check the efficiency of the index (explain) (no)
  8. Have you heard of pessimistic locking and optimistic locking (no)

Linux

  1. Have you ever used top (no)

two sides

algorithm problem

  1. Implement quicksort (simple)
  2. Realize the merging of two ordered linked lists (the sword refers to the title on the offer, which is relatively simple)

C++

  1. Types of C++ polymorphism (compile-time polymorphism and run-time polymorphism)
  2. Q What are the categories of compile-time polymorphism: function overloading and generic programming
  3. Ask if int func(int, int) and double func(int, int) are legal (illegal)
  4. Ask the bottom layer of virtual functions (virtual function table)

operating system

  1. Ask what are the inter-process communication (balabala...), compare the efficiency (not compared)

database

  1. The isolation level of the database (read uncommitted, read committed, repeatable read, serializable)
  2. Characteristics of Transactions (ACID)
  3. interpretation consistency
  4. explain isolation

Linux

  1. The use of top (reviewed, will use)
  2. The meaning of the content displayed on top
    1. VIRT: virtual memory usage
    2. RES: physical memory usage
    3. SHR: Shared memory usage
    4. %MEM: memory usage
  3. Which statements have been used, and said a lot, such as netstat, ps, vmstat, etc.
  4. How to view shared memory (answered top)

Three sides

There is one question in the interview on three sides, and the approximate meaning is as follows:

Design a resource library that implements the following three functions in O(1) time complexity:

  • Get a resource from the resource library
  • Return the assigned resource to the resource pool
  • Random access to a resource in the resource pool, both allocated and unallocated

After seeing the title, I didn't think much about it, and I ended up writing some stupid code, which the interviewer couldn't understand... Afterwards, I found that it was quite simple, hang up.

The idea is to use a vector to represent resources, use a ptr to point to the following table, the left is allocated, the right is unallocated, each time one is allocated, the ptr moves one bit to the right, and the efficiency is O ( 1 ) O(1)O ( 1 ) , use a map to record the previous element allocation, the key is the element corresponding to the following table, the value is the subscript, when someone wants to change the resource, ptr takes a step back, and the element where the ptr is located is exchanged with the target element , the records in the map are also exchanged, and the efficiency is alsoO ( 1 ) O(1)O ( 1 ) , for random access, use rand and return the subscript randomly.

At that time, I thought about it, since map was used, I used a queue and an array directly at that time. When the initialization was done, it corresponded well. Wouldn’t it be possible to access random access, push, and pop…

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324080778&siteId=291194637