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

Patreon - Senior Software Engineer

全局:

2022(1-3月) 工程类 本科 全职@patreon - 猎头 - Onsite 视频面试  | 😃 Positive 😐 Average | Pass | 在职跳槽

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

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

x
本帖最后由 tomzhawang 于 2022-12-13 16:02 编辑

**Phone screen**
  1. We want to protect an API endpoint with an in-memory rate limiter.
  2. The rate limiter should be initialized with a value that represents the maximum requests
  3. per second we want to allow. When a request hits the endpoint, the rate limiter is
  4. asked whether it has capacity to process this request and will reject or accept the request appropriately.

  5. Assume:
  6. _ Time is measured in millisecond resolution
  7. - API requests come in sequentially

  8. Design the interface and implement the logic for the rate limiter.

  9. Example at a max of 2 requests/sec:
  10. 12:00:01.100 PASS
  11. 12:00:01.200 PASS
  12. 12:00:01.300 FAIL
  13. 12:00:02.100 PASS
  14. 12:00:02.150 FAIL
  15. 12:00:02.200 PASS
复制代码
  1. from collections import deque as queue
  2. class Bucket:
  3.     def __init__(self, window_size_ms, max_requests_per_window):
  4.         self.window_size_ms = window_size_ms
  5.         self.max_requests_per_window = max_requests_per_window
  6.         self.current_num_requests_in_window = 0
  7.         self.queue = queue([])

  8.     def submit_request(self, timestamp_ms):
  9.         if self.queue:
  10.             last_time_stamp = self.queue[0]
  11.             time_diff = timestamp_ms - last_time_stamp

  12.             if time_diff >= self.window_size_ms:
  13.                 self.current_num_requests_in_window -= 1
  14.                 self.queue.popleft()

  15.         if self.current_num_requests_in_window < self.max_requests_per_window:
  16.             self.queue.append(timestamp_ms)
  17.             self.current_num_requests_in_window += 1
  18.             return True
  19.         return False

  20. def test():
  21.     bucket = Bucket(1000, 2)
  22.     for timestamp in (1100, 1200, 1300, 2100, 2150, 2200):
  23.         print(timestamp, bucket.submit_request(timestamp))

  24. # Follow ups:
  25. # 1. What if you have lots of users with different user ids? How do you handle their limits?
  26. ## Map<userId, bucket>
  27. # 2. What if you want to weight the requests, what do you do?
  28. ## Change the weight in submitRequest
  29. # 3. What if you have a distributed system? How do you manage the rate limiter?
  30. ## Use a cache like Redis, potentially backed by a DB. Use SQL for consistency, NOSQL
  31. ## if consistency isn't a must.
复制代码
**Question 2**

  1. We want to create a function that mimics the cd command.

  2. def cd(current, new):
  3.     current = current.strip("/")
  4.     path_so_far = [] if (current == "") else current.split("/")
  5.     parts = new.split("/")
  6.     for part in parts:
  7.         if not part:
  8.             continue
  9.         if part == "..":
  10.             if path_so_far:
  11.                 path_so_far.pop()
  12.         elif part == ".":
  13.             continue
  14.         else:
  15.             path_so_far.append(part)
  16.     result = "/".join(path_so_far)
  17.     return "/" + result

  18. # Test cases:
  19. current = "/"
  20. new = "a"
  21. output = "/a"

  22. current = "/b"
  23. new = "c"
  24. output = "/b/c"

  25. current = "/d"
  26. new = "/e"
  27. output = "/e"

  28. current = "/foo/bar"
  29. new = ".."
  30. output = "/foo"

  31. current = "/r/s"
  32. new = "../p/q"
  33. output = "/r/p/q"

  34. current = "/x/y"
  35. new = "p/./q"
  36. output = "/x/y/p/q"
复制代码
Onsite

You're building a shopping cart pricer app for
your local grocery store. They sell many types of
items and accept coupons. One type of coupon
discounts an item's price by a percentage (e.g.
10% off). Another tybe of coupon gives the
shopper a dollar amount discount if a minimum
count of the item is purchased (e.g. $5 off if you
buy 2 or more). A shopper may only use one coupon on a type of item.
The app should compute the price given a shopping cart and
a set of coupons applied.

Keep in mind the grocery store is planning to
accept new types of c
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
o retrieve content from creators with many followers. proactively discuss notification feed cache failure, caching recent posts, using a CDN, notification spam, failure, recovery)

The back-of-the-envelope calculations are not important. Make sure to state whether it's read-heavy or write-heavy.

Talk about rate limiting the notifications.

Draw the box diagram first.

评分

参与人数 5大米 +24 收起 理由
玉米种植专业户 + 1 给你点个赞!
ff12 + 1 给你点个赞!
Chasedream.df + 1 赞一个
wantyoulee + 1 很有用的信息!
匿名用户-BSXZZ + 20

查看全部评分


上一篇:Bloomberg VO1 面经
下一篇:Amazon L7 挂经
地里匿名用户
🔗
匿名用户-5NP1O  2024-4-19 05:20:18 来自APP
楼主, 请问这个是后端是吧
回复

使用道具 举报

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

本版积分规则

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