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

Rubrik电面

全局:

2017(7-9月) 码农类General 硕士 全职@rubrik - 内推 - 技术电面  | | Pass | 应届毕业生

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

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

x
好几天没发面经了,发个两天前面的攒点RP
美国小哥,和之前的面经全都不一样,题目不算难,但考察范围很全,从代码风格到设计思维(包括返回值以什么形式等等)都有考察,感觉比有些喜欢出偏怪难题的面试好多了。
设计一个扫雷游戏。我一开始说要分两阶段,一阶段是布雷,一阶段是扫,然后小哥说布雷不用做了,假设你的constructor就传入了布好雷的matrix。(表示很失望,不能show一发O(k) partial Fisher-Yates随机算法了)
扫雷就是一个clickTile(int row, int c
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
路,继续探索新的区域。每次我们markTile()的时候检查hasWon(),我们就能确定有没有赢了。
做完还有十分钟左右,小哥表示非常满意(我自己也很满意,这也算是我目前电面遇到最复杂的题了,算法不难,但有很多设计思想自己还是都马上想到了),问了两个问题就结束了
  1. /**
  2. * Minesweeper Game
  3. * http://minesweeperonline.com/
  4. *
  5. * 1) Game should take in a pre-populated matrix representing a minesweeper board like:
  6. *
  7. *    B X B X         B = bomb
  8. *    B B X X       X = no bomb
  9. *    X B X X
  10. *    B X X X
  11. *
  12. * 2) Expose function clickTile(row, col) which clicks the tile and returns information about
  13. *    the clicked tile (and surrounding tiles) that satisfies the following:
  14. *  
  15. *    - If neighboring bombs, return the # surrounding bombs
  16. *    - If no neighboring bombs, return set of surrounding tiles with
  17. *      neighboring bombs (and count of bombs)
  18. *    - If bomb clicked, indicate to player they lose
  19. *    - Should keep track of which tiles have been clicked
  20. *
  21. *    Example Calls:
  22. *     clickTile(0, 1) -> count of 4
  23. *     clickTile(3, 3) -> (3, 2) has 1 surrounding bomb
  24. *                               (3, 3) has 0 surrounding bombs
  25. *                               (2, 2) has 2 surrounding bombs
  26. *                               (2, 3) has 0 surrounding bombs
  27. *              (1, 2) has 2 surrounding bombs
  28. *               (1, 3) has 1 surrounding bomb
  29. *
  30. */

  31. class Point {
  32.   int x;
  33.   int y;
  34.   int count;
  35.   Point(int x, int y, int count) {
  36.     this.x = x;
  37.     this.y = y;
  38.     this.count = count;
  39.   }
  40. }

  41. class ReturnType {
  42.   boolean isAlive;
  43.   List<Point> surroundings;
  44.   ReturnType(boolean isAlive) {
  45.     this.isAlive = isAlive;
  46.     this.surroundings = new ArrayList<>();
  47.   }
  48. }

  49. class Minesweeper {
  50.   private final int[][] dirs = {
  51.     { -1, -1 }, { -1, 0 }, { -1, 1 },
  52.     { 0, -1 }, { 0, 1 },
  53.     { 1, -1 }, { 1, 0 }, { 1, 1 }
  54.   };
  55.   
  56.   private char[][] matrix;
  57.   private int m;
  58.   private int n;
  59.   
  60.   public Minesweeper(char[][] matrix) {
  61.     this.matrix = matrix;
  62.     this.m = matrix.length;
  63.     this.n = matrix[0].length;
  64.   }
  65.   
  66.   public ReturnType clickTile(int row, int col) {
  67.     if (matrix[row][col] == 'B') {
  68.       return new ReturnType(false);
  69.     }
  70.    
  71.     int count = countNeighbors(row, col);
  72.     if (count > 0) {
  73.       Point here = new Point(row, col, count);
  74.       ReturnType res = new ReturnType(true);
  75.       res.surroundings.add(here);
  76.       return res;
  77.     }
  78.    
  79.     boolean[][] visited = new boolean[m][n];
  80.     List<Point> surroundings = new ArrayList<>();
  81.     visited[row][col] = true;
  82.     dfs(row, col, visited, surroundings);
  83.    
  84.     ReturnType res = new ReturnType(true);
  85.     res.surroundings = surroundings;
  86.     return res;
  87.   }
  88.   
  89.   private int countNeighbors(int x, int y) {
  90.     int count = 0;
  91.     for (int[] dir : dirs) {
  92.       int xx = x + dir[0];
  93.       int yy = y + dir[1];
  94.       
  95.       if (xx < 0 || xx >= m || yy < 0 || yy >= n) {
  96.         continue;
  97.       }
  98.       
  99.       if (matrix[xx][yy] == 'B') {
  100.         count++;
  101.       }
  102.     }
  103.     return count;
  104.   }
  105.   
  106.   private void dfs(int x, int y, boolean[][] visited, List<Point> surroundings) {
  107.     int count = countNeighbors(x, y);
  108.     if (count > 0) {
  109.       Point point = new Point(x, y, count);
  110.       surroundings.add(point);
  111.     }
  112.    
  113.     for (int[] dir : dirs) {
  114.       int xx = x + dir[0];
  115.       int yy = y + dir[1];
  116.       
  117.       if (xx < 0 || xx >= m || yy < 0 || yy >= n || visited[xx][yy] || matrix[xx][yy] == 'B') {
  118.         continue;
  119.       }
  120.       
  121.       visited[xx][yy] = true;
  122.       dfs(xx, yy, visited, surroundings);
  123.     }
  124.   }
  125.   
  126.   public void markTileAsBomb(row, col);
  127.   public boolean hasWon();
  128. }
复制代码

评分

参与人数 8大米 +69 收起 理由
sophietao + 3 给你点个赞!
turfkids + 3 很有用的信息!
shawnpanda + 2 给你点个赞!
又见紫风铃 + 10 感谢分享!
wcbsxmj123 + 3 感谢分享!

查看全部评分


上一篇:高盛 OA
下一篇:FB电面

本帖被以下淘专辑推荐:

  • · Rubrik|主题: 18, 订阅: 3
🔗
wtcupup 2017-9-9 10:05:47 | 只看该作者
全局:
求楼主继续发个virtual onsite的面筋
回复

使用道具 举报

🔗
 楼主| 面壁者 2017-9-9 11:01:35 | 只看该作者
全局:
wtcupup 发表于 2017-9-9 10:05
求楼主继续发个virtual onsite的面筋

下周面了发
回复

使用道具 举报

🔗
刷题 2017-9-9 11:44:24 | 只看该作者
全局:
一堆帖子看过来觉得楼主是算法 web 基础知识全面精通 有强悍实习经历的牛校大神
回复

使用道具 举报

🔗
tonywen2014 2017-9-10 10:09:51 | 只看该作者
全局:
是不是就是里口529?
回复

使用道具 举报

🔗
 楼主| 面壁者 2017-9-10 10:37:29 | 只看该作者
全局:
tonywen2014 发表于 2017-9-10 10:09
是不是就是里口529?

主体部分差不多,但是不是返回点以后的棋盘,要自己设计一种返回类型包括所有需要的信息,然后还有些follow up比如怎么做扫雷机器人
回复

使用道具 举报

🔗
iwlam525 2017-10-31 04:13:19 | 只看该作者
全局:
.....居然跟我刚刚FB电面好像。。。不过我没做过minessweeper,临场发挥还有bug没来得及改,估计跪了。。。
回复

使用道具 举报

🔗
guorenwang 2017-11-20 12:56:04 | 只看该作者
全局:
请问楼主这个算法的time和space是多少?
回复

使用道具 举报

🔗
shanewww 2018-3-8 08:33:40 | 只看该作者
全局:
楼主的dfs是算了八个方向么? 扫雷里面dfs是不是只算四个方向的?😂

补充内容 (2018-3-8 08:55):
我错了。。请无视这个评论。。。是因为太久不玩了么。。。
回复

使用道具 举报

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

本版积分规则

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