Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort one of the arrays of length N. Iterate the other array of length M and do a binary search in the first array updating the global maximum. O(N*log(N) + M*log(N))

def find(F, B, T):
    ans = [0, 0, 0]
    F = sorted([x, i] for i,x in F)
    for idy,y in B:
        f = 0
        end = len(F)
        z = T - y
        while f != end:
            m = (f + end)/2
            if F[m][0] <= z:
                f = m+1
            else:
                end = m
        if f != 0 and y+F[f-1][0] > ans[0]:
            ans = [y+F[f-1][0], F[f-1][1], idy]
    return ans[1:]
        
print find([(1,3000),(2,5000),(3,4000),(4,10000)],
	 [(1,2000),(2,3000),(3,4000)], 11000)

- adr October 10, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you give java solution..

- john October 18, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be done using maximum contiguous sum subarray.
1. First add forward and backward for each location. like 1 forward + 2 backward for 1st index i.e. ith forward + (i+1)th backward for ith index cost.
2. Now apply maximum contiguous sum subarray to get the subarray <i,j>. Answer would be <i, j+1>.
Time Complexity: O(n) + O(n).
Space Complexity: O(n). (for storing the result. we can make it O(1), if we reuse the any of the forward and backward input array).

- ss October 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about the case when all pairs are valid?
forward-> [1,1000],[2,1000],[3,1000]
backward->[1,1000],[2,1000],[3,1000]
Reqd = 2000
Here all a pairs are possible - so how can you give in O(n)?

- Himanshu October 13, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More