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");
}
}
}
}
-
\$\begingroup\$ This is not for a interview, but a solution of a hackerrank challenge. hackerrank.com/challenges/maximum-element \$\endgroup\$Tobias Otto– Tobias Otto2016年12月20日 12:37:51 +00:00Commented Dec 20, 2016 at 12:37
6 Answers 6
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.
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;
You could replace the magic numbers in the case
statements by constants with meaningfull names.
Unfortunately, it is not a queue. The sequence
push A
push B
pop
push C
pop
pop
outputs ACB
instead of ABC
. A push
operation must copy s2
back to s1
before doing s1.push()
.
-
\$\begingroup\$ I need to check because I did pass all my tests on Hackerrank. \$\endgroup\$CodeYogi– CodeYogi2016年12月20日 02:38:20 +00:00Commented Dec 20, 2016 at 2:38
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
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
-
\$\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\$Damaji kalunge– Damaji kalunge2016年12月20日 15:10:14 +00:00Commented Dec 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\$Malachi– Malachi2016年12月20日 15:47:44 +00:00Commented Dec 20, 2016 at 15:47
Explore related questions
See similar questions with these tags.