KatInterview

Ring-Buffer Deque with Constant-Time Indexing

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designcircular-bufferdequedynamic-array

Last updated

Build a double-ended queue on a resizable circular array so index lookups are O(1), then replay a command list against it and report each result.

Replaying Commands on an Indexable Deque

A sequence container is needed that lets you add or remove items at either end and also read or overwrite any item by its position, all in constant time. You will build such a container yourself and then drive it with a list of commands.

The container

Elements are ordered from front to back, and positions are numbered from 0 at the front. The container supports these commands:

CommandArgumentsEffectValue produced
push_front[val]Put val before the current front elementNone
push_back[val]Put val after the current last elementNone
pop_front[]Remove the front elementthe removed value
pop_back[]Remove the last elementthe removed value
get[index]Read the element at position indexthat element
set[index, val]Overwrite the element at position index with valNone
size[]Count the stored elementsthe count

Intended cost targets for the container you design:

  • get, set and size take worst-case O(1) time.
  • push_front, push_back, pop_front and pop_back take amortized O(1) time.

Use a plain contiguous list as raw storage and manage the wrap-around and resizing logic yourself. Do not use collections.deque or any other built-in deque type to hold the elements.

The function to write

You are given two lists of equal length:

  • commands[i] is the name of the i-th command, one of the seven names above.
  • args_list[i] is the list of arguments for that command, exactly as shown in the table.

Starting from an empty container, run the commands in order. Return a list with one entry per command, where entry i is the value produced by command i. Commands that produce no value (push_front, push_back, set) contribute None.

Rules and edge cases

  • Indexing is 0-based from the front. Negative indices are not valid.
  • The tests only issue valid commands: no pop on an empty container and no get or set with an index outside 0 .. size-1. For robustness, a well-behaved implementation should still raise IndexError in those cases.
  • set changes only the value at that position. It does not change the size or the order of the other elements.
  • Pushes and pops at both ends can be freely interleaved, including a long run of push_front on a brand-new container.

Constraints

  • 1 <= len(commands) == len(args_list) <= 10^5
  • Stored values are integers in the range [-10^9, 10^9].
  • Every index passed to get or set satisfies 0 <= index < size() at that moment.
  • Every pop_front and pop_back is issued only when the container is non-empty.
  • Storage must be built from a plain contiguous array that you manage yourself, not a built-in deque type.

Examples

Example 1

Input: run_indexed_deque_commands(['push_back', 'push_back', 'push_front', 'get', 'get', 'get', 'set', 'get', 'pop_front', 'pop_back', 'size'], [[10], [20], [5], [0], [1], [2], [1, 15], [1], [], [], []])

Output: [None, None, None, 5, 10, 20, None, 15, 5, 20, 1]

Hints

Hint 1

Keep one array plus two integers: where the front element sits in the array, and how many elements are stored. Position i of the deque then lives at (front + i) modulo the array length.

Hint 2

Pushing or popping at the front only moves the front marker backward or forward by one slot (modulo the array length). Nothing needs to be shifted.

Hint 3

When the array is full, allocate one twice as large and copy the elements in logical order (not raw array order) starting at slot 0, then reset the front marker to 0. Optionally shrink when the array is at most a quarter full, so grow and shrink thresholds do not oscillate.