Interview Experience-Xiaomi

Source: Internet
Author: User

Side:
1. Quick sort, handwritten code.
2. Heap sort, describe the process, draw it on paper.
3. Singleton mode: Lazy mode and a hungry man mode.
4. Thread pool Implementation principle, the data structure used, how to dispatch resources in the pool.
5. What command is used to find a file name?
6. What is the command to delete a file and everything below?
7. SQL statement: Create a table.
Two sides:
8. Given an array, the array contains a reference to the thread, according to the data structure, the design of a thread pool scheme, the need to remove the idle thread and the return of the thread, the complexity is O (1), and the requested secondary space is a constant number of levels.
9. Implement a doubly linked list to find intersecting nodes. (I have implemented 3 ways)

Answer:

  1. Quick Line
private void sort (int start, int end, int[] num) { if(start<end) { int begin = start,last< /c11> = end;int tree = num[ start];While start<end) { for (; End>start && num[end]>=tree; End--);num[ start] = num[end];for (; End>start && num[start]<=tree; start+ +);num[ End] = num[start];} num[ start] = tree;Sort begin,start-1, num);Sort Start+1,last, num);}    }
  1. Heap Sort
    Heap sorting algorithm, there is no more thorough than this one, the analysis is in place.
    The best data structure of the storage heap is the array, can directly find the parent node or the corresponding two child nodes, to operate.
    In the course of the interview, the interviewer only asked me the process of building a small top heap; The process is generally composed of two aspects:
    • One: The current node and the sibling and its parent elements are compared, and exchanged (if necessary);
    • : After the exchange, the value of the current position is adjusted to the value of the child node of the parent node, and is combed to the appropriate size level;
  1. Single-Case mode:
    Lazy Mode (a singleton is generated the first time it is used, and the class is loaded without generating a singleton object, which is null at this time):
publicclass Singleton {    privatestaticnull;    privateSingleton() {//私有构造方法,防止该类被实例化。    }    publicstaticgetInstance(){        if(singleton==null){        sychronized(singleton){        singleton=new Singleton();        }        }        return singleton;    }}

A hungry man mode: A singleton object is generated when the class is first loaded (note, not used).

publicclass Singleton {    privatestaticnew Singleton();    privateSingleton() {//私有构造方法,防止该类被实例化。    }    publicstaticgetInstance(){        return singleton;    }}
  1. Implementation elements of the thread pool:

queues, arrays, threads (thread), get and Giveback methods, and so on.

    • The array implements the control of the number of threads and the tracking of each thread.
    • A queue is a task queue that represents a candidate task that will be assigned to a thread pool.
    • Threads are the threads that hold every thread in a threaded pool and need to be used to take out
    • After running and running, put back to the pool for standby;
    • Get method: When the thread pool still owns the spare thread, a free thread is obtained, the load task runs, and the idle record is marked as busy;
    • The Giveback method is used when an idle thread in the thread pool does not reach the pool capacity, and when a thread is idle, it is placed back into the thread pool for standby, and the thread is added to the idle record;
  1. Find a file with a name using the Find command

There was no answer at the time. See the Find command.

  1. Delete a folder and all of the following files command: RM-RF

  2. The SQL statement creates a table student,id as a self-increment primary key, name is a string, and age is an int type.

It was not correct at that time. Should be:

CREATE TABLE student(id int identity(1,1) PRIMARY KEY,name varchar(32)  NOT NULL, age int NOt NULL) 
  1. Given an array, the array contains a reference to the thread, according to the data structure, the design of a thread pool scheme requires that when the idle thread is removed and the thread is returned, the complexity is O (1), and the requested secondary space is a constant number of levels.

As required, you need to put it back or out in O (1) time, and you cannot request an O (n) secondary space, but there can be some marked fields in the task (isbusy< busy >,originIdx< corresponding array position subscript >, etc.).
After consideration, I can do this:
Each time it is removed and put back in order, but because the array subscript is recorded in each task, and the array corresponding subscript has a reference to the task, then when the task is not in order to return the thread, the thread and another running thread location can be swapped to achieve the effect of order restitution, At the same time modify the two swap threads about the busy and the value of the subordinate, so that when the removal can be done in order to achieve O (1), put back when you can put in order, in the complexity of O (1), the exchange of two threads to the location can be resolved.
The analysis chart is as follows:

    • At the beginning, 0 to 10 are idle, then set up the pointer point to the busy area and the free area of the boundary line, then the pointer should be 0;
    • When assigned, pointer++, becomes 1, then assigns the pool[0];
      If necessary, the pointer point is assigned, and then pointer++;
    • A moment, assuming that pointer points to 5, then 0 to 4 are busy;
      At this point, if pool[2] corresponds to the task class back, it should be placed in pool[4], pointer–, representing the idle starting from 4th.
    • At the same time, assuming that the Task4 corresponds to the original from Pool[4] assigned to the task, and 4 has not come back, 2 came back to occupy 4 of the position, then need to Exchange Task2 and TASK4 originidx information, let Task2 point 4, let Task4 point 2 And let the pool[2] and pool[4] references also be swapped, so that at each recovery time through this exchange position operation to reach O (1).
    • Write the Get () and Giveback () methods according to the idea.
  1. Double linked list to determine if there is a cross
    • Method 1: Use set to hold a reference to the ListNode, traverse each node to find whether the node is already in the set, and if no duplicates are found after the traversal, there is no intersection;

      The examiner proposed that if there is no additional O (n) of auxiliary Memory:

    • Method 2: Link the last node to the second head, thus converting the problem to determine if there is a loop problem, set the fast pointer, quick Pointer 2 step at a time, slow the pointer once 1 steps, until the fast pointer is null or fast pointer = = Slow pointer. The null description is no ring, and the equivalent indicates that the speed pointer encounters a loop, so there is a crossover.

      The examiner also put forward, can not change the structure of the original linked list

    • Method 3: First to find out the length of two linked lists, the difference between the length of the distance, and then define two pointers to go down at the same time, do not walk once compared to each other, until you go to null or equal. If NULL, there is no intersection;

This concludes the two rounds of interviews. Exam questions are very basic, no wonder, the foundation also need to play solid.

Interview Experience-Xiaomi

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.