楼主: xzcode
跳转到指定楼层
上一主题 下一主题
收起左侧

Google 实习面经

🔗
Gianluigi 2016-1-3 14:41:34 | 只看该作者
全局:
隐隐野烟 发表于 2016-1-3 13:35
满满的眼泪,刷了一学期的题,终还是没拿下G,本来还打算若成了去湾区暑期看你的。
来年再战Google,下 ...

lz 加油。能去哪儿都是缘分。
回复

使用道具 举报

🔗
 楼主| xzcode 2016-1-3 14:44:06 | 只看该作者
全局:
Gianluigi 发表于 2016-1-3 14:41
lz 加油。能去哪儿都是缘分。

谢过,祝你好运。我还是道行不够,再继续努力~~
回复

使用道具 举报

🔗
bbsbbstry 2016-1-4 05:23:47 | 只看该作者
全局:
楼主收到通知了么?
回复

使用道具 举报

🔗
wtcupup 2016-1-4 09:19:20 | 只看该作者
全局:
请问这道题用trie怎么做呢?比如说有 101 001 100 011 000 010, 建立一个trie 然后怎么做?
回复

使用道具 举报

🔗
Gianluigi 2016-1-4 12:46:19 | 只看该作者
全局:
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <queue>
  5. #include <unordered_map>
  6. #include <unordered_set>
  7. #include <sstream>

  8. using namespace std;

  9. // Trie node
  10. class Node{
  11. public:
  12.         int label; // 0 or 1
  13.         int level; // depth of the node in this trie tree
  14.         Node *p0,*p1; // child 0 or child 1
  15.         Node(int l, int i):label(l),level(i),p0(NULL),p1(NULL){};
  16. };

  17. class Solution{
  18. private:
  19.         // add number into the Trie tree
  20.         void addNumberUtil(Node* root, vector<int>& vec, int pos){
  21.                 if(pos==-1){
  22.                         return;
  23.                 }

  24.                 if(vec[pos]==0){
  25.                         if(root->p0==NULL){
  26.                                 root->p0 = new Node(0,pos-1);
  27.                         }
  28.                         addNumberUtil(root->p0,vec,pos-1);
  29.                 } else if (vec[pos]==1){
  30.                         if(root->p1==NULL){
  31.                                 root->p1 = new Node(1,pos-1);
  32.                         }
  33.                         addNumberUtil(root->p1,vec,pos-1);
  34.                 }
  35.                 return;
  36.         }

  37.         // find the max Xor in the Trie tree starting with root
  38.         int findMax(Node* root){
  39.                 if(root->p0==NULL && root->p1==NULL){
  40.                         return 0;
  41.                 } else if (root->p0==NULL){
  42.                         return findMaxUtil(root->p1,root->p1);
  43.                 } else if (root->p1==NULL){
  44.                         return findMaxUtil(root->p0,root->p0);
  45.                 }
  46.                
  47.                 return pow(2,root->level) + findMaxUtil(root->p0,root->p1);
  48.                
  49.         }

  50.         // recursively find the max Xor
  51.         int findMaxUtil(Node* root1, Node* root2){
  52.                 if(root1->p0==NULL && root1->p1==NULL && root2->p0==NULL && root2->p1==NULL){//leaf node
  53.                         return 0;
  54.                 } else if(root1->p0==NULL && root2->p0==NULL){
  55.                         return findMaxUtil(root1->p1,root2->p1);
  56.                 } else if(root1->p1==NULL && root2->p1==NULL){
  57.                         return findMaxUtil(root1->p0,root2->p0);
  58.                 } else if(root1->p0==NULL && root2->p1==NULL){
  59.                         return pow(2,root1->level) + findMaxUtil(root1->p1,root2->p0);
  60.                 } else if(root1->p1==NULL && root2->p0==NULL){
  61.                         return pow(2,root1->level) + findMaxUtil(root1->p0,root2->p1);
  62.                 } else if (root1->p0==NULL){
  63.                         return pow(2,root1->level) + findMaxUtil(root1->p1,root2->p0);
  64.                 } else if (root1->p1==NULL){
  65.                         return pow(2,root1->level) + findMaxUtil(root1->p0,root2->p1);
  66.                 } else if (root2->p0==NULL){
  67.                         return pow(2,root1->level) + findMaxUtil(root1->p0,root2->p1);
  68.                 } else if (root2->p1==NULL){
  69.                         return pow(2,root1->level) + findMaxUtil(root1->p1,root2->p0);
  70.                 }
  71.                
  72.                 // each node has both 0 and 1 child
  73.                 int t1 = findMaxUtil(root1->p0, root2->p1);
  74.                 int t2 = findMaxUtil(root1->p1, root2->p0);
  75.                 return pow(2,root1->level) + max(t1,t2);
  76.         }

  77. public:
  78.         int maxXor(vector<int>& vec){

  79.                 // vector to store the binary 0/1 of a number
  80.                 vector<vector<int>> binaryI(vec.size(),vector<int>{});
  81.                 int maxSize = 0;
  82.                 for(int i = 0; i < vec.size(); ++i){
  83.                         int temp = vec[i];
  84.                         while(temp>0){
  85.                                 binaryI[i].push_back(temp%2);
  86.                                 temp /= 2;
  87.                         }
  88.                         int size = binaryI[i].size();
  89.                         maxSize = max(maxSize,size);
  90.                 }

  91.                 // so that each binary representation has the same length
  92.                 for(auto& vec : binaryI){
  93.                         while(vec.size()<maxSize){
  94.                                 vec.push_back(0);
  95.                         }
  96.                 }

  97.                 // build tree
  98.                 Node *root = new Node(-1,maxSize-1);
  99.                 for(auto vec : binaryI){
  100.                         addNumberUtil(root,vec,maxSize-1);
  101.                 }

  102.                 // find max Xor
  103.                 return findMax(root);

  104.         }
  105. };


  106. int main(int argc, char** argv) {
  107.     vector<int> a{1,2,3,5,7,8,10,14,9,12,6,20};

  108.     // find answer using trie
  109.     Solution su;
  110.     auto p = su.maxXor(a);
  111.     cout<<"max = "<<p<<" using 0-1 trie."<<endl;

  112.     // find answer using brut-force method
  113.     int maxValue = numeric_limits<int>::min();
  114.     int ans1, ans2;
  115.     for(int i = 0; i < a.size()-1; ++i){
  116.             for(int j = i+1; j < a.size(); ++j){
  117.                     if(maxValue < (a[i] ^ a[j])){
  118.                             maxValue = a[i] ^ a[j];
  119.                             ans1 = a[i]; ans2 = a[j];
  120.                     }
  121.             }
  122.     }

  123.     cout<<"max = "<<(ans1^ans2)<<" using brut-force."<<endl;
  124. }
复制代码
回复

使用道具 举报

🔗
Gianluigi 2016-1-4 12:47:21 | 只看该作者
全局:

写了一个代码,但偷懒没有记录对应的两个数,只记录了最大值。

用 brut-force 的方法 cross check 了一下,似乎是对的。

回复

使用道具 举报

🔗
Gianluigi 2016-1-4 12:49:33 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
wtcupup 2016-1-4 15:56:49 | 只看该作者
全局:
more concise solution: http://analgorithmist.blogspot.com/2012/10/finding-pair-of-elements-with-maximum.html
回复

使用道具 举报

🔗
jmnjmnjmn 2016-1-5 15:50:34 | 只看该作者
全局:
这题有够难的
回复

使用道具 举报

🔗
asura23 2016-1-6 03:47:14 | 只看该作者
全局:
这题难得一笔啊,我感觉我最多做到建个树,用两个指针递归那里肯定要出错
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表