Optiver software engineer interviews cover algorithms, data structures, system design, and coding problems drawn from real interview rounds.
MessageScheduler. Each message has a globally unique identifier, a session identifier, a release timestamp, and an integer priority.Messages within a session form a FIFO queue in enqueue order. Only the first non-cancelled, undispatched message in each session may compete for dispatch. Consequently, a message whose release time has passed can still be blocked by an earlier message in its session.The scheduler's clock initially equals 0. Calling dispatch(now, limit) sets its clock to now; dispatch timestamps must be nondecreasing. Enqueueing and cancellation do not advance the clock.For each slot in a dispatch call:release_time <= now.limit messages or when no session head is eligible. Dispatch does not advance time beyond now, and there is no service duration.Cancellation removes a pending message logically. If it was a session head, the next non-cancelled message immediately becomes the head. Cancelling an unknown, already-cancelled, or already-dispatched identifier returns false.Support up to 200,000 operations efficiently. Do not scan every session on each dispatch or search linearly through a session to cancel a message.Function or class signatureMessageScheduler() — initialize an empty scheduler.enqueue(message_id: str, session_id: str, release_time: int, priority: int) -> Nonecancel(message_id: str) -> booldispatch(now: int, limit: int) -> list[str][ ["enqueue", "A1", "alpha", 10, 1], ["enqueue", "A2", "alpha", 0, 100], ["enqueue", "B1", "beta", 0, 5], ["dispatch", 0, 10], ["dispatch", 9, 10], ["dispatch", 10, 1], ["dispatch", 10, 10] ]Output, one result per operation:
[null, null, null, ["B1"], [], ["A1"], ["A2"]]Explanation: At time
0, A2 is blocked by A1, despite having priority 100 and release time 0. Only B1 can run. At time 9, A1 is still unreleased. The first call at time 10 dispatches A1 and reaches its limit; the next call dispatches A2.Example 2:Input, on a fresh scheduler:[ ["enqueue", "X1", "x", 20, 9], ["enqueue", "X2", "x", 5, 4], ["enqueue", "Y1", "y", 5, 4], ["enqueue", "Z1", "z", 3, 4], ["cancel", "X1"], ["dispatch", 5, 2], ["cancel", "X1"], ["dispatch", 5, 2] ]Output, one result per operation:
[null, null, null, null, true, ["Z1", "X2"], false, ["Y1"]]Explanation: Cancelling
X1 exposes X2. At time 5, the three session heads all have priority 4. Z1 wins first because its release time is 3; X2 wins next because it was enqueued before Y1, and both have release time 5. A second cancellation of X1 returns false.Constraints:200,000 total method calls and at most 200,000 enqueued messages.32 ASCII characters.0 <= release_time, now <= 10^18; -10^9 <= priority <= 10^9.0 <= limit <= 200,000; successive dispatch timestamps are nondecreasing.P messages are pending at any moment. How would you guarantee O(P + 1) retained space after every operation, while preserving efficient cancellation and dispatch even when cancelled messages have release times far in the future?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