Amazon Interview Question for Java Developers


Country: United States
Interview Type: Phone Interview




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

Construct a binary search tree and inset each item, return left and right nodes values as output of insertion

- Anonymous June 22, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static List<List<Integer>> insertInBag(List<Integer> nums) {
    TreeSet<Integer> bag = new TreeSet<>();

    List<List<Integer>> result = new ArrayList<>();

    for (Integer i : nums) {
      Integer smaller = bag.floor(i); // O(logn)
      Integer higher = bag.ceiling(i);// O(logn)

      ArrayList<Integer> subresult = new ArrayList<>();

      if (smaller != null) {
        subresult.add(smaller);
      } else {
        subresult.add(-1);
      }

      if (higher != null) {
        subresult.add(higher);
      } else {
        subresult.add(-1);
      }
      result.add(subresult);

      bag.add(i);

    }
    return result;
  }

- Olympus June 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Can also be implemented by a creating sorted single-linked-list.
Take two variable prev and next, just before inserting an element in the bag loop through the linked list and search for an element that is bigger than the element to be inserted and this way as soon as you find a node with value more than the value to be inserted in the bag print the two values and insert the value between the two nodes.
Worst Case Complexity - O(n)

- infinity June 30, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is an ideal case for an AVL tree implementation. Print the boundary values before rotation. Worst case complexity for insertion is O(logn).

(Surprised they asked this in an interview)

- llamatatsu June 22, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

when to put inside bag condition???

- shivam gupta June 22, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This question is from a Hackerearth coding context and not from Amazon interview

- Vivek June 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Construct a sorted DLL for every insertion and return the prev and next pointer everytime.
return -1 if any neighbor is NULL

- BHARAT SINGH HADA June 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void solve(int[] arr, int N){
        // Please write your code here
    	
    	List<Integer> temp = new ArrayList<Integer>();
    	int min =-1; int max = -1;
    	int minIndex=0; int maxIndex=0;
    	
    	for (int i = 0; i <N; i++) {
    		temp.add(arr[i]);
    		
    		if(temp.size()>1) {
    			Collections.sort(temp);
    			minIndex = temp.indexOf(arr[i])!=-1?temp.indexOf(arr[i])-1:-1;
    			maxIndex = temp.indexOf(arr[i])!=-1?temp.indexOf(arr[i])+1:-1;
    			
    			min = (minIndex!=-1 && minIndex>-1)?temp.get(minIndex):-1;
    			max = (maxIndex!=-1 && maxIndex<temp.size())?temp.get(maxIndex):-1;
    			
    		}
    		System.out.println(min + " " + max);
        }

}

- Lingaraj June 24, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void solve(int[] arr, int N){
        // Please write your code here
    	
    	List<Integer> temp = new ArrayList<Integer>();
    	int min =-1; int max = -1;
    	int minIndex=0; int maxIndex=0;
    	
    	for (int i = 0; i <N; i++) {
    		temp.add(arr[i]);
    		
    		if(temp.size()>1) {
    			Collections.sort(temp);
    			minIndex = temp.indexOf(arr[i])!=-1?temp.indexOf(arr[i])-1:-1;
    			maxIndex = temp.indexOf(arr[i])!=-1?temp.indexOf(arr[i])+1:-1;
    			
    			min = (minIndex!=-1 && minIndex>-1)?temp.get(minIndex):-1;
    			max = (maxIndex!=-1 && maxIndex<temp.size())?temp.get(maxIndex):-1;
    			
    		}
    		System.out.println(min + " " + max);
        }
    
    }

- Anonymous June 24, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void solve(int[] arr, int N){
        // Please write your code here
    	
    	List<Integer> temp = new ArrayList<Integer>();
    	int min =-1; int max = -1;
    	int minIndex=0; int maxIndex=0;
    	
    	for (int i = 0; i <N; i++) {
    		temp.add(arr[i]);
    		
    		if(temp.size()>1) {
    			Collections.sort(temp);
    			minIndex = temp.indexOf(arr[i])!=-1?temp.indexOf(arr[i])-1:-1;
    			maxIndex = temp.indexOf(arr[i])!=-1?temp.indexOf(arr[i])+1:-1;
    			
    			min = (minIndex!=-1 && minIndex>-1)?temp.get(minIndex):-1;
    			max = (maxIndex!=-1 && maxIndex<temp.size())?temp.get(maxIndex):-1;
    			
    		}
    		System.out.println(min + " " + max);
        }
    
    }

- lingarajpatil1991 June 24, 2019 | 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