查看: 982|回复: 8
收起左侧

[Leetcode] 【亚马逊】【OA】 Amazon 2021 New Grad Question Python 整理10道 答案 - 求大米

  |只看干货
本楼: 👍   100% (5)
 
 
0% (0)   👎
全局: 👍   100% (40)
 
 
0% (0)    👎

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

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

x
2021 New Grad Question List





DiskSpaceAnalysis
https://leetcode.com/playground/kykzCfMB

Highest Profit
https://leetcode.com/playground/k5LgFHbD

Minimum Cost Connection
https://leetcode.com/playground/Gj7Ecr4i

Pagination
https://leetcode.com/playground/M5GyHRYf

Smallest Negative Balance
https://leetcode.com/playground/8cVKZy43

Get Max Value
https://leetcode.com/playground/Gkya5wGs

Number of Clusters
https://leetcode.com/playground/GA4yyUBE

Item Association
https://leetcode.com/playground/ih5sm9Td

Closest Fulfillment Centers
https://leetcode.com/playground/TGJKrAXJ

Max of Min Altitudes
https://leetcode.com/playground/HAmSCYty



















评分

参与人数 5大米 +24 收起 理由
Chianti13 + 1 赞一个
代码要好好写呀 + 1 赞一个
JaceyJcjc + 1 赞一个
minyoung + 1 欢迎分享你知道的情况,会给更多积分奖励!
14417335 + 20 给你点个赞!

查看全部评分


上一篇:facebook fb meta vo 咨询
下一篇:有没有通过抄代码刷题的?
 楼主| coderoncruise 2021-12-5 07:09:50 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   100% (40)
 
 
0% (0)    👎
Amazon Fresh Deliveries

"""
Amazon Fresh Deliveries
Given allLocations list of co-ordinates (x,y) you have to find the X - closest locations from truck's location which is (0,0). Distance is calculated using formula (x^2 + y^2).

If the there is tie then choose the co-ordinate with least x value.

Sample Input :
allLocations : [ [1, 2] , [1, -1], [3, 4] ]
numOfDeliveries : 2

Sample Output :
[ [1, -1], [1 , 2] ]

Output list can be in any order.

Approach:
This question was basically K closest points to the origin (0,0) with added tie condition.
"""

from heapq import heappop, heappush

def AmazonFulfillment(allLocations,numOfDeliveries):
    heap = []
    for idx, location in enumerate( allLocations ):
        heappush(heap, (location[0]**2 + location[1]**2, location[0], idx))
        
    res = []
    while numOfDeliveries != 0:
        res.append(allLocations[ heappop(heap)[2] ])
        numOfDeliveries -=1
    return res

allLocations = [[1, 2] , [1, -1], [3, 4]]
numOfDeliveries = 2
ans = [[1, -1], [1 , 2]]
res = AmazonFulfillment(allLocations,numOfDeliveries)
print(res, res == ans)



回复

使用道具 举报

 楼主| coderoncruise 2021-12-5 13:06:04 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   100% (40)
 
 
0% (0)    👎
本帖最后由 coderoncruise 于 2021-12-5 00:12 编辑

Amazon近期高频题目 Leetcode
更多图片 小图 大图
组图打开中,请稍候......

Web capture_5-12-2021_0415_leetcode_Frequency_DESC.com_compressed.pdf

654.53 KB, 下载次数: 8, 下载积分: 大米 -1 颗

回复

使用道具 举报

wszxy 2021-12-5 14:37:56 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   83% (997)
 
 
16% (196)    👎
这是最近考的吗
回复

使用道具 举报

 楼主| coderoncruise 2021-12-5 14:44:56 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   100% (40)
 
 
0% (0)    👎
wszxy 发表于 2021-12-5 01:37
这是最近考的吗

2021 New Grad Questions可能是 2021 年春季
回复

使用道具 举报

 楼主| coderoncruise 2021-12-5 17:15:31 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   100% (40)
 
 
0% (0)    👎
求2大米

"""
Given an array of binary digits, 0 and 1, sort the array so that all zeros are at one end and all ones are at the other. Which end does not matter. To sort the array, swap any two adjacent elements. Determine the minimum number of swaps to sort the array.

Example
arr = [0, 1, 0, 1]
With 1 move, switching elements 1 and 2 yields [0, 0, 1, 1], a sorted array


Example
arr = [1, 1, 1, 1, 0, 1, 0, 1]
output = 3
[1, 1, 1, 1, 1, 0, 0, 1] => [1, 1, 1, 1, 1, 0, 1, 0] => [1, 1, 1, 1, 1, 1, 0, 0]

Function Description
Complete the function minMoves

minMoves has the following parameter(s):
int arr[n]: an array of binary digits

Returns
int: the minimum number of moves necessarry

Constraints
1 <= n <= 10^5
arr is in the set {0, 1}
Another Example
arr = [1, 1, 1, 1, 0, 0, 0 0]
We return 0 because we do not need to swap any elements

"""
def findMinSwaps(arr):
    index_zero = 0
    swap_zero = 0
    for i in range(len(arr)):
        if arr == 0:
            swap_zero += abs(i - index_zero)
            index_zero += 1
            

    index_one = 0
    swap_one = 0
    for i in range(len(arr)):
        if arr == 1:
            swap_one += abs(i - index_one)
            index_one += 1

    return min(swap_zero, swap_one)
  
# Driver code
arr = [0, 1, 0, 1]
ans = 1
res = findMinSwaps(arr)
print(res, res == ans)
回复

使用道具 举报

 楼主| coderoncruise 2021-12-5 17:17:12 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   100% (40)
 
 
0% (0)    👎
Count LRU Cache Misses

"""
Amazon Online Assessment (OA) - Count LRU Cache Misses
A virtual memory management system use least-recently-Used (LRU) cache. When a requested memory page is not in the cache and the cache is full, the page that was least-recently-used should be removed from the cache to make room for the requested page. If the cache is not full, the requested page can simply be added to the cache and considered the most-recently-used page in the cache. A given page should occur at most once in the cache.

Given the maximum size of the cache and a list of page requests,write an algorithm to calculate the number of cache misses. A cache miss occurs when a page is requested and isn't found in the cache.

int lruCacheMisses(int num, List<Integer> pages, int maxCacheSize)
Input
The input consists of three arguments:

num : an integer representing the number of pages

pages : a list of integers representing the pages requested

maxCacheSize : an integer representing the size of the cache

Output
Return an integer for the number of cache misses.

Note
The cache is initially empty and the list contains pages numbered in the range 1-50. A page at index "i" in the list is requested before a page at index "i+1".

Constraints
0 <= i < num

Examples
Example 1:
Input:
num = 6

pages = [1,2,1,3,1,2]

maxCacheSize = 2

Output: 4
Explanation:
  Cache state as requests come in ordered longest-time-in-cache to shortest-time-in-cache:

  1*

  1,2*

  2,1

  1,3*

  3,1

  1,2*

  Asterisk (*) represents a cache miss. Hence, the total number of a cache miss is `4`.

"""
回复

使用道具 举报

 楼主| coderoncruise 2021-12-5 17:18:47 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   100% (40)
 
 
0% (0)    👎
countBinarySubstrings

from collections import deque
class Solution:
    def countBinarySubstrings(self, s: str) -> int:
        queue = deque()

        res = 0

        for char in s:
            while queue and queue[-1] == char and queue[0] != char:
                queue.pop()

            if queue and queue[-1] != char:
                queue.pop()
                res += 1

            queue.appendleft(char)
        
        return res
s = Solution()
print(s.countBinarySubstrings([0,0,1,1,0,1]))
print(s.countBinarySubstrings("00110011"))
回复

使用道具 举报

astead 2021-12-12 23:49:28 来自APP | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   100% (3)
 
 
0% (0)    👎
谢谢楼主分享
回复

使用道具 举报

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

本版积分规则

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