Massive Data problems
1.GivenA,BTwo files are stored separately.50Hundreds of millionsUrl, EachUrlAccounts64Bytes. The memory limit is4GFor you to find outA,BFile commonUrl?
Solution 1: it can be estimated that the size of each file is 50 GB × 64 = 320 GB, far greater than the memory limit of 4 GB. Therefore, it is impossible to fully load it into the memory for processing. Consider a divide-and-conquer approach.
- Traverse file a, obtain each url, and store the url to the 1000 small files (marked as) based on the obtained values. In this way, the size of each small file is about 300 MB.
- Traverse file B and store the url in the same way as ATO 1000 small files (recorded ). After such processing, all the URLs that may be the same are in the corresponding small file (), and non-corresponding small files cannot have the same url. Then we only need to find the same url in the 1000 pairs of small files.
- You can store the url of a small file in hash_set for the same url in each pair of small files. Then traverse each url of another small file to see if it is in the hash_set just constructed. If it is, it is a common url and saved in the file.
Solution 2: if a certain error rate is allowed, you can use the Bloom filter. The 4G memory can be approximately 34 billion bits. Map the URLs in one file to the 34 billion bits using the Bloom filter, read the URLs of another file one by one, and check whether the URLs are consistent with the Bloom filter. If yes, the url should be a common url (note that there will be a certain error rate ).
2. There are 10 files, each of which is 1 GB. each row of each file stores the user's query, and the query of each file may be repeated. Sort the query frequency.
Solution 1:
- Read 10 files in sequence and write the query to the other 10 files according to the hash (query) % 10 results. In this way, the size of each newly generated file is about 1 GB (assuming that the hash function is random ).
- Find a machine with around 2 GB of memory and use hash_map (query, query_count) to calculate the number of times each query appears. Sort by the number of occurrences by means of fast/Heap/Merge Sorting. Output the sorted query and the corresponding query_cout to the file. In this way, 10 sorted files (marked as) are obtained ).
- Merge and sort the 10 files (combining inner and outer sorting ).
Solution 2:
Generally, the total number of queries is limited, but the number of repetitions is large. For all queries, you can add them to the memory at one time. In this way, we can use the trie tree, hash_map, and so on to directly count the number of occurrences of each query, and then perform fast/Heap/Merge Sorting based on the number of occurrences.
Solution 3: similar to solution 1, but after hash is completed and divided into multiple files, it can be handed over to multiple files for processing, using a distributed architecture (such as MapReduce ), then merge.
3. There is a 1 GB file with each row containing a word. The word size cannot exceed 16 bytes, and the memory size is limited to 1 MB. Returns the top 100 words with the highest frequency.
Solution 1: read each word x in an ordered file and save it to 5000 small files (marked as) based on this value. In this way, each file is about KB. If the size of some files exceeds 1 MB, you can continue to split the files in a similar way, knowing that the size of the small files to be decomposed cannot exceed 1 MB. For each small file, count the words in each file and the corresponding frequency (trie tree/hash_map can be used ), and take out the 100 words with the maximum frequency (the minimum heap containing 100 nodes can be used), and save the 100 words and the corresponding frequency to the file, thus obtaining 5000 files. The next step is to merge the 5000 files (similar to the merge and sort files.
4.Massive log data, extracted the one with the most visits to Baidu on a dayIP.
Solution 1: the first is this day, and the IP addresses in the logs accessing Baidu are obtained and written to a large file one by one. Note that the IP address is a 32-bit IP address with a maximum of 2 ^ 32 IP addresses. You can also use the ing method, such as modulo 1000, to map the entire large file to 1000 small files, and then find the IP address with the highest frequency in each small text (hash_map can be used for frequency statistics, then find out the maximum frequency) and the corresponding frequency. Then, among the 1000 largest IP addresses, find the IP address with the highest frequency, that is, what you want.
5.In2.5The number of non-repeated integers is found in hundreds of millions of integers, and the memory is insufficient to accommodate this2.5Hundreds of millions of integers.
Solution 1: Use 2-Bitmap (2bit for each number, 00 indicates no, 01 indicates one occurrence, 10 indicates multiple times, and 11 indicates meaningless, the total memory size is 2 ^ 32*2 bit = 1 GB memory, which is acceptable. Then scan the 0.25 billion Integers to check the corresponding bits in Bitmap. If 00 is changed to, 10 remains unchanged. After the descriptions are completed, view the bitmap and output the corresponding digit as an integer of 01.
Solution 2: You can use similar methods to divide small files. Then, find the non-repeated integers in the small file and sort them. Then merge the elements to remove them.
6. massive amounts of data are distributed in 100 minds, and we can find a way to efficiently count the top 10 of this batch of data.
Solution 1:
- Find the top 10 items on each computer and use a heap containing 10 elements (top 10 items are small, with the maximum heap, top 10 items are large, and the minimum heap is used ). For example, if we want to increase the top 10, we should first adjust the first 10 elements to the smallest heap. If we find the top 10 elements, we will then scan the subsequent data and compare it with the heap top elements. If it is larger than the top 10 elements, use this element to replace the heap top and then adjust it to the minimum heap. The final heap element is the top 10.
- After finding the top 10 on each computer, we can combine the top 10 on the 100 computers with a total of 1000 pieces of data. Then we can use the above similar method to find the top 10.
7.Tens of millions or hundreds of millions of data records (with repeated data records) are used to calculate the most frequently used data.NData.
Solution 1: Data of tens of millions or hundreds of millions can be stored in the memory of the current machine. Therefore, we recommend that you use hash_map/binary tree search/red/black tree to calculate the number of times. Then we can retrieve the first N data records that appear most frequently. We can use the heap mechanism mentioned in question 6th.
8.A text file contains about 10 thousand lines and each line contains one word, which requires statistics on the top of the most frequent appearance.10For more information about time complexity.
Solution 1: consider time efficiency. Use the trie tree to count the number of times each word appears. The time complexity is O (n * le) (le indicates the word's level length ). Then we can find out the first 10 words that appear most frequently. We can use the heap method. As mentioned in the previous question, the time complexity is O (n * lg10 ). Therefore, the total time complexity is the greater of O (n * le) and O (n * lg10.
9.A text file, locate the previous10A frequently-seen word, but this time the file is relatively long, said to be hundreds of millions of lines or billions of lines, in a word can not read the memory at a time, ask the optimal solution.
Solution 1: first, based on hash and modulo, the file is divided into multiple small files. For a single file, use the above method to find the 10 most frequently-seen words in each file. Then merge to find the 10 most frequently-seen words.
100 millionFind the largest100Number.
Solution 1: we have mentioned in the previous question that a minimum heap containing 100 elements is used. The complexity is O (100 w * lg100 ).
- Solution 2: the idea of fast sorting is adopted. After each split, only the portion larger than the axis is considered. When the portion larger than the axis is more than 100, the traditional sorting algorithm is used for sorting, the first 100. The complexity is O (100 w * 100 ).
- Solution 3: Adopt the local elimination method. Select the first 100 elements and sort them as sequence L. Then, the remaining element x is scanned at a time to compare with the smallest element in the 100 elements in the sorted order. If it is larger than the smallest element, delete the smallest element, and insert x into the sequence L using the insert sorting idea. It cyclically scans all elements. The complexity is O (100 w * 100 ).
11.CommonNMachines. Each machine hasNNumber. Max storage per machineO (N)Number and operate on them. How to findN ^ 2Number in the number?
Solution 1: First estimate the range of these numbers. For example, assume that these numbers are all 32-bit unsigned integers (2 ^ 32 in total ). We divide the integers 0 to 2 ^ 32-1 into N range segments. Each segment contains (2 ^ 32)/N integers. For example, the first field 0 to 2 ^ 32/N-1, the second segment is (2 ^ 32)/N to (2 ^ 32)/N-1 ,..., The Nth segment is (2 ^ 32) (N-1)/N to 2 ^ 32-1. Then, scan the number of N on each machine and place the number in the first segment on the first machine. Put the number in the second segment on the second machine ,..., Place the number of the nth segment on the nth machine. Note that the number stored on each machine in this process should be O (N. Next, we will calculate the number of each machine in sequence and accumulate the number at a time until the k machine is found. The accumulated number on this machine is greater than or equal to (N ^ 2)/2, the sum on the second K-1 machine is less than (N ^ 2)/2, and this number is counted as x. Then, the median we are looking for is in the (N ^ 2)/2-x position of the k machine. Then we sort the number of k machines and find the number (N ^ 2)/2-x, that is, the complexity of the median is O (N ^ 2.
Solution 2: sort the numbers on each machine first. After sorting, we use the thought of merging and sorting to merge the numbers on the N machines to get the final sorting. Find the second (N ^ 2)/second to ask for it. The complexity is O (N ^ 2 * lgN ^ 2.