Interview Question for Software Engineer / Developers


Country: Russia
Interview Type: In-Person




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

public class MyQueue<T> {
	
	private Stack<T> inputStack = new Stack<T>();
	private Stack<T> outputStack = new Stack<T>();
	
	public void enqueue(T data) {
		inputStack.push(data);
	}
	
	public T dequeue() {
		if (outputStack.isEmpty()) {
			if (inputStack.isEmpty()) {
				return null;
			}
			while (!inputStack.isEmpty()) {
				outputStack.push(inputStack.pop());
			}
		}
		
		return outputStack.pop();
	}

	// peek() and count() are trivial
}

- Iuri Sitinschi November 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c++, implementation

template <typename T>
class TwoStackQueue {
private:
	stack<T> in;
	stack<T> out;
public:
	void enqueue(T v) {
		in.push(v);
	}
	void dequeue() {
		if (out.empty()) {
			while (!in.empty()) {
				out.push(in.top());
				in.pop();
			}
		}
		out.pop();
	}
	T peek() {
		if (out.empty()) {
			while (!in.empty()) {
				out.push(in.top());
				in.pop();
			}
		}
		return out.top();
	}
	unsigned int count() {
		return in.size() + out.size();
	}
};

- kyduke November 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;

public class QueueUsing2Stacks {
	
	public Stack<Integer> stack1=new Stack<Integer>();
	public Stack<Integer> stack2=new Stack<Integer>();
	
	public void enque(int value){ //adding values to queue
		stack1.add(value);
	}
	
	public Integer deque(){
		
		if(stack1.isEmpty()){   //check if queue is empty or not.
			System.out.println("nothing in queue, cannot deque anything");
		}
		while(!stack1.isEmpty()){
			stack2.add(stack1.pop());
		}
		Integer temp= stack2.pop();
		
		while(!stack2.isEmpty()){
			stack1.add(stack2.pop());
		}
		return temp;
	}
	
	
	public static void main(String [] args){
		QueueUsing2Stacks queue=new QueueUsing2Stacks();
		queue.enque(5);
		queue.enque(3);
		queue.enque(2);
		queue.enque(1);
		System.out.println(queue.deque());
		System.out.println(queue.deque());
		queue.enque(20);
		queue.enque(10);
		System.out.println(queue.deque());
		System.out.println(queue.deque());
		System.out.println(queue.deque());
		System.out.println(queue.deque());			
	}	}

- Anonymous November 20, 2015 | 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