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
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
8 tests, not run yet
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | First in, first out | Not run | |
| 2 | Full | Not run | |
| 3 | Empty | Not run | |
| 4 | Wrapping round | Not run | |
| 5 | Peek does not remove | Not run | |
| 6 | Capacity 1 | Not run | |
| 7 | Many rounds | Not run | |
| 8 | 200,000 slots | Not run |