Amazon Interview Question for Software Engineer / Developers






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

/* atomic execution by hardware */
boolean test_and_set(boolean &mutex) {
   boolean tmp=mutex;
   mutex = true;
   return tmp;
}

void mutex_init(boolean &mutex) {
   mutex = false;
}

void mutex_lock(boolean &mutex) {
   while(test_and_set(mutex));
}

void mutex_unlock(boolean &mutex) {
   mutex = false;
}

- jzhu May 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Lock {
  private Object lockObj = new Object();
  private Thread owner;
  private int lockCount;

  /**
   * Waits for up to 'maxWait' milliseconds to acquire the lock,
   * and returns true if the lock was acquired.
   **/
  public boolean acquireLock(int maxWait) throws InterruptedException {
    Thread currentThread = Thread.currentThread();
    synchronized (lockObj) {
      if (owner == currentThread) {
        lockCount++;
        return true;
      }
      long waitedSoFar = 0L;
      while (owner != null) {
        long t0 = System.currentTimeMillis();
        long timeToWait = maxWait - waitedSoFar;
        if (timeToWait <= 0)
          return false;
        lockObj.wait(timeToWait);
        if (owner != null) {
          waitedSoFar += System.currentTimeMillis() - t0;
        }
      }
      owner = currentThread;
      lockCount = 1;
      return true;
    }
  }
  public void releaseLock() {
    Thread currentThread = Thread.currentThread();
    synchronized (lockObj) {
      if (owner == currentThread) {
        lockCount--;
        if (lockCount == 0) {
          owner = null;
          lockObj.notify();
        }
        return;
      } else {
        // Only the owner can release the lock!
        throw new IllegalStateException();
      }
    }
  }
}

- Sriram January 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is a chick egg dilemma, the sychronized() thing itself is already a mutex.

i think this is wrong.

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

I guess, the answer they are looking for is-
By using atomic instructions like test and set

- S3 January 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class mutex
{
int val;
public:
mutex(){val=0;}
lock()
{
while(testandset(&val));
}

unlock()
{
val=0;
}
}

- abc July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

try_again: test_set address;
cmp address 0;
branch_equal return;
call sleep;
jump try_again;
return:

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

/* By swap&test */
void swap(boolean &a, boolean &b) {boolean tmp=a; a=b; b=tmp;}
void mutex_init(boolean &mutex) {mutex=false;}
void mutex_lock(boolean &mutex) {
boolean wait=true;
while(wait) { swap(&wait, &mutex); }
}
void mutex_unlock(boolean &mutex) {
mutex=false;
}

- jzhu May 20, 2012 | 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