Citigroup Interview Question for Analysts


Country: United States




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

FIFO implementation. You can use an array (and re-size them if it is full) or Linked list to keep adding to the tail, provide elements from head.

- howaboutthis March 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use LinkedList with 2 pointers Head and Tail.
Remove element from HEAD and add element to Tail.

- Anil March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java implementation of Queue using Linked List:

public class Queue {
// inner class for Node
private class Node {
private int item;
private Node next;
}

private int N; // number of elements in queue
private Node first;
private Node last;

public Queue() {
first = null;
last = null;
N = 0;
}

// check if queue is empty
public boolean isEmpty() {
return first == null;
}

// return the number of elements in queue
public int size() {
return N;
}

// return the first element of queue without removing it from queue
public int peek() {
if (isEmpty())
throw new RuntimeException("Queue underflow");
return first.item;
}

// add an element to queue
public void enqueue(int item) {
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if (isEmpty())
first = last;
else
oldlast.next = last;
N++;
}

// remove the first element
public int dequeue() {
if (isEmpty())
throw new RuntimeException("Queue underflow");
int item = first.item;
first = first.next;
N--;
if (isEmpty())
last = null; // to avoid loitering
return item;
}

// string representation of Queue
public String toString() {
StringBuilder s = new StringBuilder();
Node n = first;

while (n != null) {
s.append(n.item + " ");
n = n.next;
}
return s.toString();
}

/**
* A test client.
*/
public static void main(String[] args) {
Queue q = new Queue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);

System.out.println(q);

q.dequeue();
q.dequeue();
System.out.println(q);
}
}

- singhSahab October 06, 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