📣 独立日限时特惠: VIP通行证立减$68
回复: 21
跳转到指定楼层
上一主题 下一主题
收起左侧

02/09 F家onsite面经

全局:

2015(1-3月) 码农类General 硕士 全职@meta - 内推 - Onsite  | | Other | 应届毕业生

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

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

x
地里的面经帮助很大。面完了onsite来发一个Facebook的面经,回报地里。
一面:给一个board,上面有0 和1,1不
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
#05580">和判断一个树是不是另一个树的子树。



上一篇:有做过zappos OA的同学么?8道题的。
下一篇:amazon实习面试结果要等多长时间

本帖被以下淘专辑推荐:

推荐
sealove999 2016-4-18 15:51:54 | 只看该作者
全局:
一面
  1. public class Solution {
  2.   public int minsteps(int board[][], int starti, int startj, int endi, int endj) {
  3.     int dirs[][] = new int[][] {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
  4.     int m = board.length;
  5.     int n = board[0].length;

  6.     boolean visited[][] = new boolean[m][n];
  7.     Deque<int[]> queue = new ArrayDeque<>(); // bfs

  8.     queue.offer(new int[] {starti, startj});
  9.     visited[starti][startj] = true;

  10.     int level = 0;
  11.     while (!queue.isEmpty()) {
  12.       level++;
  13.       int count = queue.size();
  14.       for (int i = 0; i < count; i++) {
  15.         int[] node = queue.poll();
  16.         for (int[] dir : dirs) {
  17.           int x = node[0] + dir[0];
  18.           int y = node[1] + dir[1];
  19.           if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] != 1 && !visited[x][y]) {
  20.             if (x == endi && y == endj) {
  21.               return level;
  22.             }
  23.             queue.offer(new int[] {x, y});
  24.             visited[x][y] = true;
  25.           }
  26.         }
  27.       }
  28.     }
  29.     return -1;
  30.   }

  31.   public List<int[]> minpath(int board[][], int starti, int startj, int endi, int endj) {
  32.     int dirs[][] = new int[][] {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
  33.     int m = board.length;
  34.     int n = board[0].length;

  35.     Map<Integer, Integer> parent = new HashMap<>(); // int[] cannot equals
  36.     boolean visited[][] = new boolean[m][n];
  37.     Deque<int[]> queue = new ArrayDeque<>(); // bfs

  38.     queue.offer(new int[] {starti, startj});
  39.     visited[starti][startj] = true;

  40.     while (!queue.isEmpty()) {
  41.       int count = queue.size();
  42.       for (int i = 0; i < count; i++) {
  43.         int[] node = queue.poll();
  44.         for (int[] dir : dirs) {
  45.           int x = node[0] + dir[0];
  46.           int y = node[1] + dir[1];
  47.           if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] != 1 && !visited[x][y]) {
  48.             queue.offer(new int[] {x, y});
  49.             visited[x][y] = true;
  50.             parent.put(x * n + y, node[0] * n + node[1]);
  51.             if (x == endi && y == endj) {
  52.               LinkedList<int[]> ret = new LinkedList<>();
  53.               while (x * n + y != starti * n + startj) {
  54.                 ret.offerFirst(new int[] {x, y});
  55.                 int p = parent.get(x * n + y);
  56.                 x = p / n;
  57.                 y = p % n;
  58.               }
  59.               return ret;
  60.             }
  61.           }
  62.         }
  63.       }
  64.     }
  65.     return null;
  66.   }

  67.   public static void main(String[] args) {
  68.     Solution s = new Solution();
  69.     System.out.println(s.minsteps(new int[][] {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}, 0, 1, 1, 1));
  70.     System.out.println(s.minsteps(new int[][] {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}, 0, 1, 2, 1));
  71.     System.out.println(s.minsteps(new int[][] {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}, 0, 1, 2, 2));
  72.     System.out.println();

  73.     System.out.println(s.minpath(new int[][] {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}, 0, 1, 1, 1));
  74.     for (int[] t : s.minpath(new int[][] {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}, 0, 1, 2, 1))
  75.       System.out.print("(" + t[0] + "," + t[1] + ") ");
  76.     System.out.println();
  77.     for (int[] t : s.minpath(new int[][] {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}, 0, 1, 2, 2))
  78.       System.out.print("(" + t[0] + "," + t[1] + ") ");
  79.     System.out.println();
  80.   }
  81. }
复制代码

补充内容 (2016-4-18 15:55):
二面,
    int dirs[][] = // 马走日
        new int[][] {{1, 2}, {-1, 2}, {1, -2}, {-1, -2}, {2, 1}, {-2, 1}, {2, -1}, {-2, -1}};
回复

使用道具 举报

推荐
sealove999 2016-4-18 16:06:20 | 只看该作者
全局:
三面
  1. public class Solution {
  2.   boolean issametree(TreeNode root1, TreeNode root2) {
  3.     if (root1 == null && root2 == null)
  4.       return true;
  5.     if (root1 != null && root2 != null) {
  6.       return root1.val == root2.val && issametree(root1.left, root2.left)
  7.           && issametree(root1.right, root2.right);
  8.     }
  9.     return false;
  10.   }

  11.   public boolean issubtree(TreeNode big, TreeNode small) {
  12.     if (big != null && small != null) {
  13.       if (big.val == small.val) {
  14.         if (issametree(big, small))
  15.           return true;
  16.       } else {
  17.         if (big.left != null && issubtree(big.left, small)) {
  18.           return true;
  19.         }
  20.         if (big.right != null && issubtree(big.right, small)) {
  21.           return true;
  22.         }
  23.       }
  24.     }
  25.     return false;
  26.   }


  27.   public static void main(String[] args) {
  28.     Solution s = new Solution();
  29.     TreeNode tn1 = new TreeNode(1);
  30.     TreeNode tn2 = new TreeNode(2);
  31.     TreeNode tn3 = new TreeNode(3);
  32.     TreeNode tn4 = new TreeNode(4);
  33.     TreeNode tn5 = new TreeNode(5);
  34.     tn1.left = tn2;
  35.     tn1.right = tn3;
  36.     tn4.left = tn5;
  37.     System.out.println(s.issubtree(tn1, tn2));
  38.     System.out.println(s.issubtree(tn1, tn4));
  39.   }
  40. }
复制代码
回复

使用道具 举报

推荐
Wrath 2016-4-21 06:26:15 | 只看该作者
全局:

如果只是把directions换成日字形走法的话万一中间走过的点有1怎么办,那样就不能走到那个点了。而且还得考虑[-1, 0]这种横向走一步,纵向来回走两步的情况,那样directions矩阵就有很多情况了。
回复

使用道具 举报

🔗
kidzlike 2016-2-19 00:49:09 | 只看该作者
全局:
卧槽。。。妥妥的offer啊
回复

使用道具 举报

🔗
iwofr 2016-2-19 01:34:31 | 只看该作者
全局:
lz能稍微说下判断一个树是不是另一个树的子树的那个题吗?是给一个subtree的root然后判断这个root在不在大树的里面,还是要检查大树里面的那个子树和另一个树所有的node都一样才行?
回复

使用道具 举报

🔗
 楼主| NR21 2016-2-19 02:43:30 | 只看该作者
全局:
iwofr 发表于 2016-2-19 01:34
lz能稍微说下判断一个树是不是另一个树的子树的那个题吗?是给一个subtree的root然后判断这个root在不在大 ...

直接给了一个子树,问这个子树存在于大的数与否。不需要大树要到leaves。
回复

使用道具 举报

🔗
silenceleaf 2016-2-19 02:53:59 | 只看该作者
全局:
FB又开始招fulltime了嘛
回复

使用道具 举报

🔗
iwofr 2016-2-19 03:36:22 | 只看该作者
全局:
NR21 发表于 2016-2-19 02:43
直接给了一个子树,问这个子树存在于大的数与否。不需要大树要到leaves。

是在大树里面找到和小树root value一样的node然后check isSameTree吗?
回复

使用道具 举报

🔗
 楼主| NR21 2016-2-19 04:04:35 | 只看该作者
全局:
iwofr 发表于 2016-2-19 03:36
是在大树里面找到和小树root value一样的node然后check isSameTree吗?

是的字数字数
回复

使用道具 举报

🔗
 楼主| NR21 2016-2-19 04:04:43 | 只看该作者
全局:
silenceleaf 发表于 2016-2-19 02:53
FB又开始招fulltime了嘛

貌似是的呢
回复

使用道具 举报

🔗
iwofr 2016-2-19 05:06:34 | 只看该作者
全局:

谢谢!祝好运啊!我也马上onsite 了!求好运!
回复

使用道具 举报

🔗
queeniejing 2016-2-23 05:48:32 | 只看该作者
全局:
LZ onsite 只有三轮吗?
回复

使用道具 举报

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

本版积分规则

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