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

热带雨林电面

全局:

2020(7-9月) 码农类General 硕士 全职@amazon - 内推 - 技术电面  | | Other | 在职跳槽

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

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

x
非常有意思的一道题
题目是:
/*
We are working on a data stream processing service.
The data is modeled as "Element" class and the data stream is represented with "ElementStream" interface.
We've noticed that our data stream contains quite a few duplicates which is impacting our downstream processing.
You
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies


Example:
ElementStream stream;
DedupedStream dedupedStream = DedupedStream(stream)
dedupedStream.getNext() -> a
dedupedStream.getNext() -> b
dedupedStream.getNext() -> c
dedupedStream.getNext() -> e

评分

参与人数 2大米 +11 收起 理由
archerw + 1 很有用的信息!
匿名用户-514RY + 10

查看全部评分


上一篇:小金人 2021NG OA
下一篇:巨婴阿祖儿店面
🔗
 楼主| cier112 2020-8-1 11:44:48 | 只看该作者
全局:
  1. /*
  2. We are working on a data stream processing service.
  3. The data is modeled as "Element" class and the data stream is represented with "ElementStream" interface.
  4. We've noticed that our data stream contains quite a few duplicates which is impacting our downstream processing.
  5. You are asked to implement a wrapper class to the original data stream to help deduplicate.

  6. Imagine you are sharing your work with the team, please write code that is as production ready as possible
  7. */

  8. // GIVEN
  9. class Element {
  10.     String id;
  11.     long timestamp;
  12. }

  13. interface ElementStream {
  14.     // Return True if stream has more elements, otherwise False
  15.     boolean hasNext();
  16.     // Return and ** remove ** next element from the stream.
  17.     // Throw NoSuchElementException if there's no more element to return.
  18.     Element getNext() throws NoSuchElementException;
  19. }

  20. class DedupedStream implements ElementStream {
  21.     // GIVEN
  22.     ElementStream stream;
  23.     HashSet<String> processed = new HashSet<>();
  24.     Element mostRecent = null;
  25.    
  26.     // a
  27.     // set: a
  28.     mostrecent: a
  29.     // a a b
  30.     //     |
  31.     @Override
  32.     boolean hasNext() {
  33.         // Write your code here
  34.         // original stream doesn't have next



  35.         if (mostRecent != null) return true;
  36.         if (!stream.hasNext()) return false;

  37.         try {
  38.             mostRecent = stream.getNext();
  39.             // check dupliatition
  40.             // if we saw this element before
  41.             if (processed.contains(mostRecent.id)) {
  42.                 mostRecent = null;
  43.                 return hasNext();
  44.             // if we have never saw this element before
  45.             } else {
  46.                 processed.add(mostRecent.id);
  47.                 return true;
  48.             }
  49.         } catch(e) {
  50.             // no elements to return;
  51.             return false;
  52.         }
  53.     }
  54.     // most = a
  55.     // a  a   b c d
  56.          get
  57.     @Override
  58.     Element getNext() {
  59.         // Write your code here
  60.         Element res = mostRecent;
  61.         if (hasNext()) {
  62.             mostRecent = null;
  63.             return res;
  64.         } else return null;
  65.     }
  66. }

  67. // a a b c d
  68. // a b c a
  69.         false
  70. /*
  71.     hashset, put all data that you read
  72.     hashset contains, skip or put it hashset
  73. */
  74. ElementStream stream;
  75. //stream.getNext() -> a
  76. //stream.getNext() -> a
  77. //stream.getNext() -> b
  78. //stream.hasNext() -> true

  79. DedupedStream dedupedStream = DedupedStream(stream)
  80. dedupedStream.getNext() -> a
  81. dedupedStream.getNext() -> b
  82. dedupedStream.getNext() -> c
  83. dedupedStream.getNext() -> e
复制代码
回复

使用道具 举报

全局:
请问lz电面是在OA之后吗
回复

使用道具 举报

🔗
ZionHuang 2020-8-19 13:00:29 | 只看该作者
全局:
谢谢 祝楼主好运
回复

使用道具 举报

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

本版积分规则

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