Amazon New Grad 2021 OA2 Coding and Workstyles Assessments

** 100 min
70 min 2 coding problems
10-20min workstyle assessments**

Coding

题目集合!!!
【超全超详细】Amazon new grad OA2 新鲜面经&资料大集合
Amazon OA2
亚马逊】【OA】整理60道
新鲜亚麻 NG OA2
题库
Amazon Online Assessment Questions 2021
Amazon OA question complilation


Greatest Common Divisor of n Numbers

Input:
5,
[2, 4, 6, 8, 10]
Output:
2

Fill The Truck

Amazon Online Assessment (OA) - Fill The Truck

from typing import List

def fill_the_truck(num: int, boxes: List[int], unit_size: int, units_per_box: List[int], truck_size: int) -> int:
    # WRITE YOUR BRILLIANT CODE HERE
    maxUnits = 0
    space = truck_size
    for i, units in enumerate(units_per_box):
        if space < boxes[i]:
            return maxUnits + space * units
        
        maxUnits += boxes[i] * units
        space -= boxes[i]
    return maxUnits
        

if __name__ == '__main__':
    num = int(input())
    boxes = [int(x) for x in input().split()]
    unit_size = int(input())
    units_per_box = [int(x) for x in input().split()]
    truck_size = int(input())
    res = fill_the_truck(num, boxes, unit_size, units_per_box, truck_size)
    print(res)

Similar LeetCode 1710. Maximum Units on a Truck

class Solution:
    def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
        boxTypes.sort(key = lambda x:x[1], reverse = True)  # sort list based on second 
                                        # item (numberOfUnitsPerBox) in decreasing
        maxUnits = 0
        for box in boxTypes:
            if truckSize < box[0]:  # truck不够装了,剩下多少全装满,然后关门
                return maxUnits + truckSize * box[1]
            
            maxUnits += box[0] *box[1]
            truckSize -= box[0] # 更新truck剩余空间
        return maxUnits

Highest Profit

Amazon | OA 2020 | Highest Profit
Amazon Online Assessment (OA) - Find The Highest Profit
Input
numSuppliers: an integer representing the number of suppliers
inventory: a list of long integers representing the value of the item at a given supplier
order: a long integer representing the number of items to be ordered.
Output
Return a long integer representing the highest profit that can be generated for the given product.

from typing import List
from collections import Counter

def find_profit(num_suppliers: int, inventory: List[int], order: int) -> int:
    # WRITE YOUR BRILLIANT CODE HERE
    d = Counter()
    for i in range(num_suppliers):
        d[inventory[i]] += 1	# {不同的利润的商品(来自各个suplliers):有多少个}
    
    cur = max(d)	# 哪个利润最高
    profit = 0
    while order > 0 and cur > 0:
        freq = d[cur]	# 这个利润有几个商品

        if order >= freq:
            order -= freq
            profit += cur * freq
        elif order < freq:
            profit += order * cur
            break
            
        cur -= 1	# 这个价位卖光啦
        d[cur] += freq	# 之前买的利润一起下降1
    
    return profit

if __name__ == '__main__':
    num_suppliers = int(input())
    inventory = [int(x) for x in input().split()]
    order = int(input())
    res = find_profit(num_suppliers, inventory, order)
    print(res)

1629. Slowest Key

class Solution:
    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:
        l = len(releaseTimes)
        res = keysPressed[0]
        maxTime = releaseTimes[0]
        if l == 1:
            return res
        if l == 0:
            return None

        for i in range(1, l):
                time = releaseTimes[i] - releaseTimes[i - 1]
                if time == maxTime:
                    res = max(res, keysPressed[i])
                if time > maxTime:
                    res = keysPressed[i]
                    maxTime = time
                    
        return res

1. Two Sum

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        memo = []
        for i, num in enumerate(nums):
            if target - num in memo:
                return [i,nums.index(target - num)]
            
            memo.append(num)

874. Walking Robot Simulation

class Solution:
    def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
        obs = set(map(tuple,obstacles)) # 提升in的查找速度(键值对应)
        dx = [0, 1, 0, -1]  # dx和dy组合起来代表
        dy = [1, 0, -1, 0]  # [north, east, south, west]
                            # 向左移动:左转(逆时针);向右移动:右转
        di = 0
        x = y = 0
        ans = 0
        
        for c in commands:
            if c == -2:
                di = (di - 1) % 4
            elif c == -1:
                di = (di + 1) % 4
            else:
                for i in range(c):
                    if (x + dx[di], y + dy[di]) not in obs:
                        x += dx[di]
                        y += dy[di]
                        ans = max(ans, x*x + y*y)
        
        return ans

countPrimeStrings

Count of ways to split a given number into prime segments
Input: str = “3175”
Output: 3
Explanation:
There are 3 ways to split this string into prime numbers which are (31, 7, 5), (3, 17, 5), (317, 5).

MOD = 1000000007

# Function to check whether a number is a prime number or not
def isPrime(number):
	num = int(number)
	i = 2
	while i * i <= num:
		if ((num % i) == 0):
			return False
		i += 1
		
	if num > 1:
		return True
	else:	# i is not a prime number
		return False

# Function to find the count of ways to split string into prime numbers
def countPrimeStrings(number, i):
	# 1 based indexing
	if (i == 0):
		return 1
	cnt = 0
	
	# Consider every suffix up to 6 digits
	for j in range(1, 7):
		# Number should not have a leading zero and it should be a prime number
		if (i - j >= 0 and number[i - j] != '0' and isPrime(number[i - j : i])):
			cnt += countPrimeStrings(number, i - j)
			cnt %= MOD
		
	# Return the final result
	return cnt

# Driver code
if __name__ == "__main__":

	s1 = "3175"
	l = len(s1)
	print (countPrimeStrings(s1, l))

# This code is contributed by Chitranayal

k length distinct char substring

Count of substrings of length K with exactly K distinct characters
Count number of substrings with exactly k distinct characters

def countkDist(str1, k): 
	n = len(str1) 
	res = 0

	# To store count of characters from 'a' to 'z' 
	cnt = [0] * 27

	# Consider all substrings beginning with str[i] 
	for i in range(0, n): 
		dist_count = 0
		cnt = [0] * 27	# Initializing array with 0 

		for j in range(i, n): # Consider all substrings between str[i..j]
			# If this is a new character for this substring, increment dist_count. 
			if(cnt[ord(str1[j]) - 97] == 0): 
				dist_count += 1

			# Increment count of current character 
			cnt[ord(str1[j]) - 97] += 1

			# If distinct character count becomes k, 
			# then increment result. 
			if(dist_count == k): 
				res += 1
			if(dist_count > k): 
				break

	return res	 

# Driver Code 
if __name__ == "__main__": 
	str1 = "abcbaa"
	k = 3
	print("Total substrings with exactly", k, 
		"distinct characters : ", end = "") 
	print(countkDist(str1, k)) 

# This code is contributed by 
# Sairahul Jella 

Amazon | OA 2019 | Substrings with exactly K distinct chars

973. K Closest Points to Origin

class Solution:
    # max-heap
    def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
        h = []
        heapq.heapify(h)
        
        # 将所有点的距离放入最小堆
        for i, point in enumerate(points):
            distance = point[0] ** 2 + point[1] ** 2
            heapq.heappush(h, (distance, i))
        
        ans = []
        # 弹出最小堆的K个元素
        for i in range(K):
            p = heapq.heappop(h)
            ans.append(points[p[1]])
        
        return ans

Employment Tree & Power Grid

Amazon Online Assessment 2 - SDE 1( New Graduate ) 2021 ( Coding - 2 Questions ) - With Solutions

Fetch Items To Display

Amazon Online Assessment (OA) - Fetch Items To Display

max average subtree

Amazon | OA 2019 | Subtree with Maximum Average

throttling gateway

Throttling Gateway Hackerrank/Citadel

Amazon Online Assessment (OA) - Throttling Gateway 要买会员

937. Reorder Data in Log Files

Input: logs = [“a1 9 2 3 1”,“g1 act car”,“zo4 4 7”,“ab1 off key dog”,“a8 act zoo”]
Output: [“g1 act car”,“a8 act zoo”,“ab1 off key dog”,“a1 9 2 3 1”,“zo4 4 7”]

Optimal Utilization

Amazon | OA 2019 | Optimal Utilization
Given 2 lists a and b. Each element is a pair of integers where the first integer represents the unique id and the second integer represents a value. Your task is to find an element from a and an element form b such that the sum of their values is less or equal to target and as close to target as possible. Return a list of ids of selected elements. If no pair is possible, return an empty list.

572. Subtree of Another Tree

Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’s descendants. The tree s could also be considered as a subtree of itself.

storage optimization

Amazon OA | storage optimization

Workstyles Assessments

Amazon Leadership Principles

Question:
I believe planning ahead would lead to a better outcome once in a while people find errors in my work
<-----o------------------------------------o---------------------------------------o----------------------------------o-----------------------------------o---->
*most like me *more like me *default *more like me *most like me

60道workstyle assessment

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值