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

Uber onsite 面经

全局:

2016(1-3月) 码农类General 硕士 全职@uber - 猎头 - Onsite  | | Fail | 在职跳槽

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

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

x
1. Merge Two Sorted Lists
2. Sparse Matrix Multiplication
3. 挂在这道:


给一个n列类似俄罗期方块的盘, 往下掉方块. 方块定义如下:
class Block {
        i
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
atsapp

后面花了好几天用segment tree给解了第3题.


  1. public class FallingBlock {

  2.     static class Block {
  3.         int left;
  4.         int right;
  5.         int height;

  6.         public Block(int left, int right, int height) {
  7.             this.left = left;
  8.             this.right = right;
  9.             this.height = height;
  10.         }
  11.     }

  12.     private int boardWidth;
  13.     int[] heights;

  14.     // width is the board width
  15.     public FallingBlock(int width) {
  16.         // Height of segment tree
  17.         int x = (int) (Math.ceil(Math.log(width) / Math.log(2)));
  18.         // Maximum size of segment tree
  19.         int maxSize = 2 * (int) Math.pow(2, x) - 1;
  20.         heights = new int[maxSize]; // Memory allocation
  21.         boardWidth = width;
  22.     }

  23.     public void fallBlock(Block block) throws IllegalArgumentException {
  24.         fallBlockInternal(block, 0, boardWidth - 1, 0);
  25.     }


  26.     public int fallBlockInternal(Block block, int start, int end, int index) {
  27.         // haven't init this segment yet
  28.         if (heights[index] == 0) heights[index] = heights[(index - 1) / 2];

  29.         if (start >= block.left && start <= block.right) {
  30.             // If segment of this node is a part of given range, then
  31.             heights[index] += block.height;
  32.             // System.out.println("start:" + start + " end:" + end + " index:" + index + " height:" + heights[index]);
  33.             return heights[index];
  34.         } else if (start > block.right || end < block.left) {
  35.             // If segment of this node is outside the given range
  36.             // System.out.println("start:" + start + " end:" + end + " index:" + index + " height:" + heights[index]);
  37.             return heights[index];
  38.         } else {
  39.             // If a part of this segment overlaps with the given range
  40.             int mid = (start + end) >>> 1;
  41.             heights[index] = Math.max(
  42.                     fallBlockInternal(block, start, mid, index * 2 + 1),
  43.                     fallBlockInternal(block, mid + 1, end, index * 2 + 2));
  44.             // System.out.println("start:" + start + " end:" + end + " index:" + index + " height:" + heights[index]);
  45.             return heights[index];
  46.         }
  47.     }

  48.     public int getMaxHeight() {
  49.         return heights[0];
  50.     }

  51.     public static void main(String[] args) throws Exception {
  52.         FallingBlock fallingBlock = new FallingBlock(100);
  53.         fallingBlock.fallBlock(new Block(20, 50, 1));
  54.         fallingBlock.fallBlock(new Block(30, 70, 2));
  55.         fallingBlock.fallBlock(new Block(10, 40, 3));
  56. //        fallingBlock.fallBlock(new Block(80, 90, 10));

  57.         System.out.println("max height is " + fallingBlock.getMaxHeight());
  58.     }

  59. }
复制代码




评分

参与人数 2大米 +16 收起 理由
kittytok + 1 很有用的信息!
匿名用户-JRQQF + 15

查看全部评分


上一篇:Airbnb电面
下一篇:Robinhood Karat 电面 跪经
推荐
wangdaye 2019-12-5 13:38:42 | 只看该作者
全局:
第三题是不是想多了?可以直接用height[width]和最高maxHeight。每放一个block,找到left到right的最大高度h,然后更新left到right的高度h+block的height,如果新的高度比当前的maxHeight高就更新它。
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-DY98T  2019-12-2 08:48:26
卧槽在职跳槽也考线段树?
回复

使用道具 举报

🔗
shzhj 2019-12-2 09:04:45 来自APP | 只看该作者
全局:
这真的是四年前的面经么。。
回复

使用道具 举报

🔗
knight0clk 2019-12-2 14:55:56 | 只看该作者
全局:
楼主的第二题需要自己给sparse的matrix设计数据结构然后再求multiplication吗?
回复

使用道具 举报

全局:
Fallingblock那题可以用扫描线的方法做吗
回复

使用道具 举报

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

本版积分规则

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