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

coinbase最新不常见oa

全局:

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

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

x
https://www.hack2hire.com/question-bank/companies/coinbase/coding-questions/69f2664c42791c82ab992693/practice?questionId=69f2665342791c82ab992694
这题很少出现,抽到了也是醉了。不过我拿了600/600,嘻嘻。我真厉害
This is a four-level Digital Recipe Manager problem. Level 1 is addRecipe / getRecipe / deleteRecipe: IDs auto-increment from "1", names are case-insensitive and a duplicate name returns an empty string, and getRecipe returns one flat array of name, then ingredients, then steps (empty if not found). Level 2 adds updateRecipe (can't rename to another recipe's name) plus search by ingredient (exact match, case-insensitive) and by name (substring, case-insensitive), both returning sorted IDs. Level 3 adds addUser and editRecipe, where the user must exist first, and starts version tracking: a version is only recorded if something actually changed, with the editor set to the userId, while updateRecipe always records a version with editor "system". Level 4 adds versionRecipe, which returns strings like "version:name:ingredientCount:stepCount:editor" in ascending version order, and rollbackRecipe, which restores an old version's content as a new version with editor "rollback" and fails if the old name now collides with another recipe.
A few gotchas visible in the code: addRecipe doesn't record a version, so the first edit is version 1. IDs are strings and sorted() is lexicographic, so "10" comes before "2", which may or may not be what the spec intends. And updateRecipe records a version even with no change while editRecipe doesn't, which is probably a deliberate rule in the problem.
class DigitalRecipeManagerImpl: def init(self): self.recipes = {} # id -> {"name", "ingredients", "steps"} self.id_by_lower_name = {} # lowercase name -> id self.history = {} # id -> list of version dicts self.next_id = 1 self.users = set()
  1. # ------------------------------------------------------------------
  2. # LEVEL 1: add / get / delete
  3. # ------------------------------------------------------------------
  4. def addRecipe(self, name, ingredients, steps):
  5.     lower = name.lower()
  6.     if lower in self.id_by_lower_name:
  7.         return ""  # duplicate name (case-insensitive)
  8.     recipe_id = str(self.next_id)
  9.     self.next_id += 1
  10.     self.recipes[recipe_id] = {
  11.         "name": name,
  12.         "ingredients": list(ingredients),
  13.         "steps": list(steps),
  14.     }
  15.     self.id_by_lower_name[lower] = recipe_id
  16.     return recipe_id
  17. def getRecipe(self, recipeId):
  18.     r = self.recipes.get(recipeId)
  19.     if r is None:
  20.         return []
  21.     return [r["name"]] + r["ingredients"] + r["steps"]
  22. def deleteRecipe(self, recipeId):
  23.     r = self.recipes.pop(recipeId, None)
  24.     if r is None:
  25.         return False
  26.     self.id_by_lower_name.pop(r["name"].lower(), None)
  27.     self.history.pop(recipeId, None)
  28.     return True
  29. # ------------------------------------------------------------------
  30. # LEVEL 2: update + search
  31. # ------------------------------------------------------------------
  32. def updateRecipe(self, recipeId, newName, newIngredients, newSteps):
  33.     r = self.recipes.get(recipeId)
  34.     if r is None:
  35.         return False
  36.     new_lower = newName.lower()
  37.     existing = self.id_by_lower_name.get(new_lower)
  38.     if existing is not None and existing != recipeId:
  39.         return False  # name taken by a different recipe
  40.     self.id_by_lower_name.pop(r["name"].lower(), None)
  41.     r["name"] = newName
  42.     r["ingredients"] = list(newIngredients)
  43.     r["steps"] = list(newSteps)
  44.     self.id_by_lower_name[new_lower] = recipeId
  45.     # updateRecipe ALWAYS records a version, even if nothing changed
  46.     self._append_version(recipeId, "system")
  47.     return True
  48. def searchRecipesByIngredient(self, ingredient):
  49.     needle = ingredient.lower()
  50.     result = [
  51.         rid for rid, r in self.recipes.items()
  52.         if any(i.lower() == needle for i in r["ingredients"])  # exact match
  53.     ]
  54.     return sorted(result)  # string sort: "10" < "2"
  55. def searchRecipesByName(self, query):
  56.     needle = query.lower()
  57.     result = [
  58.         rid for rid, r in self.recipes.items()
  59.         if needle in r["name"].lower()  # substring match
  60.     ]
  61.     return sorted(result)
  62. # ------------------------------------------------------------------
  63. # LEVEL 3: users + editRecipe (starts version tracking by user)
  64. # ------------------------------------------------------------------
  65. def addUser(self, userId):
  66.     if userId in self.users:
  67.         return False
  68.     self.users.add(userId)
  69.     return True
  70. def editRecipe(self, userId, recipeId, newName, newIngredients, newSteps):
  71.     if userId not in self.users:
  72.         return False
  73.     r = self.recipes.get(recipeId)
  74.     if r is None:
  75.         return False
  76.     new_lower = newName.lower()
  77.     existing = self.id_by_lower_name.get(new_lower)
  78.     if existing is not None and existing != recipeId:
  79.         return False
  80.     changed = (
  81.         r["name"] != newName
  82.         or r["ingredients"] != list(newIngredients)
  83.         or r["steps"] != list(newSteps)
  84.     )
  85.     self.id_by_lower_name.pop(r["name"].lower(), None)
  86.     r["name"] = newName
  87.     r["ingredients"] = list(newIngredients)
  88.     r["steps"] = list(newSteps)
  89.     self.id_by_lower_name[new_lower] = recipeId
  90.     # editRecipe only records a version if something actually changed
  91.     if changed:
  92.         self._append_version(recipeId, userId)
  93.     return True
  94. # ------------------------------------------------------------------
  95. # LEVEL 4: version listing + rollback
  96. # ------------------------------------------------------------------
  97. def versionRecipe(self, recipeId):
  98.     if recipeId not in self.recipes:
  99.         return []
  100.     entries = self.history.get(recipeId)
  101.     if not entries:
  102.         return []
  103.     return [
  104.         f'{e["version"]}:{e["name"]}:{len(e["ingredients"])}:{len(e["steps"])}:{e["editor"]}'
  105.         for e in sorted(entries, key=lambda e: e["version"])
  106.     ]
  107. def rollbackRecipe(self, recipeId, version):
  108.     r = self.recipes.get(recipeId)
  109.     if r is None:
  110.         return False
  111.     entries = self.history.get(recipeId)
  112.     if entries is None:
  113.         return False
  114.     target = next((e for e in entries if e["version"] == version), None)
  115.     if target is None:
  116.         return False
  117.     target_lower = target["name"].lower()
  118.     existing = self.id_by_lower_name.get(target_lower)
  119.     if existing is not None and existing != recipeId:
  120.         return False  # old name now belongs to another recipe
  121.     self.id_by_lower_name.pop(r["name"].lower(), None)
  122.     r["name"] = target["name"]
  123.     r["ingredients"] = list(target["ingredients"])
  124.     r["steps"] = list(target["steps"])
  125.     self.id_by_lower_name[target_lower] = recipeId
  126.     # rollback is recorded as a NEW version, editor = "rollback"
  127.     self._append_version(recipeId, "rollback")
  128.     return True
  129. # ------------------------------------------------------------------
  130. # helper used by levels 2-4
  131. # ------------------------------------------------------------------
  132. def _append_version(self, recipeId, editor):
  133.     r = self.recipes[recipeId]
  134.     entries = self.history.setdefault(recipeId, [])
  135.     next_version = (entries[-1]["version"] if entries else 0) + 1
  136.     entries.append({
  137.         "version": next_version,
  138.         "name": r["name"],
  139.         "ingredients": list(r["ingredients"]),  # snapshot copies
  140.         "steps": list(r["steps"]),
  141.         "editor": editor,
  142.     })
复制代码

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

本版积分规则

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