Data Structures & Algorithms · Interview question

What bounds must you check on a fixed-capacity stack or queue?

A strong answer

Overflow and underflow. Overflow: pushing/enqueuing onto a full structure, for a stack that's top == CAP, for a ring-buffer queue it's the full condition (head+1)%CAP == tail (or count == CAP). An unchecked push past capacity is a buffer overflow that corrupts adjacent memory. Underflow: popping/dequeuing from an empty structure, top == 0 for a stack, head == tail for a queue, which reads stale or out-of-bounds data if unchecked. On fixed memory there's no "grow to fit," so every push must verify space and every pop must verify non-empty, returning a failure indication the caller handles. The queue additionally carries the full-vs-empty ambiguity (head == tail means empty but a fully-wrapped buffer also lands there), resolved by sacrificing a slot or keeping a count. Skipping these checks is the most common way fixed-capacity structures cause memory corruption.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Stacks & Queues

LIFO stacks and FIFO queues: the two access disciplines behind the call stack, expression evaluation, and ISR-to-main event passing, and how to back each with fixed memory.

More Stacks & Queues questions

Browse all 472 interview questions