Amazon Interview Report
- 0of 0 votes
AnswersCoding: Write a Client/Server. Three methods are given. Msg.Get(), Msg.Process(), Msg.Send(). Write code. Since Msg.Get() and Msg.Send() has to send messages over the network. It takes a lot more number of threads. So how many threads out of 10, would allocate to each of the three processes. What is the proportion?
- xankar March 16, 2013 in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Threads - 0of 0 votes
AnswersExplain what is “static synchronized?” What does it lock on? what is ‘synchronized’? what does it lock on?
- xankar March 16, 2013 in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Java - 3of 3 votes
AnswersCoding:
Public void TransferAccount(AccountID id1, AccountID id2){ Account a1 = id1.GetAccount(); Account a2 = id2.GetAccount(); //Swap amounts. Temp = a1.Balance; a1.Balance = a2.Balance; a2.Balance = Temp; }
Q1: How do you make it thread safe?
I said use “public void synchronized” Good. But terrible performance since the entire method is synchronized.
Q2: Can you not lock on the entire method? I said used nested locks:Synchonized(a1) Synchronized(a2) { //swap }
His q: This will lead to a deadlock if in another thread I call Transfer (id2, id1) and Transfer (id1, id2).
Synchonized(a1) Synchronized(a2) { //swap }
Synchonized(a2) Synchronized(a1) { //swap }
How do you prevent this then? How do you design your code to not to get in to deadlock? (stumbled here)
- xankar March 16, 2013 in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm Java Threads - 0of 0 votes
AnswersPuzzle: There are 5 slots(1,2,3,4,5) and 5 people(A,B,C,D,E). Each of them provide their preferences. A=1, B=2, C=3, D=4, C=4. Given an arbitary starting sequence say BCDEA, going clock wise: how many passes does it take to fill in those slots so that max number of people are happy. People are happy if A gets 1, B gets 2, C gets 3, etc. Remember D and E cannot be kept happy together at the same time because both of them prefer Slot-4.
- xankar March 16, 2013 in United States
My answer: Worst case scenario when you start at BCDEA(only E is happy because E comes in 4th position and prefers 4), next rotation pass CDEAB(no one is happy), DEABC(no one), EABCD(no one), ABCDE(4 people are happy). So for N people, it takes N passes.
His question: Can you do better than N passes? (Can you do this in less than N passes?)
(I tried to come up with something but stumbled.)| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm - 0of 2 votes
Answers3) Coding question:
public class ThreadingQuestion extends Thread { public static void main(String[] args) { public static boolean flagRun = true; new thread { void run(){ while (flagRun) //do something }.start() flagRun = false; }
Will the thread spawned will ever see the flagRun=false?
- xankar March 16, 2013 in United States
My corrected answer after a couple of attempts: No, since each thread will get a copy of it’s own flagRun, changing the flagRun value in the main thread will not be seen in the spawned thread.
How to fix it: declare flagRun to be volatile so that the values can be changed and seen in either threads.| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Java - 1of 1 vote
AnswersWhat is difference between "volatile" and "static volatile"? Give an example
- xankar March 16, 2013 in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Java - 0of 0 votes
AnswersHow do you detect deadlocks? What tools would you use? I said do “Kill -3 .<process id>, and analyse if anything is deadlocked.
- xankar March 16, 2013 in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Java Threads