8
\$\begingroup\$

Although it's a well-known algorithm, I thought to implement it from the perspective of a coding interview. I want to know if I can improve my code somehow. So, given the time constraint, is this a good enough solution to impress the interviewer?

class MyQueue {    private Stack<Integer> s1;    private Stack<Integer> s2;    MyQueue() {        s1 = new Stack<>();        s2 = new Stack<>();    }    public void enqueue(int item) {        s1.push(item);    }    private void move() {        if (s1.isEmpty()) {            throw new NoSuchElementException();        } else {            while (!s1.isEmpty()) {                s2.push(s1.pop());            }        }    }    public int peek() {        if (s2.isEmpty()) {            move();        }        return s2.peek();    }    public int dequeue() {        if (s2.isEmpty()) {            move();        }        return s2.pop();    }}public class Solution {    public static void main(String[] args) {        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */        Scanner s = new Scanner(System.in);        int N = s.nextInt();        MyQueue q = new MyQueue();        for (int i = 0; i < N; i++) {            int op = s.nextInt();            switch (op) {                case 1:                    int item = s.nextInt();                    q.enqueue(item);                    break;                case 2:                    q.dequeue();                    break;                case 3:                    System.out.println(q.peek());                    break;                default:                    System.out.println("Invalid option");            }        }    }}
Jamal's user avatar
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
askedDec 19, 2016 at 7:20
CodeYogi's user avatar
\$\endgroup\$
1

6 Answers6

5
\$\begingroup\$

Maybe You could do better if you:

  • Define an interface with complete JavaDoc.
  • Make s1 and s2 final
  • Write a JUnit-Test with constant testdata instead of reading input from keyboard. Do not use System.out.println in JUnit.
answeredDec 19, 2016 at 15:31
Tobias Otto's user avatar
\$\endgroup\$
4
\$\begingroup\$

you can use the generics to support any kind of data type in the Queue

   class MyQueue<T> {      private Stack<T> s1;      private Stack<T> s2;
answeredDec 19, 2016 at 9:33
Damaji kalunge's user avatar
\$\endgroup\$
3
\$\begingroup\$

You could replace themagic numbers in thecase statements by constants with meaningfull names.

answeredDec 19, 2016 at 12:15
Timothy Truckle's user avatar
\$\endgroup\$
0
\$\begingroup\$

Unfortunately, it is not a queue. The sequence

push Apush Bpoppush Cpoppop

outputsACB instead ofABC. Apush operation must copys2 back tos1 before doings1.push().

answeredDec 19, 2016 at 18:26
vnp's user avatar
\$\endgroup\$
1
  • \$\begingroup\$I need to check because I did pass all my tests on Hackerrank.\$\endgroup\$CommentedDec 20, 2016 at 2:38
0
\$\begingroup\$

The logic is perfect . The case mentioned above will not occur

Operation       Stack:s1     Stack:S2   output       Comment Push A            A                            ----------------------------------------------- Push B            B                                                  A  ------------------------------------------------ Pop                           A           A          s2 is empty hence will                                  B                       copy whole 1 to s2------------------------------------------------ Push C             C          B------------------------------------------------ Pop                C                      B            s2 is not empty not do                                                        any operation of move--------------------------------------------------- Pop                            C          C            S2 becomes empty hence                                                          will copy from s1
answeredDec 20, 2016 at 5:51
Damaji kalunge's user avatar
\$\endgroup\$
0
\$\begingroup\$

ENQUEUE(Q, ELEMENT)

1) While stack1 is not empty, push everything from satck1 to stack2.

2) Push ELEMENT to stack1

3) Push everything back to stack1.

DQueue(q)

1) If stack1 is empty then error

2) Pop an item from stack1 and return it

answeredDec 20, 2016 at 15:00
user126250's user avatar
\$\endgroup\$
2
  • \$\begingroup\$By this solution enqueue operation becomes more cos-tier , as each and every time need to copy from stack1 to stack2 and most of time stack is not empty. So It is optimal to make the DQueue operation costier as presented in solution.\$\endgroup\$CommentedDec 20, 2016 at 15:10
  • \$\begingroup\$could you please explain what you are doing and how it works (please edit into the answer) thank you\$\endgroup\$CommentedDec 20, 2016 at 15:47

You mustlog in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.