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

Google 实习面经

全局:

2016(7-9月) 码农类General 硕士 实习@google - 内推 - 技术电面  | | Other | 其他

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
给一个Array,retur
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
主仍阵亡。

评分

参与人数 4大米 +14 收起 理由
jmnjmnjmn + 1 感谢分享!
muybienw + 5 感谢分享!
Gianluigi + 3 感谢分享!
xiaozhuxiaozhu + 5 感谢分享!

查看全部评分


上一篇:求Google Engineering Practicum面经
下一篇:Amazon Intern OA
推荐
 楼主| xzcode 2016-1-3 12:46:44 | 只看该作者
全局:
trie + bit manipulation 可以解决,但面时候没有码完,当时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. }
复制代码
回复

使用道具 举报

推荐
singku 2016-1-10 02:50:37 | 只看该作者
全局:
可以按位分类,递归判断低位
复杂度位 nlog(32)
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;

  4. #define MAX_BIT (31)

  5. bool divide(std::vector<int> &a, std::vector<int> &ret0, std::vector<int> &ret1, int bit_idx)
  6. {
  7.     if (bit_idx > MAX_BIT || bit_idx < 0) {
  8.         return false;
  9.     }
  10.     ret0.clear();
  11.     ret1.clear();
  12.     int mask = 1 << bit_idx;
  13.     for (int i = 0; i < a.size(); i++) {
  14.         if ((a[i] & mask) == 0) {
  15.             ret0.push_back(a[i]);
  16.         } else {
  17.             ret1.push_back(a[i]);
  18.         }
  19.     }
  20.     return true;
  21. }

  22. int max_xor(std::vector<int> &a0, std::vector<int> &b1, int cur_bit, int cur_val)
  23. {
  24.     if (cur_bit == 0) {
  25.         return cur_val;
  26.     }

  27.     if (a0.size() == 0 || b1.size() == 0) {
  28.         std::vector<int> c = a0.size() ?a0 :b1;
  29.         std::vector<int> c0, c1;
  30.         cur_bit--;
  31.         divide(c, c0, c1, cur_bit);
  32.         if (c0.size() && c1.size()) {
  33.             cur_val = cur_val | (1 << cur_bit);
  34.         }
  35.         return max_xor(c0, c1, cur_bit, cur_val);
  36.     }

  37.     std::vector<int> a00, a01;
  38.     std::vector<int> b10, b11;

  39.     cur_bit--;
  40.     divide(a0, a00, a01, cur_bit);
  41.     divide(b1, b10, b11, cur_bit);

  42.     int a00b11 = 0;
  43.     if (a00.size() && b11.size()) {
  44.         cur_val = cur_val | (1 << cur_bit);
  45.         a00b11 = max_xor(a00, b11, cur_bit, cur_val);
  46.     }
  47.     int a01b10 = 0;
  48.     if (a01.size() && b10.size()) {
  49.         cur_val = cur_val | (1 << cur_bit);
  50.         a01b10 = max_xor(a01, b10, cur_bit, cur_val);
  51.     }
  52.     if (a00b11 || a01b10) {
  53.         return (a00b11 > a01b10 ?a00b11 :a01b10);
  54.     }

  55.     return max_xor(a0, b1, cur_bit, cur_val);
  56. }

  57. int main(void)
  58. {
  59.     int test_arr[] = {
  60.         0, 1000, 1001, 1002, 1003
  61.     };
  62.     //0001 0010 0011 0100 0101 0110 0111 1000 1001 1010

  63.     std::vector<int> a(test_arr, test_arr + sizeof(test_arr)/sizeof(int));
  64.     std::vector<int> b;
  65.     int max = max_xor(a, b, 32, 0);
  66.     cout << max << endl;
  67. }
复制代码
回复

使用道具 举报

🔗
xjbTalk 2016-1-3 12:10:30 | 只看该作者
全局:
考官对时间复杂度有要求嘛??
回复

使用道具 举报

全局:
要考虑负数吗?
回复

使用道具 举报

全局:
lz是先面完一个 再约第2个面试?
为什么我是连着2个呢。。back to back。。
回复

使用道具 举报

全局:
感觉,如果不知道optimal solution的话。
可不可以直接给暴力解,组成所有两个pair,然后直接看哪个pair的xor最大?
回复

使用道具 举报

全局:
暴力估计不是面试官想要的
回复

使用道具 举报

全局:
想到一个解法,暂时不考虑负数。搞一个map track 最高位到最低位那位为一的数的的index 然后从最高位开始greedy 目的是尽量保留住高位的 1。复杂度是线性。大家觉得如何?
回复

使用道具 举报

🔗
 楼主| xzcode 2016-1-3 12:44:03 | 只看该作者
全局:
我是加面,经过和面试官讨论,想到用trie做才能达到nlogn(思路:http://stackoverflow.com/questions/9320109/two-elements-in-array-whose-xor-is-maximum)题目不考虑负数。楼主完整刷过LeetCode + 所有地里intern面经,本来刷别人的面经刷的挺自信。但经过此面,证明自己还是实力还是不到家,继续努力。
回复

使用道具 举报

🔗
 楼主| xzcode 2016-1-3 12:50:16 | 只看该作者
全局:
kinggarden2001 发表于 2016-1-3 12:38
想到一个解法,暂时不考虑负数。搞一个map track 最高位到最低位那位为一的数的的index 然后从最高位开始gr ...

怎么都到不了线性的。
回复

使用道具 举报

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

本版积分规则

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