Skip to content
Katabench
Try free

A Preview Must Not Consume Work

Easy About 15 min Free, no account needed

Give queue observers a read-only capability and keep consumption behind an explicit command; repeated previews must leave the next item unchanged.

The brief

A queue preview should answer what comes next without doing the work. The current QueuePreview reaches into WorkQueue.Items, and the composition root removes items through the same public list. Any preview client can accidentally consume or rewrite the queue because observation and mutation share one exposed representation.

Refactor the types in Work while preserving Run(int[] items) output. Inputs contain nonnegative work ids. The result is before:<next>|after:<next> around one consumption, with empty when no item exists. Consumption on an empty queue is a no-op. Never mutate the input array.

Introduce IQueueView with int? Peek(). WorkQueue implements that read-only capability and separately exposes void Consume() and a get-only int Count. QueuePreview.Render accepts only IQueueView; it must neither depend on the concrete queue nor gain access to its backing collection. Keep the queue sealed and expose no mutable collection or public setter. The root owns consumption and creates the queue.

The distinction is observable: repeated Peek and Render calls must return the same item until Consume is explicitly called. A trusted caller previews twice, consumes once, and previews again. An interface with a consuming Peek does not satisfy the contract.

How it is graded

Executable structural checks grade the shape of your types as well as their behavior.

SOLID principles exercises →

Related guides

What it practices

Command-query separation
Methods either change state or answer a question, never both.
Encapsulation
Protect invariants: no exposed mutable collections, shared static state or train-wreck navigation.

Related katas