Amazon India OA - what are my chances?
Anonymous User
754

Attended Amazon India OA, there were 2 questions for sde2.

  1. There are list of chapters where chapters[i] represents the number of pages in that chapter. We can read upto k consecutive chapters per day. Additionally, we can only read p pages per chapter a day.
    Find the minimum number of days required to complete reading all the chapters.

    This question seemed like greedy+sliding window. All that I could come up for the above question was a brute force approach with minor optimizations which led to few test cases timing out

    input #1
    chapters = [10,20,30,40]
    k = 2
    p = 10

    result: 6

    chapters: [5,1,4,3,2,7,1]
    k = 3
    p = 2

    result: 7

My brute force approach:

chapters = [10,20,30,40]
n = len(chapters)
k = 2
p = 10
movingIndex = 0
days = 0
while movingIndex < n :
    startIndex = movingIndex
    for index in range(movingIndex, movingIndex+k):
        if index < n and chapters[index]:
            chapters[index] = chapters[index] - min(p, chapters[index])
            if chapters[index] == 0 and chapters[startIndex] == 0:
                startIndex = index+1
    movingIndex = startIndex
    days += 1
print("Min days "+str(days)) 
  1. There are n nodes in amazon shipping centre, where each node should conect to the nearest hub, the last node is always a hub. There will be list of k queries provided, each of them having a list of additional nodes which could also act as hubs, we need to find the min cost of having this setup for each query.
    The cost is defined as the difference between node and the nearest hub. all the nodes are 1 indexed.
    Note: a node can connect to a hub whose index is equal or greater than that.

    This is a straightforward prefix sum question which I was able to complete it without any issues in O(n).

    nodes = [10,20,30,40,50]
    queries = [[2,4], [1,3]]

    result = [20, 20]

    explanation: for query 1,
    node 1 nearest hub 20, 20-10 = 10
    node 2 nearest hub is itself: 20-20 = 0
    node 3 nearest hub is 40, 40-30 = 10
    node 4, nearest hub is itself: 40-40 = 0
    node 5, nearest hub is itself: 50-50 = 0

    similarly for query 2

Can anyone provide any idea on how 1st question can be optimized?
Also what are my chances to move to the next rounds? Is it okay to fail few test cases?

Comments (9)