Amazon Interview Question for Software Engineer / Developers






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

This was the only question asked to me for 45min phone interview. Interviewer had shared etherpad where I was suppose to write the code. I came up with below code which I thought was not good enough because he asked me a major flaw in this code. So I realized that it has unnecessary high CPU utilization. So he asked me how would you remove that(not implementation). At that time could think of only putting sleep (after the interview I realized wait-notify would have been better, but anyways time was gone).
But I got my third interview call, so I guess he just judged my thinking process. :)
Here is that (no so good)code:-

public class Semaphore {
    private int counter;
    private int boundValue;
        
    public Semaphore () {
        counter = 0;
        boundValue = 0;
    }
    
    public Semaphore (int bound) {
        counter = 0;
        boundValue = bound;
    }
    
    public synchronized boolean isSafe() {
       while (true) {
            if (counter < boundValue) { 
               counter ++;
                break;
            }
        }
        return true;
    }
    
    public synchronized boolean notifyDone() {
        counter--;
        return true;
    }  
    
    public boolean setBound(int value) {
       boundValue = value; 
       return true;
    }  
    
    public int getBound() {
       return boundValue;
    } 
}

- Mugdha February 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above code gets into a deadlock when the counter is more than the bound value since it does not release the lock.

- Ajay February 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I did not understand your point here.
Suppose bound value is 5.
So now thread1, thread2, thread3, thread4, thread5 will call isSafe and increment counter to 5. now if 2 more threads thread6, thread7 calls it, they will be in continuous while loop until any of 1 to 5 threads call notifyDone().

The flaw in this code can be that Semaphore class should be singleton.

- Anonymous February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Maybe is the busy waiting? the isSafe method you wrote, will keep the thread stay in the while loop checking till then break it. Maybe put the thread to sleep when it check unsafe and once it isSafe then wake all the sleeping processes?

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

here is the implementation of counting semaphores.

public class CountingSemaphores {
	private int signals = 0;
	private int upperBound = 0;
	
	public CountingSemaphores(int upperBound) {
		this.upperBound = upperBound;
	}
	
	public synchronized void take() throws InterruptedException {
		while(signals == upperBound) {
				wait();
		}
		signals++;
		this.notify();
	}
	public synchronized void release() throws InterruptedException {
    		while(signals == 0) { 	
				wait();
		}
    		this.signals--;
    		this.notify();
	}
}

Now simply create one object of this CountingSemaphores class and use take and release methods for synchronizations.

CountingSemaphores countingSemaphore = new CountingSemaphores(10);

Task1 t1 = new Task1(countingSemaphore);
Task2 t2 = new Task2(countingSemaphore);
t1.start();
t2.start();

In run methods of task1 call take method of CountingSemaphores class and in run method of task2 call release method of CountingSemaphores class.

Thats it I hope this will help.
Correct me if I am wrong or any modification required in code.

Thanks

- upendrajat77 September 20, 2015 | 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