Skip to content

Commit d652bbf

Browse files
committed
add markdown
1 parent 3c676fc commit d652bbf

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

codereview/twoStackQueue.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
## Purpose
2+
I recently came across this interview question
3+
> Implement a queue with 2 `Stack`s. Your queue should have an `enqueue` and a `dequeue` function.
4+
Assume you already have a `Stack` implementation
5+
6+
## Discussion
7+
The way I thought about this problem is that one `Stack` would keep track of the input elements (i.e. what's been enqueued)
8+
and at some point, this `Stack` would transfer its elements to the second `Stack`.
9+
10+
By `pop`ping the results of the first `Stack` onto the the second `Stack`, the second `Stack` now represents the elements, ordered
11+
by when they were first inputted into the queue.
12+
13+
By `pop`ping off this second `Stack`, elements are now dequeued in the order that they were enqueued.
14+
15+
What happens in the case where some elements are enqueued, an element is dequeued, and then a bunch of elements are enqueued?
16+
When does the transferring of elements from the first `Stack` to the second take place?
17+
18+
In order to preserve the order of elements, the transfer of elements doesn't take place until the second `Stack` is empty.
19+
Thus, when dequeueing, first check that the second `Stack` is empty. If it isn't, then transfer the elements from the first `Stack`
20+
to the second.
21+
22+
For example, take the case where `1`, `2`, and `3` are enqueued.
23+
1. The first `Stack` looks like this: `| 1 | 2 | 3|`.
24+
2. The second `Stack` is empty.
25+
3. When `dequeue` is called, `3`, `2`, and `1` are popped off the first `Stack` in that order.
26+
4. Now the first `Stack` is empty
27+
5. The second `Stack` looks like `| 3 | 2 | 1 |`.
28+
6. Since `dequeue` was called, we `pop` the second `Stack` (bye bye `1`) and now it looks like `| 3 | 2 |`.
29+
7. Now suppose `4` is enqueued.
30+
8. The first `Stack` is no longer empty - it looks like: `| 4 |`.
31+
9. Now suppose `dequeue` is called again - we can't immediately transfer `4` from the first `Stack` to the second since
32+
the second `Stack` would look like `| 3 | 2 | 4 |`.
33+
10. Thus, we have to wait until all elements have been popped off the second `Stack` to transfer the elements from the first `Stack`.
34+
35+
36+
## Implementation
37+
38+
<!-- language: lang-java --!>
39+
40+
import java.util.NoSuchElementException;
41+
import java.util.Stack;
42+
43+
public class TwoStackQueue<T> {
44+
private final Stack<T> input = new Stack<>();
45+
private final Stack<T> output = new Stack<>();
46+
47+
public void enqueue(T data) {
48+
input.add(data);
49+
}
50+
51+
public T dequeue() {
52+
if (isEmpty()) {
53+
throw new NoSuchElementException("Unable to dequeue on empty queue");
54+
}
55+
56+
transfer();
57+
58+
return output.pop();
59+
}
60+
61+
public boolean isEmpty() {
62+
return input.empty() && output.empty();
63+
}
64+
65+
public T peek() {
66+
if (isEmpty()) {
67+
throw new NoSuchElementException("Unable to peek on empty queue");
68+
}
69+
70+
transfer();
71+
72+
return output.peek();
73+
}
74+
75+
private void transfer() {
76+
if (output.empty()) {
77+
while (!input.empty()) {
78+
output.push(input.pop());
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)