Practice

Queue Run-Length Compression

2025/Oct/Nov·Variant 3·Q2·[14 marks]
HARDQueues

Binary digits "0" and "1" are stored in a linear queue of strings. QueueHead and QueueTail start at -1. NumberItems starts at 0. The queue can hold up to 20 digits.

Write:

  • function Enqueue(Value) — insert if there is space and return TRUE; return FALSE if full
  • function Dequeue() — return the next digit, or "False" if empty
  • procedure Compress() — dequeue every digit and build a compressed string that stores each run as the digit followed by how many times it appeared consecutively. Store the result in global NewString and output it.

You can assume no run is longer than 9, and the queue is never empty when Compress() runs.

Input: A count, then that many digits (each 0 or 1). Output: One compressed string.

Example:

Input:  7
        1
        1
        0
        0
        0
        1
        1
Output: 120312

(two 1s, three 0s, two 1s)

Premium is coming soon. All grading features are currently unlocked.

Sample Test Cases

Test 1: Two-three-two runs
Inputs: 7, 1, 1, 0, 0, 0, 1, 1
Expected: TRUE TRUE TRUE TRUE TRUE TRUE TRUE 120312
Test 2: Single run of ones
Inputs: 4, 1, 1, 1, 1
Expected: TRUE TRUE TRUE TRUE 14