Skip to question
SystemVerilogDesignVerificationFirmwareArchitectureASIC Interview Questions→
/Interview questions/Implement and verify a bounded queue

Q012·Free·Firmware

Implement and verify a bounded queue

Difficulty
Medium
Topic
Data Structures
Language
PYTHON
Interview prompt

Question

Implement a fixed-capacity FIFO with O(1) push, pop, and peek, then briefly explain how you would verify its boundary and wraparound behavior. Capacity is an integer; values may be arbitrary Python objects, including None.

Circular queue trace showing accepted pushes, a full-queue rejection, a pop, wraparound, and the final logical order
A bounded queue must reject overflow without corrupting its head, tail, count, or logical order.
Candidate starting point

Implementation scaffold

class BoundedQueue:

    def __init__(self, capacity):
        """TODO: implement this method."""
        pass

    def push(self, value):
        """TODO: implement this method."""
        pass

    def pop(self):
        """TODO: implement this method."""
        pass

    def peek(self):
        """TODO: implement this method."""
        pass

    def __len__(self):
        """TODO: implement this method."""
        pass

    def check_invariants(self):
        assert 0 <= self.size <= self.capacity
        assert 0 <= self.head < self.capacity
        assert 0 <= self.tail < self.capacity
        if self.size == 0:
            assert self.head == self.tail
Reviewed example

Trace one case

Input
capacity=3; push(10), push(20), push(30), push(99), pop(), push(40)
Expected output
push(99) reports overflow and changes no state; pop returns 10; final queue is [20, 30, 40]

The failed overflow leaves the full queue intact, then the final push wraps the circular tail into the slot cleared by pop.

What to cover

Requirements

  1. Reject nonpositive capacity, overflow, and pop or peek underflow.
  2. Use a circular array with explicit head, tail, and size state.
  3. Clear a popped slot so the queue does not retain stale object references.
  4. Describe directed wraparound checks and a seeded randomized comparison against collections.deque.
Exact question handoffPractice Q012

Solve it in the question bank, keep your progress, and reveal the reviewed solution when your access allows.

Open in question bank →
Solution accessEach time you open this Solution, one Practice Credit is used; it is not permanently unlocked. Premium Solution content also uses one credit per opening.
Continue learning

Firmware Guide

Review algorithms, data structures, fixed-memory reasoning, concurrency, and silicon bring-up.

  • Data Structures
  • Python
  • Circular buffer
  • Queue
Firmware Guide →
Continue practicing

Related questions

Q071 · Data StructuresDynamic arrays and queuesEasy→Q219 · Data StructuresQueue methodsEasyP→Q094 · Data StructuresImplement a simple least-recently-used cacheMedium→Q176 · Data StructuresMaintain the K largest stream valuesMedium→Q042 · Data StructuresReturn the first unique value in a streamEasy→
ASIC.FYI · Learn silicon end to end.info@asic.fyi