This is on behalf of a friend. He went through an Amazon OA for SDE Internship-3m. Applied in November 2021 (no referral) and got the OA in February 2022. This OA was through the HackerRank platform. The solutions below were my solutions and not the one my friend submitted.
These were the questions asked:
Question 1:
The actual question was quite long, I have shorted it to the below question.
We are given 2 strings, S and T.
We need to find the maximum number of subsets in S that can be rearranged to form string T. Return -1 if there is no match.
So basically it was an Anagram problem. It is very similar to this LeetCode question - https://leetcode.com/problems/find-all-anagrams-in-a-string/ . The only different is that we need to get the count of the anagrams rather than the index of the starting element.
Example:
S = "abcdcba"
T = "abc"
Answer = 2
S = "sononososonoons"
T = "nos"
Answer = 4
Solution :
def countMaximumOperations(self,s,t):
if len(t) > len(s):
return -1
tCount = {}
sCount = {}
for i in range(len(t)):
tCount[t[i]] = 1 + tCount.get(t[i],0)
sCount[s[i]] = 1 + sCount.get(s[i],0)
res = 1 if tCount == sCount else 0
l = 0
for r in range(len(t),len(s)):
sCount[s[r]] = 1 + sCount.get(s[r],0)
sCount[s[l]] -= 1
if sCount[s[l]] == 0:
sCount.pop(s[l])
l+=1
if sCount == tCount:
res+=1
return res if res > 0 else -1Question 2:
This question was same as this post's question 1 :
https://leetcode.com/discuss/interview-question/1733741/Amazon-OA-or-SDE-Intern
(borrowed the examples from above post)
Example:
Input = boxes - [2, 2, 3, 3, 2, 4, 4, 4, 4, 4]
Answer = 4
Explanation: 3 boxes of weight 2 in 1st round, 2 boxes of weight 3 in 2nd round, 3 boxes of wt 4 in 3rd and 2 boxes of wt 4 in 4th round.
Input = boxes - [2, 3, 3]
Answer = -1
Explanation: There is only one box with weight 2 and we can only take either 2 or 3 boxes in one round not lesser.
Solution:
def boxRemoval(self, arr):
dp = defaultdict(int)
cnt = 0
for vals in arr:
dp[vals] +=1
for keys in dp.keys():
value = dp.get(keys)
if value < 2:
return -1
if value % 3 == 0:
print(str((value//3))+" 3s") #Use this to get the box removal info
cnt+= value//3
elif value % 3 == 1:
print(str((value//3)-1)+" 3s and 2 2s") #Use this to get the box removal info
cnt+= value//3+1
elif value % 3 == 2:
print(str((value//3))+" 3s and 1 2s") #Use this to get the box removal info
cnt+= value//3+1
return cntHope this helps. Upvote if you found this helpful.