1. Tell me about yourself. 2. Implement Queue using stacks. 3. Find first non-repeated character in a given string.
Anonymous
class MyStack { // Push element x onto stack. List q1 = new LinkedList(); List q2 = new LinkedList(); public void push(int x) { if(q1.isEmpty()){ q1.add(x); while (!q2.isEmpty()){ q1.add(q2.remove(0)); } }else{ q2.add(x); while(!q1.isEmpty()){ q2.add(q1.remove(0)); } } } // Removes the element on top of the stack. public void pop() { if(q1.isEmpty()){ q2.remove(0); }else{ q1.remove(0); } } // Get the top element. public int top() { if(q1.isEmpty()){ return q2.get(0); } else{ return q1.get(0); } } // Return whether the stack is empty. public boolean empty() { return q1.isEmpty() && q2.isEmpty(); } }
Check out your Company Bowl for anonymous work chats.