Two-Spike Order Rail
A small diner keeps order tickets on spikes. You push a ticket onto a spike and can only take the top one, so a spike is a stack. But the cooks must cook orders oldest first.
The diner has exactly two spikes. Used with care, they can hand out tickets in arrival order.
Your task
Write a class TicketRail with these methods:
pin(ticket): a new order arrives with this ticket number.serve(): remove and return the oldest ticket still waiting.peek(): return the oldest ticket without removing it.isEmpty(): returntrueif no tickets are waiting.
Store tickets only in two stacks, using only push, pop, look at the top, and check if empty (an array with push / pop in JavaScript, a list with append / pop in Python). Each method should take O(1) time on average over many calls.
class TicketRail
Examples
new TicketRail() .pin(12) .pin(15) .serve() .pin(18) .peek() .serve() .serve() .isEmpty()
[null, null, null, 12, null, 15, 15, 18, true]
Tickets 12 and 15 are pinned. The oldest, 12, is served first. Then 18 arrives. The oldest left is 15, so peek shows 15 and serve hands out 15, then 18. Now the rail is empty.
Constraints
- 1 ≤ number of method calls ≤ 1,000
- 1 ≤ ticket ≤ 1,000,000; ticket numbers may repeat.
- serve and peek are only called when at least one ticket is waiting.
Start with brute force
Use one spike. To serve, lift every ticket onto the second spike, take the bottom one, then put the rest back.
Each serve moves every waiting ticket twice, so with n tickets waiting a serve is O(n) and a busy shift costs O(n²). The better design moves each ticket across at most once.
Hints
0 of 4 shown- Hint 1
Pin tickets 1, 2, 3 on one spike. Now lift them off one by one and push each onto the second spike. Which ticket is on top of the second spike?
- Hint 2
Moving a whole spike onto another reverses it, putting the oldest ticket on top. One spike can take new tickets while the other hands out old ones.
- Hint 3
When is it safe to move tickets to the serving spike? Try: pin 1, pin 2, serve, pin 3, serve. What goes wrong if you move ticket 3 across while ticket 2 is still there?
- Hint 4
Keep an
inboxstack forpinand anoutboxstack forserveandpeek. Only when the outbox is empty, pour the whole inbox onto it. Then the outbox top is always the oldest ticket.isEmptychecks both.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Pouring one stack onto another reverses it. New tickets go onto an inbox spike; pouring it onto an outbox spike puts the oldest ticket on top, ready to serve.
Trace the example: pin 12, pin 15, serve, pin 18, peek, serve, serve.
- After pinning 12 and 15, the inbox is
[12, 15]with 15 on top, and the outbox is empty. - On the first serve the outbox is empty, so the inbox is poured into it and the outbox becomes
[15, 12]with 12 on top. Popping returns 12. - After pinning 18, the inbox is
[18]and the outbox is[15]. - The peek does not pour, because the outbox is not empty. The outbox top is 15, which is the oldest ticket, so peek returns 15.
- The next serve pops 15. The serve after that finds the outbox empty, pours 18 across and pops it.
The peek step shows the key rule. If 18 were poured on top of 15 at that point, the next serve would wrongly return 18, so the inbox is poured only when the outbox is empty.
One pour can move many tickets, so a single serve can be slow. But each ticket is pushed once, poured once and popped once: O(1) per call on average (amortized).
class TicketRail {
constructor() {
this.inbox = []; // new tickets, newest on top
this.outbox = []; // reversed, oldest on top
}
pin(ticket) {
this.inbox.push(ticket);
}
refill() {
// only pour when the outbox is empty, or the order breaks
if (this.outbox.length === 0) {
while (this.inbox.length > 0) this.outbox.push(this.inbox.pop());
}
}
serve() {
this.refill();
return this.outbox.pop();
}
peek() {
this.refill();
return this.outbox[this.outbox.length - 1];
}
isEmpty() {
return this.inbox.length === 0 && this.outbox.length === 0;
}
}class TicketRail:
def __init__(self):
self.inbox = [] # new tickets, newest on top
self.outbox = [] # reversed, oldest on top
def pin(self, ticket):
self.inbox.append(ticket)
def _refill(self):
# only pour when the outbox is empty, or the order breaks
if not self.outbox:
while self.inbox:
self.outbox.append(self.inbox.pop())
def serve(self):
self._refill()
return self.outbox.pop()
def peek(self):
self._refill()
return self.outbox[-1]
def is_empty(self):
return not self.inbox and not self.outboxRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.