Final challenge: two classics and a trace
After this: Follow a bracket scan, build a queue from two stacks, and finish a min-stack.
This challenge has six steps, and each step is scored on its own. First you follow a bracket scan to the character that breaks it. Next you trace and finish a queue made from two stacks, and work out what its dequeue costs. Last, you predict and finish a min-stack, which is a stack that can tell you its smallest value at any time. You can try each step as many times as you like, and hints work as they do in the lessons.
Step 1 of 6Trace the brackets
Trace the brackets
Scan {[(])} from left to right, keeping the openers on a stack. Where does the scan fail, and why?
Trace two stacks
You can build a queue from two stacks. Enqueue pushes onto a stack called inbox. Dequeue pops from a stack called outbox. If outbox is empty, dequeue first moves every value from inbox to outbox, one pop and push at a time. Start empty, then enqueue 1, 2, 3; dequeue; enqueue 4. What do inbox and outbox hold, from bottom to top, and what does the next dequeue return?
class QueueOfTwoStacks: def __init__(self): self.inbox = [] # enqueue pushes here self.outbox = [] # dequeue pops from here
def enqueue(self, value): self.inbox.append(value)
def dequeue(self): if not self.outbox: # refill only when empty while self.inbox: self.outbox.append(self.inbox.pop()) # reversing restores arrival order return self.outbox.pop()import java.util.ArrayDeque;import java.util.Deque;
public class QueueOfTwoStacks { Deque<Integer> inbox = new ArrayDeque<>(); // enqueue pushes here Deque<Integer> outbox = new ArrayDeque<>(); // dequeue pops from here
void enqueue(int value) { inbox.push(value); }
int dequeue() { if (outbox.isEmpty()) { // refill only when empty while (!inbox.isEmpty()) { outbox.push(inbox.pop()); // reversing restores arrival order } } return outbox.pop(); }}Fill the refill
Complete the refill: while inbox has values, move the top of inbox onto outbox.
def dequeue(self): if not self.outbox: while self.inbox: ______________________________ # ← move the top of inbox onto outbox return self.outbox.pop() int dequeue() { if (outbox.isEmpty()) { while (!inbox.isEmpty()) { ______________________________ // ← move the top of inbox onto outbox } } return outbox.pop(); }Cost it
What does dequeue cost on the queue made of two stacks?
Predict the min-stack
MinStack is a stack that can tell you its smallest value at any time. It keeps a second stack, mins, whose top is always the smallest value now on the stack. What do the three print lines show?
class MinStack: def __init__(self): self.values = [] self.mins = [] # mins[i] is the smallest of values[0..i]
def push(self, value): self.values.append(value) if not self.mins or value <= self.mins[-1]: self.mins.append(value) # a new minimum else: self.mins.append(self.mins[-1]) # the old minimum still holds
def pop(self): self.mins.pop() return self.values.pop()
def min(self): return self.mins[-1]import java.util.ArrayDeque;import java.util.Deque;
public class MinStack { Deque<Integer> values = new ArrayDeque<>(); Deque<Integer> mins = new ArrayDeque<>(); // the top of mins is the smallest value on the stack
void push(int value) { values.push(value); if (mins.isEmpty() || value <= mins.peek()) { mins.push(value); // a new minimum } else { mins.push(mins.peek()); // the old minimum still holds } }
int pop() { mins.pop(); return values.pop(); }
int min() { return mins.peek(); }}s = MinStack()for v in [5, 3, 7]: s.push(v)print(s.min())s.pop()s.push(2)print(s.min())s.pop()s.pop()print(s.min(), s.values, s.mins)MinStack s = new MinStack();for (int v : new int[] {5, 3, 7}) s.push(v);System.out.println(s.min());s.pop();s.push(2);System.out.println(s.min());s.pop();s.pop();System.out.println(s.min() + " " + s.values + " " + s.mins);Finish the min-stack
value is larger than the current minimum. Write the else branch: push the old minimum again, so that mins holds as many entries as values.
def push(self, value): self.values.append(value) if not self.mins or value <= self.mins[-1]: self.mins.append(value) else: ______________________________ # ← value is not a new minimum: push the old minimum again void push(int value) { values.push(value); if (mins.isEmpty() || value <= mins.peek()) { mins.push(value); } else { ______________________________ // ← value is not a new minimum: push the old minimum again } }- Follow a bracket scan to its failure, build a queue from two stacks and work out what its dequeue costs, and finish a min-stack that gives its smallest value in O(1).