大数据面试题解决方案

1)给一个超过100G大小的log file, log中存着IP地址, 设计算法找到出现次数最多的IP地址?!

解决方法

将100G分成100份,将每个ip映射到相应文件中 ip_if=ip%100

找出每个文件中的出现次数最多的一个ip

再将100份里找出来的最多的一个放入一个哈希表中进行比较找出最大值

2)与上题条件相同,如何找到top K的IP?如何直接⽤用Linux系统命令实现?

解决方法

将100G分成100份,将每个ip映射到相应文件中 ip_if=ip%100

找出每个文件中的出现次数最多的k个ip

再将100份里找出来的最多的k个放入一个哈希表中进行比较找出最大的k 个

3)给定100亿个整数,设计算法找到只出现一次的整数!

将100亿个数分拆成1000份文件,再将每份文件里使用位图,并用两位bit表示数字出现的次数,00存出现0次的数,01存放出现1次的数,10存放出现多次的数,11舍弃,再将1000份中出现一次的数全部合并到一个文件里存放即可

4)给两个文件,分别有100亿个整数,我们只有1G内存,如何找到两个文件交集!

将第一个文件里的数用哈希映射到1000个文件中,将第二个文件用同样的哈希映射到另1000个文件中,然后比较每个哈希映射相同的文件即可
5)1个文件有100亿个int,1G内存,设计算法找到出现次数不超过2次的所有整数!

将100亿个数分拆成1000份文件,再将每份文件里使用位图,并用两位bit表示数字出现的次数,00存出现0次的数,01存放出现1次的数,10存放出现2次的数,11舍弃,再将1000份中出现次数不超过二的数全部合并到一个文件里存放即可
6)给两个文件,分别有100亿个query,我们只有1G内存,如何找到两个文件交集?
算法和近似算法!

精确算法:

将两个文件分别存入相同哈希算法的1000个哈希桶(共两千个)文件,再在每个文件找出相同的query

近似算法:

利用布隆过滤器进行比较

7)如何扩展BloomFilter使得它支持删除元素的操作?!

将bloomfilter中每一位扩展成一个技术为,例如00表示无,01表示1,当没删除一个,计数器减1,直到减为0为止
8)如何扩展BloomFilter使得它支持计数操作?!

同上题思路
9)给上千个文件,每个文件大小为1K—100M。给n个词,设计算法对每个词找到所有包含它的文件,你只有100K内存!

对上千个文件生成1000个布隆过滤器,并将1000个布隆过滤器存入一个文件中,将内存分为两份,一分用来读取布隆过滤器中的词,一块用来读取文件,知道将每个布隆过滤器读完为止
10)有一个词典,包含N个英文单词,现在任意给一个字符串,设计算法找出包含这个字符串的所有英文单词!

?暂时不会

位图

[cpp]  view plain  copy
  1. <span style="font-size:18px;">#pragma  once  
  2. #include<vector>  
  3. #include<iostream>  
  4. using namespace std;  
  5. class BitMap  
  6. {  
  7. public:  
  8.     BitMap(size_t range)  
  9.     {  
  10.         _bitMap.resize((range>>5) +1);  
  11.     }  
  12.     void Set(size_t x)  
  13.     {  
  14.         size_t index=x>>5;  
  15.         size_t num =x%32;  
  16.         /*size_t tmp=1; 
  17.         tmp<<=num;*/  
  18.         //_bitMap[index]|=tmp;  
  19.         _bitMap[index]=(1<<num);  
  20.     }  
  21.     void Reset(size_t x)  
  22.     {  
  23.         size_t index=x>>5;  
  24.         size_t num =x%32;  
  25.         size_t tmp=0;  
  26.         tmp<<=num;  
  27.         _bitMap[index] &=(~(tmp));  
  28.     }  
  29.     bool Test(size_t x)  
  30.     {  
  31.         size_t index=x>>5;  
  32.         size_t num =x%32;  
  33.         size_t tmp=1;  
  34.         tmp<<=num;  
  35.         _bitMap[index] &=tmp;  
  36.         return _bitMap[index];  
  37.     }  
  38.     ~BitMap()  
  39.     {}  
  40. protected:  
  41.     vector<size_t> _bitMap;  
  42. };</span>  
布隆过滤器

[cpp]  view plain  copy
  1. <span style="font-size:18px;">#pragma  once  
  2. #include <iostream>  
  3. #include <string>  
  4. #include <vector>  
  5. #include "BitMap.h"  
  6. using namespace std;  
  7. struct  __HashFunc1  
  8. {  
  9.     size_t BKDRHash(const char *str)    
  10.     {    
  11.         register size_t hash = 0;    
  12.         while (size_t ch = (size_t)*str++)    
  13.         {           
  14.             hash = hash * 131 + ch;   // 也可以乘以31、131、1313、13131、131313..             
  15.         }    
  16.         return hash;    
  17.     }    
  18.     size_t operator()(const string& str)  
  19.     {  
  20.         return BKDRHash(str.c_str());  
  21.     }  
  22. };  
  23. struct  __HashFunc2  
  24. {  
  25.     size_t SDBMHash(const char *str)    
  26.     {    
  27.         register size_t hash = 0;    
  28.         while (size_t ch = (size_t)*str++)    
  29.         {    
  30.             hash = 65599 * hash + ch;           
  31.             //hash = (size_t)ch + (hash << 6) + (hash << 16) - hash;    
  32.         }    
  33.         return hash;    
  34.     }    
  35.     size_t operator()(const string& str)  
  36.     {  
  37.         return SDBMHash(str.c_str());  
  38.     }  
  39. };  
  40. struct  __HashFunc3  
  41. {  
  42.     size_t RSHash(const char *str)    
  43.     {    
  44.         register size_t hash = 0;    
  45.         size_t magic = 63689;       
  46.         while (size_t ch = (size_t)*str++)    
  47.         {    
  48.             hash = hash * magic + ch;    
  49.             magic *= 378551;    
  50.         }    
  51.         return hash;    
  52.     }    
  53.     size_t operator()(const string& str)  
  54.     {  
  55.         return RSHash(str.c_str());  
  56.     }  
  57. };  
  58. struct  __HashFunc4  
  59. {  
  60.     size_t APHash(const char *str)    
  61.     {    
  62.         register size_t hash = 0;    
  63.         size_t ch;    
  64.         for (long i = 0; ch = (size_t)*str++; i++)    
  65.         {    
  66.             if ((i & 1) == 0)    
  67.             {    
  68.                 hash ^= ((hash << 7) ^ ch ^ (hash >> 3));    
  69.             }    
  70.             else    
  71.             {    
  72.                 hash ^= (~((hash << 11) ^ ch ^ (hash >> 5)));    
  73.             }    
  74.         }    
  75.         return hash;    
  76.     }    
  77.     size_t operator()(const string& str)  
  78.     {  
  79.         return APHash(str.c_str());  
  80.     }  
  81. };  
  82. struct  __HashFunc5  
  83. {  
  84.     size_t JSHash(const char *str)    
  85.     {    
  86.         if(!*str)          
  87.             return 0;    
  88.         register size_t hash = 1315423911;    
  89.         while (size_t ch = (size_t)*str++)    
  90.         {    
  91.             hash ^= ((hash << 5) + ch + (hash >> 2));    
  92.         }    
  93.         return hash;    
  94.     }    
  95.     size_t operator()(const string& str)  
  96.     {  
  97.         return JSHash(str.c_str());  
  98.     }  
  99. };  
  100. template<class K=string  
  101. ,class HashFunc1=__HashFunc1  
  102. ,class HashFunc2=__HashFunc2  
  103. ,class HashFunc3=__HashFunc3  
  104. ,class HashFunc4=__HashFunc4  
  105. ,class HashFunc5=__HashFunc5>  
  106. class RefBloomFilter  
  107. {  
  108. public:  
  109.     RefBloomFilter(size_t num)  
  110.         :_range(num*5)  
  111.         ,_bitMap(num*5)  
  112.     {}  
  113.     void Set(const K& key)  
  114.     {  
  115.         size_t index1=HashFunc1()(key)%_range;  
  116.         size_t index2=HashFunc2()(key)%_range;  
  117.         size_t index3=HashFunc3()(key)%_range;  
  118.         size_t index4=HashFunc4()(key)%_range;  
  119.         size_t index5=HashFunc5()(key)%_range;  
  120.         _bitMap.Set(index1);  
  121.         _bitMap.Set(index2);  
  122.         _bitMap.Set(index3);   
  123.         _bitMap.Set(index4);  
  124.         _bitMap.Set(index5);    
  125.         cout<<index1<<endl;  
  126.         cout<<index2<<endl;  
  127.         cout<<index3<<endl;  
  128.         cout<<index4<<endl;  
  129.         cout<<index5<<endl<<endl;  
  130.   
  131.           
  132.     }  
  133.     bool Reset(const K& key)  
  134.     {  
  135.         size_t index1=HashFunc1()(key)%_range;  
  136.         size_t index2=HashFunc2()(key)%_range;  
  137.         size_t index3=HashFunc3()(key)%_range;  
  138.         size_t index4=HashFunc4()(key)%_range;  
  139.         size_t index5=HashFunc5()(key)%_range;  
  140.         if(_bitMap[index1]==0  
  141.             ||_bitMap[index2]==0  
  142.             ||_bitMap[index3]==0  
  143.             ||_bitMap[index4]==0  
  144.             ||_bitMap[index5]==0)  
  145.             return false;  
  146.         _bitMap.Reset(index1);  
  147.         _bitMap.Reset(index2);  
  148.         _bitMap.Reset(index3);  
  149.         _bitMap.Reset(index4);  
  150.         _bitMap.Reset(index5);  
  151.         cout<<index1<<endl;  
  152.         cout<<index2<<endl;  
  153.         cout<<index3<<endl;  
  154.         cout<<index4<<endl;  
  155.         cout<<index5<<endl<<endl;  
  156.         return true;  
  157.     }  
  158.     bool Test(const K& key)  
  159.     {  
  160.         size_t index1=HashFunc1()(key)%_range;  
  161.         size_t index2=HashFunc2()(key)%_range;  
  162.         size_t index3=HashFunc3()(key)%_range;  
  163.         size_t index4=HashFunc4()(key)%_range;  
  164.         size_t index5=HashFunc5()(key)%_range;  
  165.         if(_bitMap.Test(index1)!=0  
  166.             &&_bitMap.Test(index2)!=0  
  167.             &&_bitMap.Test(index3)!=0  
  168.             &&_bitMap.Test(index4)!=0  
  169.             &&_bitMap.Test(index5)!=0)  
  170.         {  
  171.             cout<<index1<<endl;  
  172.         cout<<index2<<endl;  
  173.         cout<<index3<<endl;  
  174.         cout<<index4<<endl;  
  175.         cout<<index5<<endl<<endl;  
  176.             return true;  
  177.           
  178.         }  
  179.         cout<<index1<<endl;  
  180.         cout<<index2<<endl;  
  181.         cout<<index3<<endl;  
  182.         cout<<index4<<endl;  
  183.         cout<<index5<<endl<<endl;  
  184.         return false;  
  185.     }  
  186. protected:  
  187.     BitMap _bitMap;  
  188.     size_t _range;  
  189. };  
  190. void test()  
  191. {  
  192.     RefBloomFilter<> bf(1);  
  193.     char* str1="http://blog.csdn.net/shangguan_1234";  
  194.     char* str2="http://blog.csdn.net/shangguan_1235";  
  195.     char* str3="http://blog.csdn.net/shangguan_1233";  
  196.     char* str4="http://blog.csdn.net/shangguan_1232";  
  197.     char* str5="http://blog.csdn.net/shangguan_1231";  
  198.     bf.Set(str1);  
  199.     bf.Set(str2);  
  200.     bf.Set(str3);  
  201.     bf.Set(str4);  
  202.     bf.Set(str5);  
  203.     cout<<bf.Test(str1)<<endl;  
  204.     cout<<bf.Test(str2)<<endl;  
  205.     cout<<bf.Test(str3)<<endl;  
  206.     cout<<bf.Test(str4)<<endl;  
  207.     cout<<bf.Test(str5)<<endl;  
  208. }</span>  


转载地址:https://blog.csdn.net/shangguan_1234/article/details/53088988

猜你喜欢

转载自blog.csdn.net/with__sunshine/article/details/80306567