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

2014-04-18 Amazon on-site

全局:

2014(4-6月) 码农类General 硕士 全职@amazon - 网上海投 - Onsite  | | Other |

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

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

x
除了第四轮烙印要求必须使用Java外,楼主都是用Python写的。

一轮:
1. 判断两个linked list 是否有交叉,有则返回True,没有返回False。follow-up: 有环怎么办。
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
a,楼主最近两年来基本都在写Python,Java在白板上写实在困难。恨死烙印)

楼主会在楼下发自己的答案。希望大家批评指正。

评分

参与人数 3大米 +16 收起 理由
bryanjhy + 3 给你点个赞!
kang1415926 + 10 祝楼主好运!!!
Soviet + 3 谢谢!!好细心!祝好运

查看全部评分


上一篇:Bloomberg Onsite - 2014 Software Developer(Entry Level)
下一篇:G家电面,悲剧了
推荐
ki87uj 2014-12-3 19:34:41 | 只看该作者
全局:
我感觉LZ的第3题这么做是对的。。。我们是不是有一个assumption,看不到除了两头以外里面的数字?所以取当前最大。另外有没有一个assumption没有重复数字?
回复

使用道具 举报

推荐
april.lsy 2014-12-4 05:12:08 | 只看该作者
全局:
发现亚麻收了好多nyu的
回复

使用道具 举报

推荐
 楼主| johnnywsd 2014-11-25 05:14:44 | 只看该作者
全局:
lz就是个jb
回复

使用道具 举报

🔗
 楼主| johnnywsd 2014-4-21 10:34:36 | 只看该作者
全局:
一轮,1
  1. class ListNode(object):
  2.     def __init__(self, val=None):
  3.         self.val = val
  4.         self.next = None


  5. class Solution(object):

  6.     def __init__(self):
  7.         self._visited = dict()

  8.     def is_interset(self, A, B):
  9.         p1 = A
  10.         p2 = B
  11.         while p1:
  12.             if p1 not in self._visited:
  13.                 self._visited[p1] = 1
  14.             else:
  15.                 break
  16.             p1 = p1.next

  17.         while p2:
  18.             tmp = self._visited.get(p2, '0')
  19.             if tmp == 1:
  20.                 return True
  21.             elif tmp == 2:
  22.                 break
  23.             else:
  24.                 # tmp == 0
  25.                 self._visited[p2] = 2
  26.             p2 = p2.next
  27.         return False
复制代码
回复

使用道具 举报

🔗
 楼主| johnnywsd 2014-4-21 10:36:37 | 只看该作者
全局:
一轮,2
  1. # Amazon need to keep a record about the best seller items.
  2. # We need this class
  3. # class BestSellerRanks{
  4. #     public List<Item> getTop50Ranks(); // get top 50 ranked Items
  5. #     public getItemRank(String id) // get the rank of the item whoes id is id.
  6. #     public sellItem(String id, int num) // sell more num items whose id is id
  7. # }
  8. # We need to make these three method run as fast as possible.
  9. # FYI the Item class may like this
  10. # class Item{
  11. #     String id; // Unique id for each item
  12. #     String name;
  13. #     int val; // number of this item sold.
  14. #     .....
  15. # }


  16. class Item(object):
  17.     def __init__(self, id=None, val=None):
  18.         self.id = id
  19.         self.val = val


  20. class BestSellerRanks(object):
  21.     def __init__(self, items):
  22.         """
  23.         Time complecity O(nlogn)
  24.         """
  25.         self._items = sorted(items, key=lambda x: x.val, reverse=True)
  26.         self._dict_id_to_idx = {}
  27.         for idx, item in enumerate(self._items):
  28.             self._dict_id_to_idx[item.id] = idx

  29.     def get_top_50_Ranks(self):
  30.         """
  31.         Time complecity O(1)
  32.         """
  33.         return self._items[:50]

  34.     def sell_item(self, id, num):
  35.         """
  36.         Time complecity O(n)
  37.         """
  38.         idx = self._dict_id_to_idx[id]
  39.         self._items[idx].val += num
  40.         for i in range(idx, 0, -1):
  41.             if self._items[i].val > self._items[i - 1].val:
  42.                 self._dict_id_to_idx[self._items[i].id] -= 1
  43.                 self._dict_id_to_idx[self._items[i - 1].id] -= 1
  44.                 self._items[i], self._items[i - 1] = \
  45.                     self._items[i - 1], self._items[i]
  46.             else:
  47.                 break

  48.         return self._items[:50]
复制代码
回复

使用道具 举报

🔗
 楼主| johnnywsd 2014-4-21 10:38:56 | 只看该作者
全局:
二轮,1

以下三个Solutions是逐步优化的。

Solution1
  1. # You and your friend are playing a game
  2. # There is an list of numbers, you pick up one from either
  3. # the front or the rare. Then your friend pick up one
  4. # Question: What the maxmium sum of numbers you can get.


  5. class Solution(object):
  6.     """
  7.     DP, without cach. O(2^n)
  8.     """
  9.     def get_max_sum(self, A):
  10.         return self._get_max_sum_aux(A, 0, len(A) - 1)

  11.     def _get_max_sum_aux(self, A, left, right):
  12.         if left > right:
  13.             return 0
  14.         elif left == right:
  15.             return A[left]
  16.         else:
  17.             tmp_sum = sum(A[left:right + 1])
  18.             chose_left = tmp_sum - self._get_max_sum_aux(A, left + 1, right)
  19.             chose_right = tmp_sum - self._get_max_sum_aux(A, left, right - 1)
  20.             res = max(chose_left, chose_right)
  21.             return res
复制代码
Solution2
  1. # You and your friend are playing a game
  2. # There is an list of numbers, you pick up one from either
  3. # the front or the rare. Then your friend pick up one
  4. # Question: What the maxmium sum of numbers you can get.


  5. class Solution(object):
  6.     """
  7.     DP, cache. O(n^2). because there are n * n keys in self._cache
  8.     """
  9.     def __init__(self):
  10.         self._cache = {}

  11.     def get_max_sum(self, A):
  12.         return self._get_max_sum_aux(A, 0, len(A) - 1)

  13.     def _get_max_sum_aux(self, A, left, right):
  14.         if left > right:
  15.             return 0
  16.         elif left == right:
  17.             return A[left]
  18.         else:
  19.             if (left, right) in self._cache:
  20.                 return self._cache[(left, right)]
  21.             tmp_sum = sum(A[left:right + 1])
  22.             chose_left = tmp_sum - self._get_max_sum_aux(A, left + 1, right)
  23.             chose_right = tmp_sum - self._get_max_sum_aux(A, left, right - 1)
  24.             res = max(chose_left, chose_right)
  25.             self._cache[(left, right)] = res
  26.             return res
复制代码
Solution3
  1. # You and your friend are playing a game
  2. # There is an list of numbers, you pick up one from either
  3. # the front or the rare. Then your friend pick up one
  4. # Question: What the maxmium sum of numbers you can get.


  5. class Solution(object):
  6.     """
  7.     DP, cache. O(n^2). because there are n * n keys in self._cache
  8.     decrease the time of cacluate the sume in the recursions
  9.     """
  10.     def __init__(self):
  11.         self._cache = {}

  12.     def get_max_sum(self, A):
  13.         curr_sum = sum(A)
  14.         return self._get_max_sum_aux(A, 0, len(A) - 1, curr_sum)

  15.     def _get_max_sum_aux(self, A, left, right, curr_sum):
  16.         if left > right:
  17.             return 0
  18.         elif left == right:
  19.             return A[left]
  20.         else:
  21.             if (left, right) in self._cache:
  22.                 return self._cache[(left, right)]
  23.             tmp_sum = curr_sum
  24.             chose_left = tmp_sum - self._get_max_sum_aux(A, left + 1, right,
  25.                                                          curr_sum - A[left])
  26.             chose_right = tmp_sum - self._get_max_sum_aux(A, left, right - 1,
  27.                                                           curr_sum - A[right])
  28.             res = max(chose_left, chose_right)
  29.             self._cache[(left, right)] = res
  30.             return res
复制代码
回复

使用道具 举报

🔗
 楼主| johnnywsd 2014-4-21 10:52:12 | 只看该作者
全局:
三轮,1

Solution1
  1. class Solution:
  2.     # @param A, a list of integers
  3.     # @return an integer
  4.     def maxSubArray(self, A):
  5.         num = len(A)
  6.         f = [None] * num
  7.         if not A:
  8.             return 0
  9.         if num == 1:
  10.             return A[0]
  11.         f[0] = A[0]
  12.         mymax = A[0]
  13.         #We should ignore the sum of the
  14.         #previous n-1 elements if nth element is greater than the sum.
  15.         for i in xrange(1, num):
  16.             f[i] = max(A[i], f[i-1] + A[i])
  17.         
  18.         return max(f)
复制代码

Solution2
  1. public class Solution {
  2.     public int maxSubArray(int[] A) {
  3.         if (A == null){
  4.             return 0;
  5.         }
  6.         int curr_max = A[0];
  7.         int max = A[0];
  8.         for(int i=1; i<A.length; i++){
  9.             curr_max = Math.max(curr_max + A[i], A[i]);
  10.             if (curr_max > max){
  11.                 max = curr_max;
  12.             }
  13.         }
  14.         return max;
  15.     }
  16. }
复制代码
回复

使用道具 举报

🔗
 楼主| johnnywsd 2014-4-21 10:53:19 | 只看该作者
全局:
四轮,1
烙印要求使用Java,楼主Java好久没使用过了,各位同学给补充下答案吧。
回复

使用道具 举报

🔗
discoveryi 2014-4-21 11:26:52 | 只看该作者
全局:
楼主maxmium subarray用DP做不至于用了45分钟吧?  能到面试官有什么特别要求吗?

补充内容 (2014-4-21 11:27):
难道面试官要求DP和Divide and conquer两个solution?
回复

使用道具 举报

🔗
 楼主| johnnywsd 2014-4-21 11:31:21 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
Soviet 2014-4-21 11:36:10 | 只看该作者
全局:
请问第四个题是求Unique Paths 的数量呢?还是每个格子上有些值求最小或最大path value sum?
回复

使用道具 举报

🔗
 楼主| johnnywsd 2014-4-21 11:48:24 | 只看该作者
全局:
Soviet 发表于 2014-4-21 11:36
请问第四个题是求Unique Paths 的数量呢?还是每个格子上有些值求最小或最大path value sum?

任意条就可以了。
回复

使用道具 举报

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

本版积分规则

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