Google Interview Question for SDE-2s


Country: United States




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

public class CircularQueue {
	private int[] data;
	private int front;
	private int rear;
	private int size;
	private int count;

	public CircularQueue(int capacity) {
		intialize(capacity);
	}

	public synchronized void intialize(int capacity) {
		size = capacity;
		data = new int[size];
		front = 0;
		rear = -1;
		count = 0;
	}

	public synchronized void enqueue(int value) throws Exception {
		if (count >= size) {
			throw new Exception("Queue overflow");
		}

		if (rear == size - 1) {
			rear = -1;
		}

		data[++rear] = value;
		count++;
	}

	public synchronized void dequeue() throws Exception {
		if (count <= 0) {
			throw new Exception("Queue is Empty");
		}

		if (front == size - 1) {
			front = 0;
		} else if (front == rear) {
			front = 0;
			rear = -1;
		} else {
			front++;
		}
		count--;
	}

	/**
	 * For testing purpose only - Prints the values in the Queue
	 */
	public void printQueue() {
		if (front <= rear) {
			for (int i = front; i <= rear; i++) {
				System.out.print(data[i] + " -> ");
			}
		} else if (count > 0) {
			for (int i = front; i < size; i++) {
				System.out.print(data[i] + " -> ");
			}
			for (int i = 0; i <= rear; i++) {
				System.out.print(data[i] + " -> ");
			}
		}
		System.out.println();
	}

	public static void main(String[] args) throws Exception {
		CircularQueue cq = new CircularQueue(5);
		cq.enqueue(1);
		cq.enqueue(2);
		cq.enqueue(3);
		cq.enqueue(4);
		cq.printQueue();

		cq.dequeue();
		cq.enqueue(5);
		cq.enqueue(6);
		cq.printQueue();
	}
}

Output:
1 -> 2 -> 3 -> 4 -> 
2 -> 3 -> 4 -> 5 -> 6 ->

- ganni December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why make enqueue and dequeue contend on each other, when is most cases they actually don't?

- Anonymous December 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Classic producer consumer bounded buffer problem.

- Anonymous December 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CircularQueue<E> {

	private E[] queue;
	private int size;
	private int head,tail;

	public CircularQueue(int size){
		this.head = 0;
		this.tail = 0;
		this.size = size;
		this.initialize(size);
	}
	
	@SuppressWarnings("unchecked")
	public void initialize(int size){
		this.setQueue((E[]) new Object[size]);
		this.head = 0;
		this.tail = 0;
		this.size = size;
	}

	public E[] getQueue() {
		return queue;
	}

	public void setQueue(E[] queue) {
		this.queue = queue;
	}
	
	public synchronized void enqueue(E e){
		if(checkOverflow()){
			System.out.println("Queue overflow");
			return;
		}
		this.queue[this.tail] = e;		
		this.tail = (this.tail+1)%this.size;
	}
	
	public synchronized void dequeue(){
		if(checkUnderflow()){
			System.out.println("Queue underflow");
			return;
		}
		this.queue[this.head] = null;
		this.head = (this.head+1)%this.size;
	}
	
	private boolean checkOverflow(){
		if(this.tail==this.head && this.queue[this.head]!=null){
			return true;
		}
		return false;
	}
	
	private boolean checkUnderflow(){
		if(this.tail==this.head && this.queue[this.head]==null){
			return true;
		}
		return false;
	}
}

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

A solution using lock is trivial. Did the interviewer expect a lockless implementation?

- henryadam December 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Lockless implementation in an frigging interview? LOL.

Have you considered more granular locking?

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

Thread-safety could also assume blocking queue, i.e. instead of throwing an exception, enqueuing thread(s) is waiting when space is available and dequeuing thread(s) is waiting when the queue is empty. Has this been discussed at the interview?

- DS January 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CircularQueue {

	
	int maxSize;
	int[] array;

	int head; // index of the head element
	int tail; // index of the last added element

	int size;

	CircularQueue(int maxSize) {

		array = new int[maxSize];
		this.maxSize = maxSize;
		head = 0;
		tail = -1;
		size = 0;

	}

	synchronized void push(int value) {

		if (size == maxSize) {
			throw new RuntimeException("Queue is full");
		}

		if (tail == array.length - 1) {

			// wrap around case
			tail = -1;
		}

		array[++tail] = value;
		size++;
	}

	synchronized int pop() {
		if (size == 0) {
			throw new RuntimeException("Queue is empty");
		}

		int value = array[head++];

		if (head == array.length) {
			head = 0;
		}

		size--;
		return value;
	}

	@Override
	public String toString() {
		
		if(size ==0 ) {
			return "Queue is empty";
		}
		
		StringBuilder buf = new StringBuilder();
		
		buf.append("Size: "+size+"  Queue: ");
		
		
		int i = head;
		

		while(true) {
			buf.append(array[i]+" ");
			
			if( i == tail) {
				break;
			}
			
			if(i == array.length -1) {
				i =-1;
			}
			
			i++;
		}
		
		return buf.toString();
	}
}

- Serdar January 15, 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