1.5 quickly identify Machine Faults

Source: Internet
Author: User

Question: assume that one machine stores only one ID record, and two backups are saved for each data record. In this way, two machines store the same data. The ID is an integer smaller than 1 billion.

Question 1: at a certain time, if you get a list of data file IDs. Can I quickly find the ID that appears only once in this table? That is, you can quickly find the data ID of the faulty machine.

Question 2: What if two machines have crashed? (Assume that two copies of the same data are not lost at the same time, that is, two unequal IDs are missing in the list)

Extended question: if all the machines have three backups, that is, the machines with the same Id have three backups. At the same time, there are three crashed hosts. Can the above solution be used?

What if there are n backups and N machines are dead at the same time?

~~~~~~~~~~~~~~~~Split line~~~~~~~~~~~~~~~~~~~~~~

Reelaborate on the problem:

Problem 1: An array is known. Only one data in the array appears once, and the other data appears twice. We need to find this data.

Problem 2: An array is known, and two different data entries appear in the array and the other data appears twice. We need to find the two data entries.

Problem 3: An array is known and three data items are lost in the array. We need to find the three data items.

Then, it can be expanded to n

~~~~~~~~~~~~~~~~~Split line~~~~~~~~~~~~~~~~~~~~~

Problem 1: An array is known. Only one data in the array appears once, and the other data appears twice. We need to find this data.

Method 1: sort by count (using map)

Idea: traverse the entire ID list and use map to record the number of occurrences of each ID. After that, the request ID appears only once

Time complexity O (N), space complexity O (N)

Note: Without map, if arrays are used, the space complexity will be greater than n (n indicates n numbers), which should be 1 billion (the maximum value of ID may be 1 billion)

Disadvantages:The space is too complex. You need to save the ID that has already existed twice, but it is no longer a faulty machine.

Method 2: sort by count, but do not store

Thoughts:Traverse the list. For each ID, check whether the hash table has the same ID.

If yes, the ID is deleted from the hash table;

Otherwise, add the ID to the hash table.

In this way, after the list is traversed, the remaining element in the hash table is the requested ID.

Time complexity O (N), space complexity is preferably O (1), and worst is O (n)

Method 3: Use the exclusive or operation (recommended)

Thoughts:Returns the value of all IDs in the list.

The exclusive or operation can be used to obtain

X ^ x = 0 x ^ y = z x ^ 0 = x

The time complexity is O (n), and the space complexity is O (1 ). In terms of time and space, the optimal performance has been achieved.

Disadvantages:The premise is that only one ID appears once. If it appears multiple times, it is not suitable

Method 4: Use the "invariant" (recommended)

Ideas:Here, the sum of all IDS is a constant and sums the remaining IDs. The sum of all IDs and the sum of the remaining IDS is the desired ID.

Time Complexity: O (n) time, space complexity O (1)

Conclusion: The use of XOR and invariant has been optimized. The list can be traversed only once, and only one variable is needed.

~~~~~~~~~~~~~~~~~Split line~~~~~~~~~~~~~~~~~~~~~

Problem 2: An array is known, and two different data entries appear in the array and the other data appears twice. We need to find the two data entries.

Two different data types are lost.

If the missing two numbers are different,

Method: perform an exception or operation

Ideas:Because the number of missing values is different, the result of the last XOR is not 0.

  1. (1) perform the XOR operation on all IDs in the array, and the result is.
  2. (2) In the binary representation of a, we find B at the lowest position of 1.
  3. (3) Based on whether B is 1, the number in the ID array is divided into two arrays, where B in one array is 1, and B in the other queue is 0.
  4. (Note: In each array, except for the number that appears only once, the other number appears twice. In this case, you can use an exclusive or operation in the array)
  5. (4) then, perform an exclusive or operation on the two arrays respectively. Then, two numbers not 0 are obtained. That is, the two IDs are lost.

If the missing two numbers are the same

(In this case, all IDs in the array are displayed in pairs, with an exclusive or 0 value. You cannot use an exclusive or implementation)

Method: can be implemented using the invariant. Two equations are lost, and two equations are generated for simultaneous evaluation.

  1. 1) Calculate the sum of all IDs before the initial loss.
  2. (2) Calculate the sum of IDS after loss, and perform the subtraction operation on the Result (1) (2) to obtain the equation x + y =.
  3. (3) The difference between the sum of squares before and after the loss is used in conjunction with (2) to obtain the equation x * x + y * Y = B.
  4. (4) The two equations can be combined to obtain the final result.

~~~~~~~~~~~~~~~~~Split line~~~~~~~~~~~~~~~~~~~~~

Problem 3: An array is known and three data items are lost in the array. We need to find the three data items.

Then, it can be expanded to n

Method 1: we need to establish three/n equations to find the numbers

At this time, when the equation is N, it is difficult to obtain n equations.

Method 2: map is not stored when count sorting + Count value reaches

In this case, we can finally obtain the numbers

~~~~~~~~~~~~~~~~~Split line~~~~~~~~~~~~~~~~~~~~~

Related Questions

Give you a messy playing card (not including the king of the size), and draw a card from it. How can we use the simplest method to know if the card is drawn from 1 ~ Which one of the 13 s? (Do not know the color)

Method: Use the invariant

Calculate the sum of all cards (1 +... + 13) x 4 = 364

Then subtract the number of cards left, and the final result is the extracted card.






1.5 quickly identify Machine Faults

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.