Oracle Interview Question for Financial Software Developers


Country: India
Interview Type: In-Person




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

I would say just use 2 queues. Assume you are inserting 1, 2, 3, 4, 5 (in this order). Assume one of the queues has 4, 3, 2, 1 already when you are trying to insert 5. First insert this 5 to the other queue, then keep removing each element from the other queue and insert it to this one. You will then end up with 5, 4, 3, 2, 1 in one queue and another empty queue.

This is inefficient because it requires O(n) for each insert.

- Sunny February 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

treat Front  Pointer === to the top  then Push and insert equivalent

    Pop  operation   -- reverse the queue  remove  and again reverse the list

- kalungedamaji1137 February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StackUsingQueue {
	Queue<Integer> q1 = new LinkedList<Integer>();
	Queue<Integer> q2 = new LinkedList<Integer>();

	public void pop() {

		while (q1.size() > 1)
			q2.add(q1.poll());

		System.out.println("POPped:" + q1.poll());

		while (!q2.isEmpty())
			q1.add(q2.poll());

	}

	public void push(int val) {
		System.out.println("PUSHed:" + val);
		q1.add(val);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		StackUsingQueue suq = new StackUsingQueue();
		suq.push(5);
		suq.push(4);
		suq.push(3);
		suq.push(2);
		suq.push(1);

		suq.pop();
		suq.push(6);
		suq.pop();
		suq.pop();
		suq.pop();

	}

}

- Anonymous March 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.


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