Amazon Interview Question for SDE-2s


Country: India




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

1. Generate the Prefix Sum Array starting A1, A1 + A2, A1 + A2 + A3......
vector<int> prefix_sum;
2. auto itr = std::lower_bound(prefix_sum.begin(), prefix_sum.end(), l )
3. box number = (itr - prefix_sum.begin() + 1)
Complexity per query = O( log<base 2> N )

- mrsurajpoudel.wordpress.com July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Step 1 : Generate prefix sum array
Step 2: Apply Binary Search(we have to modified binary search little bit) to find required answer.

Following is the java code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    
    public static int bbSearch(int[] a, int l, int r, int k) {
        
        if (l <= r) {
            
            int mid = (l + r +1)/2;
            
            if(l==r && k <= a[mid]) {
                
                return mid;
            }
        
            if( mid >= 1 && k > a[mid-1] && k <= a[mid])
                return mid;
        
            if(a[mid] > k)
                return bbSearch(a, l, mid-1, k);
            if(a[mid] < k) 
                return bbSearch(a, mid+1, r, k);
        }
        return -1;    
    }

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		int[] pa = new int[n];
		for(int i = 0; i<n; i++) {
			a[i] = in.nextInt();
			if(i == 0)
				pa[i] = a[i];
                        else	
				pa[i] = pa[i-1] + a[i];
		}
        
        int q = in.nextInt();
        for(int i =0; i < q; i++) {
            int idx = in.nextInt();
            int ans = bbSearch(pa, 0, n-1, idx);
            System.out.println(ans + 1);
        }
		

	}
}

- Anonymous July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Step 1 : Create prefix sum array
Step 2: apply binary search to find reqired box

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    
    public static int bbSearch(int[] a, int l, int r, int k) {
        
        if (l <= r) {
            
            int mid = (l + r +1)/2;
            
            if(l==r && k <= a[mid]) {
                
                return mid;
            }
        
            if( mid >= 1 && k > a[mid-1] && k <= a[mid])
                return mid;
        
            if(a[mid] > k)
                return bbSearch(a, l, mid-1, k);
            if(a[mid] < k) 
                return bbSearch(a, mid+1, r, k);
        }
        return -1;    
    }

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		int[] pa = new int[n];
		for(int i = 0; i<n; i++) {
			a[i] = in.nextInt();
			if(i == 0)
				pa[i] = a[i];
                        else	
				pa[i] = pa[i-1] + a[i];
		}
        
        int q = in.nextInt();
        for(int i =0; i < q; i++) {
            int idx = in.nextInt();
            int ans = bbSearch(pa, 0, n-1, idx);
            System.out.println(ans + 1);
        }
		

	}
}

- Anonymous July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Step 1 : Create prefix sum array
Step 2: apply binary search to find reqired box

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    
    public static int bbSearch(int[] a, int l, int r, int k) {
        
        if (l <= r) {
            
            int mid = (l + r +1)/2;
            
            if(l==r && k <= a[mid]) {
                
                return mid;
            }
        
            if( mid >= 1 && k > a[mid-1] && k <= a[mid])
                return mid;
        
            if(a[mid] > k)
                return bbSearch(a, l, mid-1, k);
            if(a[mid] < k) 
                return bbSearch(a, mid+1, r, k);
        }
        return -1;    
    }

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		int[] pa = new int[n];
		for(int i = 0; i<n; i++) {
			a[i] = in.nextInt();
			if(i == 0)
				pa[i] = a[i];
                        else	
				pa[i] = pa[i-1] + a[i];
		}
        
        int q = in.nextInt();
        for(int i =0; i < q; i++) {
            int idx = in.nextInt();
            int ans = bbSearch(pa, 0, n-1, idx);
            System.out.println(ans + 1);
        }
		

	}
}

- rahul dhamu July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Step 1 : Create prefix sum array
Step 2: apply binary search to find reqired box

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    
    public static int bbSearch(int[] a, int l, int r, int k) {
        
        if (l <= r) {
            
            int mid = (l + r +1)/2;
            
            if(l==r && k <= a[mid]) {
                
                return mid;
            }
        
            if( mid >= 1 && k > a[mid-1] && k <= a[mid])
                return mid;
        
            if(a[mid] > k)
                return bbSearch(a, l, mid-1, k);
            if(a[mid] < k) 
                return bbSearch(a, mid+1, r, k);
        }
        return -1;    
    }

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		int[] pa = new int[n];
		for(int i = 0; i<n; i++) {
			a[i] = in.nextInt();
			if(i == 0)
				pa[i] = a[i];
                        else	
				pa[i] = pa[i-1] + a[i];
		}
        
        int q = in.nextInt();
        for(int i =0; i < q; i++) {
            int idx = in.nextInt();
            int ans = bbSearch(pa, 0, n-1, idx);
            System.out.println(ans + 1);
        }
		

	}
}

- dhamu.31954 July 15, 2016 | 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