A Baidu interview question

Source: Internet
Author: User
Q: there are 10 thousand workers in plant a, numbered 0-9999, (EE [10000]), 1 factory director (gg) assigned tasks, and 1 Supervisor (MM) Administrator. the factory was too busy for a period of uncertainty. It may suddenly be very busy. After receiving more than 5000 tasks in one day, one task could only be assigned to one worker, or there may be no new tasks in dozens of days. the director assigned tasks to the 10 thousand workers and assigned them one by number. The last worker started from the beginning and the task completion time was different, A worker may have another task when assigning it.

However, all the 10 thousand workers are very lazy. If they receive the task, they will not do it first. They need to supervise one worker and ask, if the worker has the task, they will do it. If the worker has no task, they will not do it. The plant manager only divides tasks into one task, and may not have any new tasks in a few days. However, the supervisor is very tired. The supervisor needs to check the situation of all workers every day, even if these workers do not have any tasks, in fact, every day, there are no tasks for workers (about 80%). Could you tell me how to make the work of the supervisor easy. for example, I only ask a half worker every day.

Peak Wong:

The analysis is as follows:

Because the task completion time is different, it is possible that A, B, and C have a task on a certain day, But B is the first to complete the task. After B is completed, the employee ID of the person with the task may be discontinuous;

Use an array to indicate whether 10 thousand workers have tasks and save the employee ID of the last person to be assigned tasks;

1) from the previous day's "employee ID of the person with the last assigned task", ask the person with the next employee ID in sequence, and set the corresponding working status until the employee does not have a job on the previous day, persons who do not have a job on the day, and the employee ID of the person who has a job at the end of the step is the "employee ID of the person who has the last assigned job" of the day ";

2) from the previous day's "employee ID of the person with the last assigned task", ask the person with the previous employee ID and the previous day's work;

The problem is that the supervisor can know the information, otherwise it will not ask one by one.
There is also the time consumed by step 1 of tailzhou T1, the time consumed by the worker to complete T2, if T2

Therefore, there are no restrictions on many conditions.

But think about it carefully, that is, the supervisor should record the working status of all workers, and then only query the workers at work every day. (And record who is still at work)

In fact, the most fundamental solution is that each time the Director assigns a task, the supervisor is also notified of who has been assigned the task. Now the question is assumed that the director is too busy to notify the Supervisor (in fact, just assign the supervisor ).

It is easy to solve this problem. The supervisor only needs to record (or he may guess) the person who finally assigned the assignment to the supervisor.

Then, during daily queries, in addition to supervising the persons who came to work the previous day, they also need to check the workers who started from the next number assigned to the task last time and query them in sequence, I know that I have met a person who has not been assigned a job (this employee will not be assigned another job ). at the same time, the supervisor should record or guess which worker is the last assigned task of the day. In the process of "viewing workers starting from the next number assigned to the task last time, query them in order", if the workers are credible, ask them. If it is not credible, you can guess that the last one was idle yesterday, and today's busy workers can do it.

In fact, since the supervisor can supervise the work, he has to have a channel to know that the worker had to do the work on that day (otherwise all the workers can say that today I have no task), so there is no such complicated problem.

Solution:

Q: Will the supervisor finish the work all the time?

In this case, the simplest consideration is to remember whether the worker can finish the work today when he first asks.

In fact, the supervisor only needs to ask the question the next day after the worker completes the work. The Director will not assign him new tasks because he does not finish the task.

But the question is: But the invigilator is very tired. Every day, the invigilator depends on the situation of all workers. Even if these workers do not have a task, it is different. That is to say, the invigilator must ask every day. Otherwise, the worker will not start working.

At this time, there is no need to ask the workers who are not working.

First, let's talk about the work queue. Everyone asks, and what can be done today is moved to the idle queue.

Idle queue: searches for non-idle queues in binary mode. After processing, the tasks can be completed today and cannot be completed and moved to the work queue.

A very simple question. There are very few people to ask for binary search.

It can satisfy the requirements of the landlord: for example, if only one half of the workers are asked every day, the supervisor does not need to be notified when I use my method to divide the jobs.

In fact, the notification does not make sense, because it is distributed in order, the invigilator actually needs to know how many new tasks are available today.

Even if you do not know, it takes up to 10 times to search for 10000 users in binary mode.

In the past 10 times, there are no tasks for workers (about 80%) every day. Therefore, there are only about 20% people asked every day.

1. Problem Analysis

The current situation is that the invigilator checks all workers every day and urges them to work because they do not need to start work, that is, they need to access every element of EE [10000] once. The goal is to only ask half a worker every day. In fact, workers who do not have a job do not need to ask. The ideal situation is that the supervisor only asks workers who have a job, or ask as few workers as possible, that is, access EE [10000] elements as little as possible.

What should we do? The supervisor thought of a solution. He made 10 thousand cards with the worker number on each card, ranging from 0 to 9999, matching the subscript of the array EE [10000.

The invigilator got into battle with his secret weapon. No. 0. Is there a job? No. Good. Put it in the right pocket. 1. Is there a job? Yes. Can I finish it today? Yes. Well, put it in the right pocket (and put it behind the 0 ). 2. Is there a job? Yes. Can I finish it today? No. Well, put it on the left. No. 3. Right side (after 1 ). No. 4. You can't finish it today. Leave it on the left ....... The next day, first look at the right side (yesterday was okay), No. 0, there is no job, yes, can you finish it today, OK, don't move. I have a job on the first day. I have a job, but I can't finish it. Well, put it on the left (and then put it on the back of yesterday ). No. 3, No. Oh, the Director Gg hasn't been assigned to the site yet. You can check the blank space tomorrow from here (remember), but it's still hard today, because it may be from the back of the number and allocated to the front. The right side is all checked. Check the left. 2. You can finish it today. Put it on the right side (and put it behind 0 ). Ah, I met the first day. Today's check is over. On the third day, it was easy. Start from 3 and check the right. After the task is finished today, you can't move it over. You can move it to the back of the left and check the right side if there is no task or 3. Let's look at what we have done on the left, move it to the right, and insert other cards in sequence. we can't finish it, don't move it until we can't finish what we 've added today, or the cards on the left are all checked.

After talking about this, there is actually a working queue on the left and a non-working queue on the right, the difference between the left and right is that the right should be sorted by number (because the manager Gg assigns tasks in order ). Take another pencil and mark it on some cards. Work is much easier. It is assumed that the worker must be reminded to work on a daily basis, and the supervisor will be reminded after the Director assigns a task every day. If a worker keeps working after a reminder, the supervisor only needs to write down the card for a few days before asking. If the supervisor sends a reminder every morning, the supervisor may score tasks early in the morning (before asking) or in the afternoon (after asking, but the worker has not finished working.

If it is the former, of course there is no problem. If it is the latter's problem, it will be completed only today, but it will also be placed on the right side. If the director has just divided the second and fourth places, so according to the above logic, the 4 th cannot be pushed.

So in order to avoid this situation, you cannot leave it in idle space. Well, just prepare another pocket. Fortunately, there are two pockets on the clothes of the supervisor, and two on the bottom. The following two are used, and two on the top are not used. Just use one.

When checking the lower left and right pockets, all the items that can be done on the day are placed on the top left. OK, first lower right, then lower left, and then put the upper right corner of the page that can be finished on the current day, and then insert the upper left to the lower right in sequence. It should be okay. No matter when the Director divides tasks, the supervisor only needs to look at his pocket. In the lower right corner of the page, you need to access the website from the first time you visited the website yesterday, to a new one. Access is required in the lower left corner. Cards on the upper right or upper left only need to be organized. In fact, every day, the workers (about 80%) have no tasks, both in the lower right corner, and may have no new tasks for dozens of days. This is easy, only a small number of workers can ensure normal operation.

Well, if you want to simulate it first, use the brain to simulate it. There are too many people to think about it. Write a program. To write a program, you must first have an algorithm.

2. Algorithm

The following Algorithm simulates the most common situation, that is, the supervisor does not know when the supervisor divides tasks, and the supervisor can divide tasks at any time of a day. The worker can only ask for work every day, the supervisor sends reminders only in the morning (to simplify the calculation of work hours, that is, the work hours are in the unit of days ).

Completely Random start point simulation, that is, this method can start from the time point any supervisor wants to use this method.

Assume that a task is completed in 1-N days, and the number of new tasks received by the factory in one day is 0-M. Simulate a day in T minutes. The timer is accurate to milliseconds.

A. initialize EE [10000] With a number between 0 and N to simulate the working status of the current worker. EE [I] indicates how many days the worker has to complete the task. EE [I] = 0, indicating no task.

B. Set the timer. The timer of the plant manager is 1-T * 60*1000 milliseconds, and the timer of the supervisor is set to T * 60*1000 milliseconds.

C. When the plant manager timer arrives, the plant manager divides tasks. Use C to record where the plant manager starts. The first time there was a random initialization project, a random number between 0 and 99, then find the first EE [I] = 0 I, from this c = I start to allocate.

0-M is randomly generated. If it is 0, c = I will not change. If it is a value between 1-M, query one by one and if it is EE [I] = 0, give him a random value of 1-N until these tasks are completed and c = + 1 is finally allocated. The problem with the Research supervisor is that the director is relatively easy and will ask him to continue looking for it next time.

Reset the plant manager timer, which is scheduled to be T * 60*1000-at the time specified last time, plus a period of 1-T * 60*1000 milliseconds, because the plant manager will only divide the timer once a day, therefore, we need to use up the time of today and add a certain time of the next day. (We can see from the previous figure that the timer of the factory director is set to T. Just consider accessing shared data, this issue is not considered here ).

D. When the supervisor timer arrives, the supervisor asks the worker.

Create four linked lists. A (not finished yet), B (not working), C (finished today), D (finished today), and Initialization is empty. But B is an ordered linked list. C and D are used in turn.

From the first scheduled time, access EE [10000]. What I can't do today (EE [I]> 1) is received to a's tail and what I can do (EE [I] = 1) on the tail of C, no working (EE [I] = 0) is connected to B, and D is empty.

For the second time, access B. If you cannot finish it today, it will be connected to the tail of a. If you can finish it, it will be connected to the tail of D, and record the node pointer P of the person with a job before the person without a job. If there is no such person, it is the first person in the linked list or empty (everyone has a job ). However, the whole linked list B must be accessed in any case.

Access to a, cannot do not move, can be completed to the end of D, and finally Insert the elements in c B. Note that the elements in the linked list are unique, that is, when moving to another linked list, it also means deleting from the original linked list.

The third time, starting from P, accesses the elements in B. Today, the elements cannot be connected to the tail of a, but the elements can be connected to the tail of C, until you find a job that is not working, or you have already searched it all (adjust P to the new position when searching ). After the last linked list, you may need to find it from the beginning.

Access to a, cannot do not move, can be done to connect to the C tail, and finally Insert the elements in D into B.

The fourth scheduled time is similar to the second one, but the C and D positions are reversed. At this time, the work of the supervisor has been easy, and the whole system will continue to run in this new way.

The timer of the supervisor does not need to be reset.

The element content in A, B, C, and D is a worker ID. The access syntax is similar to If (EE [p-> Index]> 1 ).
 

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.