STAQ-P4

A ring of platforms

Practice 4 of 6Stacks and queues8 tests

The problem

Build a queue of fixed size as a circular buffer (lesson 4). RingQueue(capacity) holds at most capacity items. When it is full, push refuses the item and returns False.

q = RingQueue(2)
q.push('a'); q.push('b')    # True, True
q.push('c')                 # False: full
q.pop()                     # 'a'
q.peek()                    # 'b'
len(q), q.is_full()         # 1, False

Store the items in one list of length capacity, made in __init__. collections.deque is switched off.

Limits

  • Capacity up to 200,000, and up to 600,000 calls in one test.

Builds on STAQ-4 Circular buffers.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

8 tests, not run yet
Test results
#TestResultReads · time
1First in, first outNot run
2FullNot run
3EmptyNot run
4Wrapping roundNot run
5Peek does not removeNot run
6Capacity 1Not run
7Many roundsNot run
8200,000 slotsNot run