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

脸书家onsite

全局:

2016(4-6月) 码农类General 硕士 全职@meta - 内推 - Onsite  | | Pass | 在职跳槽

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

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

x

脸家效率很高, 两三天后就发了offer, 然后级别数字居然和目前的一模一样, 还不给加面。。。这和拒信有什么区别呢。。。只能锯掉了

1. behavior question。 最后问了道dot product的老题
2. given a list of words, find whether a given target word exists. Should support “.” which matches any character. Follow up: support “*” which matchs 0 or more char
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
coding是给一个数组和一个数N, 随机返回该数组中任意一个不大于N的数。


评分

参与人数 4大米 +54 收起 理由
D-Bazinga + 3 感谢分享!
vancexu + 10 感谢分享!
jxf2898 + 1 感谢分享!
夏虫不知雪花 + 40

查看全部评分


上一篇:Yahoo电面
下一篇:Cisco 两轮面经 (全印阵容)
推荐
zzgzzm 2016-11-6 12:56:14 | 只看该作者
全局:
forestwn 发表于 2016-10-9 02:41
211 Add and Search Word - Data structure design, Follow up: support “*” which matchs 0 or more cha ...

这个应该就是LC211的扩展了,加上了support for '*'. 关键是当search word遇到'*',要跳过所有可能连续的'*'继续寻找下一个非'*'的char。在字典数据结构的Trie中就对于找当前node的所有对应的子孙nodes(若是'.'就找所有子孙)。然后再循环每个子孙node递归。一个edge case就是若word以连续的'*'结尾的话就直接返回true,因为'*'之前的prefix在字典中已经找到了。
大家看看code有没有问题。
  1. struct Nd { // Trie node for dictionary
  2.     bool flag; // true if representing a word
  3.     vector<Nd*> next;
  4.     Nd():flag(false),next(vector<Nd*>(26)) {}
  5. };
  6.    
  7. class WordDictionary {
  8. private:   
  9.     Nd* r; // dummy root for Trie
  10.    
  11.     // get all nodes from descendants of cur representing as char c;
  12.     // if c is not alphabet, get all descendants
  13.     void getNodes(Nd* cur, char c, vector<Nd*>& nodes) {
  14.         if (!cur) return;
  15.         int ic = c - 'a';
  16.         for (int i = 0; i < 26; ++i) {
  17.           if (!cur->next[i]) continue;
  18.           if (ic == i || ic < 0 || ic >= 26)
  19.              nodes.push_back(cur->next[i]);
  20.           getNodes(cur->next[i], c, nodes);
  21.         }
  22.     }
  23.    
  24.     // search word w from index start
  25.     bool mySearch(string w, int start, Nd* cur) {
  26.         if (!cur) return false;
  27.         int n = w.length();
  28.         if (start == n) return cur->flag;
  29.         else if (start > n) return false;
  30.         
  31.         char c = w[start];
  32.         if (c >= 'a' && c <= 'z') { // c is alphabet
  33.             return mySearch(w, start+1, cur->next[c-'a']);
  34.         }else if (c == '.') { // c is '.'
  35.             for (int i = 0; i < 26; i++) {
  36.                if (mySearch(w, start+1, cur->next[i])) return true;
  37.             }
  38.             return false;
  39.         }else { // c is '*'
  40.            // find next non '*' char and store it in c
  41.            while (++start < n && (c=w[start]) == '*') continue;
  42.            if (start == n) return true; // all trailing '*''s
  43.            // get all nodes from descendants of cur representing as char c;
  44.            // otherwise c is '.', and so get all descendants
  45.            vector<Nd*> nodes; getNodes(cur, c, nodes);
  46.            for (auto node : nodes) {
  47.                if (mySearch(w, start, node)) return true;
  48.            }
  49.            return false;
  50.         }
  51.     }
  52.    
  53. public:
  54.     // constructor
  55.     WordDictionary() { r = new Nd(); }

  56.     // Adds a word into the data structure.
  57.     void addWord(string word) {
  58.         Nd* cur = r;
  59.         for (char c:word) {
  60.             int i = c-'a';
  61.             if (!cur->next[i]) cur->next[i] = new Nd();
  62.             cur = cur->next[i];
  63.         }
  64.         cur->flag = true;
  65.     }

  66.     // Returns if the word is in the data structure. Support '.' and '*'
  67.     bool search(string word) { return mySearch(word, 0, r); }
  68. };
复制代码
回复

使用道具 举报

推荐
fubu 2016-11-6 13:36:44 | 只看该作者
全局:
贴我的代码,我这儿'*'是match 0个或多个前缀字符的 '*' Matches zero or more of the preceding element

  1. public class WordDictionary {
  2.     TrieNode root;
  3.     WordDictionary() {
  4.         root = new TrieNode();
  5.     }
  6.     // Adds a word into the data structure.
  7.     public void addWord(String word) {
  8.         TrieNode cur = root;
  9.         for(int i=0; i<word.length(); i++) {
  10.             char c = word.charAt(i);
  11.             int index = c - 'a';
  12.             if(cur.children[index] == null) {
  13.                 cur.children[index] = new TrieNode();
  14.             }
  15.             cur = cur.children[index];
  16.             cur.letter = c;
  17.         }
  18.         cur.hasWord = true;
  19.     }

  20.     public boolean search(String word) {
  21.         TrieNode cur = root;
  22.         return search(root, word, 0);
  23.     }
  24.    
  25.     private boolean search(TrieNode cur, String word, int index) {
  26.         if(cur == null) {
  27.             return false;
  28.         }
  29.         if(index == word.length()) {
  30.             return cur.hasWord;
  31.         }
  32.         char c = word.charAt(index);
  33.         if(index == word.length() - 1 || word.charAt(index+1) != '*') {
  34.             if(c == '.') {
  35.                 for(TrieNode node : cur.children) {
  36.                     if(search(node, word, index+1)) {
  37.                         return true;
  38.                     }
  39.                 }
  40.                 return false;
  41.             } else if(c == '*') {
  42.                 return false;
  43.             } else {
  44.                 return search(cur.children[c - 'a'], word, index+1);
  45.             }
  46.         } else {
  47.             if(search(cur, word, index+2)) {
  48.                 return true;
  49.             }
  50.             if(c == '.') {
  51.                 for(TrieNode node : cur.children) {
  52.                     if(search(node, word, index)) {
  53.                         return true;
  54.                     }
  55.                 }
  56.                 return false;
  57.             } else {
  58.                 return search(cur.children[c - 'a'], word, index);
  59.             }
  60.         }

  61.     }
  62.    
  63.     class TrieNode {
  64.         TrieNode[] children;
  65.         char letter;
  66.         boolean hasWord;
  67.         TrieNode() {
  68.             children = new TrieNode[26];
  69.             hasWord = false;
  70.         }
  71.     }
  72. }
复制代码













回复

使用道具 举报

推荐
zzgzzm 2016-11-6 10:02:00 | 只看该作者
全局:
ccrjohn8787 发表于 2016-7-2 20:55
多谢楼主!请问最后一题有什么要求吗?constant space?不然是不是扫一遍记一下不大于N的数,然后用个rand?

只需要O(1),不要存<=max的值,对<=max的直接做reservoir sampling即可。O(N) one pass 时间复杂度。
  1. int getRand(const vector<int>& a, int high) {
  2.   int res = INT_MIN, count = 0;
  3.   for (int x:a) if (x <= high && rand()%(++count) == 0) res = x;
  4.   return res;
  5. }
复制代码
回复

使用道具 举报

🔗
pustar 2016-7-2 16:45:09 | 只看该作者
全局:
Minimum Size Subarray Sum题目要求是什么啊?能细说下吗?
回复

使用道具 举报

🔗
ccrjohn8787 2016-7-2 20:55:30 | 只看该作者
全局:
多谢楼主!请问最后一题有什么要求吗?constant space?不然是不是扫一遍记一下不大于N的数,然后用个rand?
回复

使用道具 举报

🔗
Fustang 2016-7-2 22:04:37 | 只看该作者
全局:
ccrjohn8787 发表于 2016-7-2 20:55
多谢楼主!请问最后一题有什么要求吗?constant space?不然是不是扫一遍记一下不大于N的数,然后用个rand?

那题应该是FB常考题random max index的变种 可以用Reservoir Sampling 不需要额外space
回复

使用道具 举报

🔗
 楼主| kunzi 2016-7-2 22:52:53 | 只看该作者
全局:
pustar 发表于 2016-7-2 16:45
Minimum Size Subarray Sum题目要求是什么啊?能细说下吗?

leetcode原题啊
回复

使用道具 举报

🔗
hello2pig 2016-7-7 11:48:31 | 只看该作者
全局:
dot product 是什么题? 直接球dot product?
回复

使用道具 举报

🔗
hyj143 2016-7-8 03:14:26 | 只看该作者
全局:
求问楼主 autocomplete 是怎么回答的
回复

使用道具 举报

🔗
jy_121 2016-7-9 13:48:34 | 只看该作者
全局:
同问autocomplete怎么答的?谢谢
回复

使用道具 举报

🔗
ChrisGates23 2016-7-10 05:59:53 | 只看该作者
全局:
请问题2需要写代码么
回复

使用道具 举报

🔗
ChrisGates23 2016-7-10 06:02:44 | 只看该作者
全局:
ChrisGates23 发表于 2016-7-10 05:59
请问题2需要写代码么

I mean followup
回复

使用道具 举报

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

本版积分规则

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