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

求问:Two Sigma那道print timestamps in two blocking queue with diff < 1的题!

全局:

2016(7-9月) 码农类General 硕士 全职@twosigma - 猎头 - Onsite  | | Other | 在职跳槽

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

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

x
如题:Two Sigma那道print timestamps in two blocking queue with diff < 1的题,看了这位大神的解释还是不太懂,如果不加锁会造成什么问题呢?还有如果有C++的代码写法就更好啦


您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
觉这题板上都没有解释很清楚的,有没有哪位可以详细解释一下。。c++加锁和thread是用boost吗...a




评分

参与人数 1大米 +2 收起 理由
jiaozhu200601 + 2 感谢分享!

查看全部评分


上一篇:Snapchat SDET entry level 电面的题目会不同吗
下一篇:Facebook 面经
🔗
singledog2016 2016-7-11 12:05:39 | 只看该作者
全局:
楼主几号onsite?
回复

使用道具 举报

🔗
jiaozhu200601 2016-9-12 05:36:21 | 只看该作者
全局:
感谢分享,请问楼主offer了吗,我下周onsite。
回复

使用道具 举报

🔗
 楼主| ivanml 2016-9-12 06:58:45 | 只看该作者
全局:
jiaozhu200601 发表于 2016-9-12 05:36
感谢分享,请问楼主offer了吗,我下周onsite。

没有,撑到下午还是挂了
回复

使用道具 举报

🔗
jiaozhu200601 2016-9-12 09:11:53 | 只看该作者
全局:
ivanml 发表于 2016-9-12 06:58
没有,撑到下午还是挂了

请问有遇到什么新题目吗,如果没有,那么还只是遇到那两套老题的其中一套?
回复

使用道具 举报

🔗
 楼主| ivanml 2016-9-12 10:28:08 | 只看该作者
全局:
jiaozhu200601 发表于 2016-9-12 09:11
请问有遇到什么新题目吗,如果没有,那么还只是遇到那两套老题的其中一套?

就是老题,但是有随机的follow up
Good luck
回复

使用道具 举报

🔗
forestwn 2016-11-3 12:03:46 | 只看该作者
全局:
lz是用C++上机的吗?lock怎么写的?
回复

使用道具 举报

🔗
wbwt 2017-9-28 08:18:12 | 只看该作者
全局:
楼主搞懂题意了吗...同没有读懂题意,为什么需要加锁?他是指两个queue会实时有新的timestamp进来吗
回复

使用道具 举报

无效楼层,该帖已经被删除
🔗
Mike8848 2018-9-18 11:39:35 | 只看该作者
全局:
Sorry if this is a duplicate. I wanted to edit my reply but the website just swallowed my answer. What a f* great user experience.

  1. # PCQueue is a thread-safe, unbounded queue.
  2. # - push() appends an item to the end of the queue
  3. # - poll() polls an item from the head of the queue. It can block waiting.
  4. #
  5. # Queue is not thread safe

  6. # input source 1
  7. q1 = PCQueue()
  8. # input source 2
  9. q2 = PCQueue()
  10. # we unify the two streams here
  11. unified = PCQueue()
  12. # helper queues
  13. qs = [Queue(), Queue()]


  14. def thread1():
  15.   while True:
  16.     head = q1.poll()
  17.     unified.push((head, 1))

  18. def thread2():
  19.   while True:
  20.     head = q2.poll()
  21.     unified.push((head, 2))


  22. def processing_thread():
  23.   while True:
  24.     val, qid = unified.poll()

  25.     myq = qs[qid]
  26.     myq.append(val)
  27.     # the other queue
  28.     oq = qs[2 - qid]

  29.     oq_new_begin = 0
  30.     for i in xrange(len(oq)):
  31.       oval = oq[i]
  32.       diff = oval - val
  33.       if diff < -1:
  34.         oq_new_begin = i + 1
  35.       elif diff > 1:
  36.         break
  37.       else if diff != 0 or qid == 1:
  38.         print oval, val
  39.     del oq[:oq_new_begin]

  40. if __name__ == '__main__':
  41.   t1 = Thread(thread1)
  42.   t2 = Thread(thread2)
  43.   t3 = Thread(processing_thread)

  44.   t1.start()
  45.   t2.start()
  46.   t3.start()
复制代码
回复

使用道具 举报

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

本版积分规则

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