Amazon Interview Experience SDE (On Campus 2019)
Qualification : currently pursuing B.E. Electrical and Electronics Engineering.
Round 1: After general introduction, the interviewer asked me if I was good with arrays. I said ‘Yes’ and then he asked me two problems on arrays/vectors and then one problem on BST.
Problem 1 : Given two arrays(A & B), can you tell if its possible to swap an element from A with an element from B and make the sum of both arrays equal ?
Answer : Sort the arrays.Calculate sum of both arrays and take out difference of the sum. If difference is odd – it is not possible to do the required exchange(return 0), else if it is even – (lets say difference is 8) divide it by 2 (= 4 ) and then traverse both the arrays(this can be done by different methods – I used binary search with some modification) and find a pair which has difference = 4. If found – it is possible(return 1), else return 0.
Answer : This is how I told explained him the solution. After a minute of thinking I said, “first we will calculate initial sum of both the arrays, and then take out their difference. If the difference was odd – it means it is not possible to make the sum same by swapping two elements. Interviewer asked me why the difference needs to be even? I told him that any transaction resulting from swapping two elements will always result in even increments or deductions in the sum value. He was convinced and told me to continue. “So if difference is even we will divide it by two and then take two pointers to traverse both arrays in parallel. So yeah it will help if we also sort the arrays so then we can use binary search. If a pair is found we return 1 else we return 0.
He told me to write the code for the same and I did while simultaneously explaining some assumptions I was taking to avoid corner cases for now and conditions. (like both vectors have elements in them, and sum of A initially is greater than B).
He read the code and asked some doubts which I explained nicely and he was convinced.
Problem 2 : https://www.geeksforgeeks.org/coin-game-of-two-corners-greedy-approach/
I told me the exact solution approach and he countered with some examples but couldn’t find a flaw in that. He asked what if the number of coins initially are odd. I told him that the game would be unfair then, but he said lets make it unfair, now how will you win ? I told him that if the sum of odd elements now is greater than even ones then only I can win otherwise its not possible for me to win in this situation. He countered with some examples but was convinced by the end.
Problem 3 : Given a BST replace every node with sum of all nodes which are greater than that node. replace the maximum value node with 0.
Answer : Pretty easy recursive approach. I struggled with two flawed solutions before presenting the third correct one.
duration : 1-1.5 hrs.
result : selected for round 2.
Round 2: Friendly and encouraging interviewer. He asked me two problems, one on Binary Tree and another on vector and maths.
Problem 1 : Given a Binary tree, print the maximum depth of a left node.( the node needs to be a left child ) (if the node is right child of the left child of the root node then it wont count as a left node)
It took me some examples to realise what the question was. He himself told me to clarify the ques by asking any example so I took different scenarios and asked him what the answer will be in those cases. He told me what do you think the answer should be ? I told him the answers to trial cases according to my understanding of the problem and finally concluded I had understood the problem correctly.
Answer : It is very simple recursive approach. Just keep track of maxDepth of all the left nodes. To check if the node is left or right child just pass a integer argument while calling to recursion ( say 1 for left and 2 for right).
function will look something like this (C++)
Interviewer was convinced with my approach and quickly moved to second question.
Problem 2 : Given a list of songs and a random number generator that generates any random number from 1-INT_MAX, return a shuffled playlist.(in place)
My Approach : list of songs = vector A;
int size = A.size();
int j=0; // iterator over the array
Interviewer was convinced by my approach but he had some different approach in mind and kept questioning the stability of this solution and told me to do a dry run over it once. It was correct so he didn’t question any further.
duration : 1 hr approx.
result : selected for round 3.
Round 3: This was the most difficult round and the interviewer was tough and not that friendly. He asked me two questions both of which I wasn’t able to answer but struggled a lot and went with far my approaches.
Problem 1 : Given a two numbers n and k. Can ‘n’ be broken down using ‘k’ powers of 2 ? If yes, return a possible set.
Example : n = 9, k=3 – answer : YES, 4+4+1 = 9;
No, it cannot be done by binary conversion of the number and then combining the powers of two there (which I tried instantly). I wasn’t able to explain the working
Problem 2 : Given ‘n’ vectors of different sizes and an integer ‘k’, find max possible set of 3 numbers from distinct arrays in which the difference of max-min <= k. (once you find a triplet, it cannot be counted in other sets).
It was a complicated question – I used sorting, min heaps, three pointers, and what not. My solution used three nested loops with individual sorting in each and creating min heap and taking difference every time and minimizing the difference of max-min and increasing the min pointer of one array if condition didn’t meet or increasing the pointer of all the arrays if it did. This went very complex and the intePASS.rviewer was confused as to what I was writing. He questioned me a lot of times on how it will work in each step. Sometimes I explained while other times I too struggled. He asked me complexity of the solution which I figured was somewhat like O(n*m^2*log(mn)) and he wasn’t convinced with anything I just told. I was grilled enough to believe that I am out of the process after that.
duration : 1.5-2 hrs.
result : selected for round 4 !!! (maybe they don’t judge you only on basis of your solution)
Round 4 : This was hiring manager round mostly.
He asked me to introduce myself which I did nicely with my prepared answer.
He then asked me if I had any weakness or any criticism I received for something last year ?
After that he asked me about my projects and internship experience. I told him honestly what I did and knew and when he asked about anything related to the projects like database, I told him till what extent I did the project and used it and don’t know in depth concepts of databases. He asked me about my extra curriculars activities which I had mentioned in my resume.
He then asked what all data structures I knew? I told him every data structure I was aware of. I forgot to mention linked lists and so at the end I said ” and yes, linked lists too” and then he told me then that he will be asking a question on linked list. (I don’t know if he had planned that whichever data structure I say last will be the topic of questions, because psychologically it will be our weak point 😛 and as a matter of fact linked lists were somewhat my weakness ).
The question was easy and a very common interview problem – Merge two sorted linked list.
I didn’t know the O(1) space complexity solution before so I did with arrays first, then he asked me if I can do it in O(1) space. So after a minute of thinking I was able to figure that out too. Then I wrote the code and the interviewer was satisfied.
Duration : 20-30 mins.
Result : SELECTED.