📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
回复: 29
跳转到指定楼层
上一主题 下一主题
收起左侧

Zenefits最近几道难题的解法

全局:

2015(7-9月) 码农类General 博士 全职@zenefits - 内推 - 技术电面 Onsite 在线笔试  | | Other | 在职跳槽

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

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

x
本人总结了一下最近zenefits面试中出现的难题
OA3

1. Longest Chain
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
color:rgb(255, 255, 255)">.鐣欏?璁哄潧-涓

评分

参与人数 7大米 +66 收起 理由
tonygxxx1212 + 10 感谢分享!
hercule24 + 5 感谢分享!
MCwong + 5 感谢分享!
Triston + 3 感谢分享!
lqzgz + 3 感谢分享!

查看全部评分


上一篇:新鲜的LiveRamp OA题目
下一篇:2015年10月22日 google MTV onsite

本帖被以下淘专辑推荐:

推荐
 楼主| kennethinsnow 2015-10-23 11:09:16 | 只看该作者
全局:
http://www.1point3acres.com/bbs/thread-131422-1-1.html
valid binary tree
  1. public class Solution1 {
  2.         class TreeNode{
  3.                 char value;
  4.                 TreeNode left;
  5.                 TreeNode right;
  6.                 TreeNode(char inp){
  7.                         this.value = inp;
  8.                 }
  9.         }
  10.         public String sExp(char[][] pair){
  11.                 // build an adj matrix row is the parent node, col is the child node
  12.                 int len = pair.length;
  13.                 if (len == 0) return "";
  14.                 int[][] adjMat = new int[26][26];
  15.                 int[] inNode = new int[26];        // count the incoming edges of each node
  16.                 int[] children = new int[26]; // count the number of children of each node
  17.                 Map<Character, TreeNode> nodes = new HashMap();
  18.                 TreeNode parent, child;
  19.                 UnionFind myUnion = new UnionFind(26);
  20.                 // build binary tree based on pair
  21.                 for (int i = 0; i < len; i++){
  22.                         int row = pair[i][0] - 'A';
  23.                         int col = pair[i][1] - 'A';
  24.                         if (children[row] == 2 && adjMat[row][col] != 1){
  25.                                 // more than 2 children but not the same edge
  26.                                 return "E1";
  27.                         }else if (adjMat[row][col] == 1){
  28.                                 // duplicated edges
  29.                                 return "E2";
  30.                         } else if (myUnion.connected(row, col)){
  31.                                 // if new link connect two that are already in same union there is loop
  32.                                 return "E3";
  33.                         }
  34.                         adjMat[row][col] = 1;
  35.                         children[row]++;
  36.                         inNode[col]++;
  37.                         myUnion.connect(row, col);
  38.                         connectNodes(pair, nodes, i);
  39.                 }
  40.                
  41.                 // check multiple roots
  42.                 int rNum = 0;
  43.                 TreeNode root = null;
  44.                 for (char x : nodes.keySet()){
  45.                         if (inNode[x - 'A'] == 0){
  46.                                 rNum++;
  47.                                 if (rNum > 1) return "E4";
  48.                                 root = nodes.get(x);
  49.                         }
  50.                 }
  51.                 if (root == null) return "E5";
  52.                
  53.                 // convert it to s-expression
  54.                 return toSExpression(root);
  55.         }
  56.        
  57.         // convert it to s-expression
  58.         String toSExpression(TreeNode root){
  59.                 if (root == null) return "";
  60.                 return "(" + root.value + toSExpression(root.left) + toSExpression(root.right) + ")";
  61.         }
  62.        
  63.         // connect parent and child node
  64.         private void connectNodes(char[][] pair, Map<Character, TreeNode> nodes,
  65.                         int i) {
  66.                 TreeNode parent;
  67.                 TreeNode child;
  68.                 if (nodes.containsKey(pair[i][0])) parent = nodes.get(pair[i][0]);
  69.                 else{
  70.                         parent = new TreeNode(pair[i][0]);
  71.                         nodes.put(pair[i][0], parent);
  72.                 }
  73.                 if (nodes.containsKey(pair[i][1])) child = nodes.get(pair[i][1]);
  74.                 else{
  75.                         child = new TreeNode(pair[i][1]);
  76.                         nodes.put(pair[i][1], child);
  77.                 }
  78.                 if (parent.left == null) parent.left = child;
  79.                 else {
  80.                         if (parent.left.value < pair[i][1]){
  81.                                 parent.right = child;
  82.                         } else {
  83.                                 parent.right = parent.left;
  84.                                 parent.left = child;
  85.                         }
  86.                 }
  87.         }
  88.        
  89.         public static void main(String[] args){
  90.                 Solution1 sln = new Solution1();
  91.                 // (A(B(D(E(G))))(C(F)))
  92.                 // char[][] pair = {{'B', 'D'}, {'D', 'E'}, {'A', 'B'}, {'C', 'F'}, {'E', 'G'}, {'A', 'C'}};
  93.                 // E3
  94.                 // char[][] pair = {{'A', 'B'}, {'A', 'C'}, {'B', 'D'}, {'D', 'C'}};
  95.                 // (A(B(D)(G))(C(E(F))(H)))
  96.                 // char[][] pair = {{'A', 'B'}, {'A', 'C'}, {'B', 'G'}, {'C', 'H'}, {'E', 'F'}, {'B', 'D'}, {'C', 'E'}};
  97.                 // E1
  98.                 // char[][] pair = {{'A', 'B'}, {'A', 'C'}, {'B', 'D'}, {'A', 'E'}};
  99.                 // E2
  100.                 // char[][] pair = {{'A', 'B'}, {'A', 'C'}, {'B', 'D'}, {'A', 'C'}};
  101.                 // E4
  102.                 char[][] pair = {{'A', 'B'}, {'A', 'C'}, {'B', 'G'}, {'C', 'H'}, {'E', 'F'}, {'B', 'D'}};
  103.                 System.out.println(sln.sExp(pair));
  104.         }
  105. }
复制代码
回复

使用道具 举报

推荐
 楼主| kennethinsnow 2015-10-23 10:45:06 | 只看该作者
全局:
机器人距离和最小问题
注意问题参见 http://www.1point3acres.com/bbs/thread-145166-1-1.html
  1. /*//problem: robot merge point
  2. //input:
  3. //robot: 1
  4. //obstacle: X
  5. [
  6. 0   0   0   M   1
  7. 0   1   X   0   0
  8. 0   X   0   0   0
  9. 0   0   0   1   0
  10. 0   0   0   0   0
  11. ]
  12. //output:
  13. //best merge point: M
  14. 3 + 1 + 3 = 7

  15. Definition: Best merge point: minimal sum of path num between robots and merge point*/
  16. public class Solution{
  17.         class Point{
  18.                 int x;
  19.                 int y;
  20.                 public Point(int x, int y){
  21.                         this.x = x;
  22.                     this.y = y;
  23.                 }
  24.         }
  25.         Point bestMergePoint(char[][] mat){
  26.          int m = mat.length;
  27.          if (m == 0) return null;
  28.          int n  = mat[0].length;
  29.        
  30.          // bfs each point that is 1
  31.          int[][] dis = new int[m][n];
  32.          for (int i = 0; i < m; i++){
  33.              for (int j = 0; j < n; j++){
  34.                  if (mat[i][j] == '1') bfs(mat, i, j, dis);
  35.              }
  36.          }
  37.          // count number
  38.          int min = Integer.MAX_VALUE;
  39.          Point ret = null;
  40.          for (int i = 0; i < m; i++){
  41.              for (int j = 0; j < n; j++){
  42.                      // if this point is not visited by a robot
  43.                      if (dis[i][j] == 0 && mat[i][j] == '0') continue;
  44.                  if (mat[i][j] != 'X' && dis[i][j] < min){
  45.                      min = dis[i][j];
  46.                      ret = new Point(i, j);
  47.                  }
  48.              }
  49.          }
  50.          return ret;
  51.         }

  52.         //bfs matrix to mark the distance from x and y
  53.         void bfs(char[][] mat, int x, int y, int[][] dis){
  54.                  Queue<Point> path = new LinkedList();
  55.                  int[] dx = {1,-1,0,0};
  56.                  int[] dy = {0,0,1,-1};
  57.                  int m = mat.length;
  58.                  int n = mat[0].length;
  59.                  boolean[][] visited = new boolean[m][n];
  60.                  int ct = 0;
  61.                  System.out.println("Started at: " + x + " " + y);
  62.                  int di = 0;
  63.                  path.offer(new Point(x, y));
  64.                  visited[x][y] = true;
  65.                  while (!path.isEmpty()){
  66.                      int count = path.size();
  67.                      for (int i = 0; i < count; i++){
  68.                          Point cur = path.poll();
  69.                          ct++;
  70.                          // System.out.println(cur.x + " " + cur.y);
  71.                          // visited[cur.x][cur.y] = true;
  72.                          // update cur.value
  73.                          dis[cur.x][cur.y] += di;
  74.                          // check its neighbors
  75.                          for (int j = 0; j < 4; j++){
  76.                              int nx = cur.x + dx[j];
  77.                              int ny = cur.y + dy[j];
  78.                              if (nx >= 0 && nx < m && ny >= 0 && ny < m && mat[nx][ny] != 'X' && !visited[nx][ny]){
  79.                                  path.offer(new Point(nx, ny));
  80.                                  visited[nx][ny] = true;
  81.                              }
  82.                          }
  83.                      }
  84.                      di++;
  85.                  }
  86.                  System.out.println("total points added to queue: " + ct);
  87.         }
  88.         public static void main(String[] args){
  89.                 Solution sln = new Solution();
  90.                 char[][] mat = {{'0', '0', '0', '0', '1'},
  91.                                                 {'0', '1', 'X', '0', '0'},
  92.                                                 {'0', 'X', '0', '0', '0'},
  93.                                                 {'0', '0', '0', '1', '0'},
  94.                                                 {'0', '0', '0', '0', '0'}};
  95.                 Point ret = sln.bestMergePoint(mat);
  96.                 System.out.println(ret.x + " " + ret.y);
  97.         }
  98. }
复制代码
回复

使用道具 举报

🔗
 楼主| kennethinsnow 2015-10-23 10:36:22 | 只看该作者
全局:
  1. public class Solution {
  2.         int longest_chain(String[] w) {
  3.         int len = w.length;
  4.         if (len == 0) return 0;
  5.         Queue<String> cBuf = new LinkedList();
  6.         Map<Integer, Set<String>> allWd = new HashMap();
  7.         int maxLen = 0;
  8.         int maxCLen = 0;
  9.         // find the longest length of strings in w
  10.         for (int i = 0; i < w.length; i++){
  11.             int curLen = w[i].length();
  12.             maxLen = Math.max(maxLen, curLen);
  13.             if (allWd.containsKey(curLen)) allWd.get(curLen).add(w[i]);
  14.             else{
  15.                 Set<String> curSet = new HashSet();
  16.                 curSet.add(w[i]);
  17.                 allWd.put(curLen, curSet);
  18.             }
  19.         }
  20.         // search from certain length of words
  21.         for (int i = maxLen; i > maxCLen; i--){
  22.             cBuf = new LinkedList(allWd.get(i));
  23.             // start from initial set of words to search for chain
  24.             int clen = 0;
  25.             while (!cBuf.isEmpty()){
  26.                 clen++;
  27.                 Set<String> nextSet = allWd.get(i - clen);
  28.                 if (nextSet == null) break;
  29.                 int count = cBuf.size();
  30.                 for (int h = 0; h < count; h++){
  31.                     String t = cBuf.poll();
  32.                     // get next string by deleting one letter
  33.                     for (int l = 0; l < t.length(); l++){
  34.                         String tt = t.substring(0, l) + t.substring(l + 1);
  35.                         if (nextSet.contains(tt)){
  36.                             cBuf.offer(tt);
  37.                             nextSet.remove(tt);
  38.                         }
  39.                     }
  40.                 }
  41.             }
  42.             maxCLen = Math.max(maxCLen, clen);
  43.             i -= Math.max(clen - 1, 0);
  44.         }
  45.         return maxCLen;
  46.     }
复制代码
回复

使用道具 举报

🔗
 楼主| kennethinsnow 2015-10-23 10:38:36 | 只看该作者
全局:
nqueen变形
棋盘上放queen 已经保证同一行或者同一列不会出现2个queen,求出对于每个Queen最大的威胁次数威胁指只要一个queen所能移动的范围(对于这道题就是对角线)内有别的queen就算威胁。
  1.         int maxThreats(int[] a) {
  2.         int len = a.length;
  3.         if (len == 0) return 0;
  4.         int[] threat = new int[len]; // max threats for queen in each row
  5.         Map<Integer, Integer> diag1 = new HashMap();
  6.         Map<Integer, Integer> diag2 = new HashMap();
  7.         for (int i = 0; i < len; i++){
  8.             int col = a[i] - 1;
  9.             if (diag1.containsKey(i - col)){
  10.                 threat[i]++;
  11.                 threat[diag1.get(i - col)]++;
  12.             }
  13.             diag1.put(i - col, i);
  14.             if (diag2.containsKey(i + col)){
  15.                 threat[i]++;
  16.                 threat[diag2.get(i + col)]++;
  17.             }
  18.             diag2.put(i + col, i);
  19.         }
  20.         int maxThreat = 0;
  21.         for (int i = 0; i < len; i++){
  22.             maxThreat = Math.max(maxThreat, threat[i]);
  23.         }
  24.         return maxThreat;
  25.     }
复制代码
回复

使用道具 举报

🔗
 楼主| kennethinsnow 2015-10-23 10:43:13 | 只看该作者
全局:
虫吃叶子 给定一个int作为叶子的数量,再给一个数组,其中每个数代表一个虫行走的步数, 求没有被吃掉的叶子数量。 比如n = 100, bug = {2,5}, 那么一百片叶子中号码是2或者5的倍数的叶子全部被吃掉了。
  1. int numberOfUneaten(int n, int[] p){
  2.                 // find non divisible numbers in p
  3.                 Arrays.sort(p);
  4.                 for (int i = 0; i < p.length; i++){
  5.                         for (int j = 0; j < i; j++){
  6.                                 if (p[j] != 0 && p[i] % p[j] == 0){
  7.                                         p[i] = 0;
  8.                                         break;
  9.                                 }
  10.                         }
  11.                 }
  12.                 // find lcm of numbers in p
  13.                 int l = 1;
  14.                 for (int i = 0; i < p.length; i++){
  15.                         if (p[i] != 0){
  16.                                 l = lcm(l, p[i]);
  17.                         }
  18.                 }
  19.                 int res = n % l;
  20.                 // find undivisible numbers from 1 to min(lcm, n)
  21.                 int[] mark = new int[Math.min(l, n) + 1];
  22.                 for (int i = 0; i < p.length; i++){
  23.                         if (p[i] != 0){
  24.                                 for (int j = p[i]; j <= Math.min(l, n); j += p[i]){
  25.                                         mark[j] = 1;
  26.                                 }
  27.                         }
  28.                 }
  29.                 int count = 0;
  30.                 int count1 = 0;
  31.                 for (int i = 1; i < Math.min(l, n); i++){
  32.                         if (mark[i] == 0){
  33.                                 count++;
  34.                                 if (i <= res) count1++;
  35.                         }
  36.                 }
  37.                
  38.                 // find undivisible numbers from 1 to n
  39.                 if (n < l) return count;
  40.                 else{
  41.                         return n / l * count + count1;
  42.                 }
  43.         }
  44.         int lcm(int a, int b){
  45.                 return a * b / gcd(a, b);
  46.         }
  47.         int gcd(int a, int b){
  48.                 if (a < b) return gcd(b, a);
  49.                 return b == 0 ? a : gcd(b, a % b);
  50.         }
复制代码
回复

使用道具 举报

🔗
xiaoquexing 2015-10-23 10:50:05 | 只看该作者
全局:
赞,楼主很用心啊
回复

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

本版积分规则

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