A queue via two stacks builds a first-in, first-out (FIFO) queue — where the oldest waiting element leaves first — out of nothing but two last-in, first-out (LIFO) stacks. This is the classic interview exercise known as Implement Queue using Stacks (LeetCode 232). The bridge between the two orders is a single move: popping every element off one stack and pushing it onto another reverses the sequence, so the newest-on-top of one stack becomes the oldest-on-top of the other. Your queueVia2Stacks() factory returns an object with five methods and stores its data in two plain arrays used as stacks via push and pop. See Queue for background.
function queueVia2Stacks(): {
enqueue(x: unknown): void; // add x to the back of the queue
dequeue(): unknown | undefined; // remove and return the front (oldest), or undefined if empty
peek(): unknown | undefined; // return the front without removing it, or undefined if empty
size(): number; // how many elements are queued
isEmpty(): boolean; // true when the queue holds nothing
};
const q = queueVia2Stacks();
q.enqueue('a');
q.enqueue('b');
q.enqueue('c');
q.dequeue(); // 'a' — oldest out first (FIFO)
q.dequeue(); // 'b'
q.peek(); // 'c' — the front, still in the queue
q.size(); // 1
// Interleaving enqueues and dequeues keeps FIFO order:
const q = queueVia2Stacks();
q.enqueue(1);
q.enqueue(2);
q.dequeue(); // 1
q.enqueue(3);
q.dequeue(); // 2 — the 3 you just added waits its turn
q.dequeue(); // 3
q.dequeue(); // undefined — empty
dequeue returns the element that has waited longest, the opposite of a stack. enqueue adds at the back; dequeue and peek read from the front.push and pop alone. Don't reach into the middle of an array or use shift/unshift; the whole point is to build FIFO from LIFO primitives.undefined — dequeue() and peek() on an empty queue must return undefined, not throw.n operations costs O(n) total even though a single dequeue can occasionally do more work.0, false, null, and undefined. To tell an empty queue apart from a queued undefined, check size() or isEmpty() rather than the return value.