注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
做题环境
- 面试官会发hackerrank链接。全程在hackerrank中进行。
- 面试官会提供test case,不需要自己写。测试要求是根据提供input打印结果,和期望结果对比即可。
- 只要思路清晰并通过提供的测试,即可进入下一环节。
题目描述
经典的Shipping Cost。整体分为三部分:
- 基础单价计算
- multi-tier运费结构
- 引入fixed与incremental类型混合运费结构
下面做思路总结与考点分析。有用请加大米。
Part 1: 基础版本
要求: 给定order和shipping_cost,计算total shipping cost。
例如:
order_us = {
"country": "US",
"items": [
{"product": "mouse", "quantity": 20},
{"product": "laptop", "quantity": 5}
]
}
order_ca = {
"country": "CA",
"items": [
{"product": "mouse", "quantity": 20},
{"product": "laptop", "quantity": 5}
]
}
shipping_cost = {
"US": [
{"product": "mouse", "cost": 550},
{"product": "laptop", "cost": 1000}
],
"CA": [
{"product": "mouse", "cost": 750},
{"product": "laptop", "cost": ;: 2, "cost": 1100},
{"type": "incremental", "minQuantity": 3, "maxQuantity": None, "cost": 1000}
]
}
]
}输出:
calculate_shipping_cost(order_us, shipping_cost) == 14700
calculate_shipping_cost(order_ca, shipping_cost) == 19100题目分析:
- 基本同上。注意:与可以交替出现(例如
- fixed -> incremental -> fixed -> incremental
复制代码 )。 - 解法与 Part 2 基本一致,唯一区别是当 type == "fixed" 时,不乘数量,直接加上固定值。
- edge cases基本同上。
- follow-up:如果要将该代码推入 production,应如何提升 code readability?
总结
- Stripe 的 coding 题不注重算法复杂度或最优解法,更看重思路清晰、结果正确、实现迅速。
- 建议使用 Python答题。语法简单、实现迅速。
- 注意沟通。尽可能clarify问题。同时上文提到的edge cases和follow up, 可以提前准备一下。
码字不易,有用请加大米。
|