Adobe Interview Question for Member Technical Staffs


Country: India




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

It's a regular queue, not a circular queue, but aside from that I think this might work:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.Semaphore;


public class ThreadQueue {

	Semaphore writeSemaphore = new Semaphore(1);
	Semaphore readSemaphore = new Semaphore(1);
	Queue <Integer> queue = new LinkedList();
	int size; 
	
	public ThreadQueue(int i) {
		size = i;
	}

	public void enqueue(int i)
	{
		try
		{
			writeSemaphore.acquire();
			while(queue.size() >= size)
			{
				synchronized(writeSemaphore)
				{
					writeSemaphore.wait();
				}
			}
			queue.offer(i);
			writeSemaphore.release();
			synchronized(readSemaphore)
			{
				readSemaphore.notify();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public int dequeue()
	{
		try
		{
			readSemaphore.acquire();
			while(queue.size() == 0)
			{
				synchronized(readSemaphore)
				{
					readSemaphore.wait();
				}
			}
			int item = queue.poll();
			readSemaphore.release();
			synchronized(writeSemaphore)
			{
				writeSemaphore.notify();
			}
			return item;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}
	
}

- Anonymous May 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

overkill
does not use the uniqueness of circular queue operations

- Anonymous May 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi this is written test ques or interview.....if u have given java written can u pls give me some idea bcz i have java written test and interview next week...........

- anuj.aj07 May 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use BlockingQueue API present in Java 6.

- kuldeep May 15, 2013 | 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