Front–Middle–Back Queue

Implement FrontMiddleBackQueue with no constructor arguments and six methods: pushFront(value), pushMiddle(value), pushBack(value), popFront(), popMiddle(),…

Implement FrontMiddleBackQueue with no constructor arguments and six methods: pushFront(value), pushMiddle(value), pushBack(value), popFront(), popMiddle(), popBack(). The queue is ordered front-to-back. Before an insertion, pushMiddle inserts at index floor(length/2). Before a removal, popMiddle removes index floor((length-1)/2), choosing the frontmost center when length is even. Empty pops return -1 and leave the queue unchanged. Return one result per input record: null for construction and pushes, and the strict integer returned by each pop.

operations and arguments are parallel arrays of equal length. The first and only constructor record is FrontMiddleBackQueue with []. There are 0..1000 methods after construction: 1..1001 total records. Each push has one strict integer value in [1,1000000000]; each pop has no arguments. Only the six named methods are allowed. Boolean/fractional values and wrong arities are invalid. Duplicate values remain separate occurrences. The caller's input arrays need not be mutated. Teaching traces contain at most 12 total records; coding retains the full 1001-record domain.

Example 1: input operations = ["FrontMiddleBackQueue","pushBack","pushBack","pushBack","pushBack","popMiddle","pushMiddle","popMiddle"], arguments = [[],[10],[20],[30],[40],[],[25],[]]; output [null,null,null,null,null,20,null,25]. After inserting [10,20,30,40], popMiddle returns 20. Pushing 25 gives [10,25,30,40], whose frontmost middle is 25.

Example 2: input operations = ["FrontMiddleBackQueue","popFront","pushMiddle","pushBack","popMiddle","popBack","popMiddle"], arguments = [[],[],[7],[7],[],[],[]]; output [null,-1,null,null,7,7,-1]. Empty pops return -1. The two inserted 7s remain distinct occurrences and are removed one at a time.

Category: Queues. Topic: queue. Difficulty: Medium. Patterns: linear-scan.