Optiver software engineer interviews cover algorithms, data structures, system design, and coding problems drawn from real interview rounds.
CircularBufferQueue with methods to enqueue and dequeue elements in a circular manner. The enqueue method should add an element, and if the queue is full, it should overwrite the oldest element. The dequeue method should remove and return the front element. def __init__(self, capacity: int) -> None: def enqueue(self, value: Any) -> None: def dequeue(self) -> Any: def is_empty(self) -> bool: def is_full(self) -> bool: cbq = CircularBufferQueue(3) cbq.enqueue(1) cbq.enqueue(2) cbq.enqueue(3) cbq.enqueue(4) out = cbq.dequeue() 1 cbq.enqueue(5) out = cbq.dequeue() 2 CircularQueue class that implements a circular queue with basic operations such as enqueue, dequeue, and is_empty. The queue should be of fixed size, and once it reaches its maximum capacity, new items should overwrite the oldest items. def __init__(self, size: int) -> None: def enqueue(self, item: Any) -> None: def dequeue(self) -> Any: def is_empty(self) -> bool: queue = CircularQueue(3) queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) queue.enqueue(4) # This should overwrite 1
queue.dequeue() returns 2 queue = CircularQueue(2) queue.enqueue(5) queue.enqueue(6) queue.dequeue() queue.enqueue(7)
queue.dequeue() returns 6 1 <= size <= 1000 CircularQueue class that has a fixed size. It should allow for the operations of enqueue (adding an element to the queue), dequeue (removing an element from the front of the queue), and peek (looking at the front element without removing it). Ensure that these operations handle the wrap-around mechanism when the end of the buffer is reached. When the queue is empty, the dequeue and peek operations should return None.Function/class signature:class CircularQueue:def __init__(self, size: int): # initializes the queue with a given sizedef enqueue(self, value: Any) -> bool: # adds value to the queue, returns successdef dequeue(self) -> Optional[Any]: # removes and returns the front valuedef peek(self) -> Optional[Any]: # returns the front value without removing itdef is_empty(self) -> bool: # checks if the queue is emptydef is_full(self) -> bool: # checks if the queue is fullcq = CircularQueue(3); cq.enqueue(1); cq.enqueue(2); cq.enqueue(3); cq.dequeue(); cq.peek() 2 1, the next element 2 is at front.Example 2: Input: cq = CircularQueue(2); cq.enqueue(1); cq.enqueue(2); cq.enqueue(3) False False because the queue is full.Constraints:1 <= size <= 1000.enqueue operation must handle the case when the queue is full gracefully.CircularBufferQueue class that implements a circular queue with the following operations: enqueue to add an item to the queue, dequeue to remove an item, and is_empty to check if the queue is empty. The queue should have a fixed size limit to ensure efficient memory usage and prevent overflow. Use None or an alternative mechanism to represent an empty slot in the buffer.class CircularBufferQueue: def __init__(self, size: int): def enqueue(self, item: Any) -> bool: def dequeue(self) -> Any: def is_empty(self) -> bool: cbq = CircularBufferQueue(3) cbq.enqueue(1) → Output: True cbq.enqueue(2) → Output: True cbq.dequeue() → Output: 1 cbq.enqueue(3) → Output: True cbq.enqueue(4) → Output: True cbq.dequeue() → Output: 2 1 and 1000.def __init__(self, capacity: int) -> None: Initializes the circular buffer queue with a given capacity.def enqueue(self, item: Any) -> None: Adds an item to the queue. Raises an error if the queue is full.def dequeue(self) -> Any: Removes and returns the item at the front of the queue. Raises an error if the queue is empty.def is_empty(self) -> bool: Returns True if the queue is empty, otherwise False.def is_full(self) -> bool: Returns True if the queue is full, otherwise False.def clear(self) -> None: Clears all items from the queue.CircularBufferQueue(3) followed by enqueue(1), enqueue(2), enqueue(3), dequeue() → Output: 1 → Explanation: The queue initially contains [1,2,3]. After dequeuing, the first item is removed.Example 2: CircularBufferQueue(2) followed by enqueue(10), enqueue(20), enqueue(30) → Output: Error → Explanation: The third enqueue operation fails because the queue is full.Constraints:capacity (1 <= capacity <= 10000)10000.def __init__(self, capacity: int) -> None: Initializes the circular buffer with a given capacity.def enqueue(self, value: Any) -> None: Adds an element to the buffer, raises an exception if the buffer is full.def dequeue(self) -> Any: Removes and returns the oldest element, raises an exception if the buffer is empty.def is_full(self) -> bool: Returns True if the buffer is full, otherwise False.def is_empty(self) -> bool: Returns True if the buffer is empty, otherwise False.cb = CircularBuffer(3); cb.enqueue(1); cb.enqueue(2); cb.enqueue(3) → Output: None, Explanation: The buffer contains [1, 2, 3].cb.dequeue() → Output: 1, Explanation: Removes 1, buffer now contains [2, 3].Sign up for free to access walkthroughs, AI-generated questions, and more.
Get Started Free