Linkedin Interview Question for Software Engineer / Developers


Country: United States




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

public class BlockingQueue {

  private List queue = new LinkedList();
  private int  limit = 10;

  public BlockingQueue(int limit){
    this.limit = limit;
  }


  public synchronized void enqueue(Object item)
  throws InterruptedException  {
 
    while(this.queue.size() == this.limit) {
      wait();
    }
  //Notify all the threads that are waiting 
    if(this.queue.size() == 0) {
      notifyAll();
    }
    this.queue.add(item);
  }


  public synchronized Object dequeue()
  throws InterruptedException{
    while(this.queue.size() == 0){
      wait();
    }
  
    if(this.queue.size() == this.limit){
      notifyAll();
    }

    return this.queue.remove(0);
  }

}

- chandershivdasani September 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the above code is incorrect. If a reader blocks when the queue count was 0, then a writer puts something in the queue, the reader should be unblocked. The code above unblocks only when the queue fills to limit.
Calling "notifyAll()" on every Write() could be inefficient if the implementation is not tracking the number of blocked Readers(). I mean you don't want a system call on every queue Write().
[Note everything above is also true for Writers blocked on Limit, and readers need to unblock them]

- Tall Brian November 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class MyBlockingQueue<T> {
    
    private Queue<T> queue;
    private AtomicInteger limit = new AtomicInteger(10);
    private Lock put_lock = new ReentrantLock();
    private Lock take_lock = new ReentrantLock();
    
    public MyBlockingQueue(AtomicInteger limit){
        queue = new LinkedList<T>();
        this.limit = limit;
    }
    
    public boolean put(T item) throws InterruptedException{
        put_lock.lockInterruptibly();
        try{
            while(queue.size() == limit.get()){
                put_lock.newCondition().await();
            }
            put_lock.newCondition().signal();
            queue.add(item);
        }finally{
            put_lock.unlock();
        }
        
        return true;
    }
    
    public T take() throws InterruptedException{
        take_lock.lockInterruptibly();
        try{
            while(queue.size() == 0){
                take_lock.newCondition().await();
            }
            take_lock.newCondition().signal();
            return queue.poll();
        }finally {
            take_lock.unlock();
        }
    }
}

- inheritance January 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I found some comment for this at tutorials.jenkov.com/java-concurrency/blocking-queues.html: "If the queue size is not equal to either bound when enqueue() or dequeue() is called, there can be no threads waiting to either enqueue or dequeue items.

Could anyone elaborate this comment?

- robin October 02, 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