Linkedin Interview Question


Team: systems and infrastructure
Country: United States
Interview Type: Phone Interview




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

Implementation using semaphores

public class BoundedBlockingQueue<T> {
	private final List<T> queue;
	private final Semaphore availableItem;
	private final Semaphore availableSpace;
	
	public BoundedBlockingQueue(int size) {
		this.queue = new LinkedList<T>();
		availableItem = new Semaphore(0);
		availableSpace = new Semaphore(size);
	}
	
	public void add(T element) throws InterruptedException {
		availableSpace.acquire();
		boolean isAdded = false;
		try {
			synchronized (this) {
				isAdded = queue.add(element);
			}
		} finally {
			if (!isAdded) {
				availableSpace.release();
			} else {
				availableItem.release();
			}
		}
	}
	
	public T remove() throws InterruptedException {
		availableItem.acquire();
		T element;
		synchronized (this) {
			element = queue.remove(queue.size() - 1);
			availableSpace.release();
		}
		return element;
	}
	
}

- Looper October 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What's the point of the availableItem semaphore? you only lock it once..

- Sid February 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just implement an array blocking queue with a producer and consumer class. The implementation is pretty simple.

- compmanic August 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

got this q asked.

- me too October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the solution:-

public class ProducerConsumerProblem {
private BlockingQueue<String> items = new LinkedBlockingQueue<String>(5); 
private Scanner scanner = new Scanner(System.in);

public ProducerConsumerProblem() {
	consume();
	produce();
}
	public void produce(){
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				while(true){
					System.out.print("Name the new Item - ");
					try {
						items.put(scanner.next());
						TimeUnit.SECONDS.sleep(1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		Thread t = new Thread(runnable);
		t.start();
	}
	
	public void consume(){
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				while(true){
					try {
						System.out.print("Retrieving the item - "+items.take());
						TimeUnit.SECONDS.sleep(5);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		Thread t = new Thread(runnable);
		t.start();
	}
	public static void main(String[] args) {
		new ProducerConsumerProblem();
	}

}

- Rambrij Chauhan December 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using java synchronized, both deque and enque has to ensure operations are vaild or else wait.

wait until space available for dequing then notifying waiting threads.

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();
    }
    this.queue.add(item);
    notifyAll();
  }


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

}

- next_big_gig October 31, 2014 | 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