Amazon OnCampus interview SDE-1
Company: Amazon
Role: SDE I
Interview Stage: Onsite
Location: unknown
Year: unknown
Original Post Content:
Kindly Ignore mistakes. I was a bit nervous and in hurry
Question 1 : =
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed,
the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected
and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house,
return the maximum amount of money you can rob tonight without alerting the police.
Example :
Input: nums = [1,2,3,1]
Output: 4
# nums denote money
# adjacent house robbing is not allowed
nums = [1,2,3,1]
def rec(idx, nums):
if idx>=len(nums):
return 0
rob_current = nums[idx] + rec(idx+2, nums)
skip_current = rec(idx+1, nums)
return max(rob_current, skip_current)
rec(0, nums)
tc = O(2^n)
sc = O(2n)
nums = [1,2,3,1]
n = len(nums)
dp = [0]*(n+1)
dp[0] = 0
def rec(idx, nums):
if idx<0:
return 0
rob_current = nums[idx] + (dp[idx-2] if idx-2 >= 0 else 0)
skip_current = (dp[idx-1] if idx-1 >= 0 else 0)
dp[idx] = max(rob_current, skip_current)
return dp[idx]
rec(n-1, nums)
print(dp[-1])
dp = nums[:] # 1 2 3 1
dp[1] = max(dp[0], dp[1])
for i in range(2, len(nums)):
dp[i] = max(nums[i]+dp[i-2] , dp[i-1])
print(dp[-1])
tc = O(n), sc = O(n)
# 1 2 3 1
if len(nums)<2:
print(max(nums))
i1, i2 = max(nums[0], nums[1]), nums[0]
for i in range(2, len(nums)):
ans = max(nums[i]+i2 , i1)
i2 = i1
i1 = ans
print(i1)
tc = O(n), sc = O(1)
---------------------------------------------
Question 2 :
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the earliest time after which all the oranges are rotten. A rotten orange at index [i,j]
can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 - 0 1 2
1 - 0 1 2
2 - 2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
0 1 2
0 - 0 1 2
1 - 0 1 2
2 - 2 1 1
q = [(0,0)]
encouter: 2
move 4 directions -> if 1 -> q.append((x,y))
row, col = len(grid), len(grid[0])
q = []
oranges = 0
# find fresh oranges and fill q
for i in range(row):
for j in range(col):
if grid[i][j] == 1:
oranges += 1
elif grid[i][j] == 2:
q.append((i,j, 0))
time = 1
while q and oranges>0:
r_curr, c_curr, t = q.pop(0)
for r_delta,c_delta in [(1,0), (-1,0), (0,1), (0,-1)]:
nr = r_curr+r_delta
nc = c_curr+c_delta
if 0<=nr<row and 0<=nc<col and grid[nr][nc]==1:
grid[nr][nc] = 2
oranges -= 1
time = max(time, t+1)
q.append((nr, nc, t+1))
print(time if oranges==0 else -1)
tc = O(n*m)
sc = O(n*m)