Area 05 · Data structures and algorithms
Service counter queue with undo
A government service counter keeps a priority line and a regular line. Serve fairly under a practice rule, and let the clerk undo the last serve.
- Spec no.
- HO-10
- Level
- Exam-level
- Time box
- 1 h 30 min
- Reference
- Java · C#
- Grading
- Self-assessed · rubric is ours, not DICT's
01 Coverage
| Area | Sub-skills practised | Role |
|---|---|---|
| 05 Data structures and algorithms | Queues, Stacks | Primary |
| 06 Control structures | Conditional, Iteration | Also |
Sign up to start coding practice
Read the task freely. An account is required before editing and running a solution. Code runs also need an internet connection.
Sign up or sign in02 Requirements
- ARRIVE name P|R adds to the back of the priority or regular queue.
- SERVE: take the front of the priority queue, unless 2 priority people were served in a row and someone is waiting in regular; then serve regular. (This rule is made up for the exercise.) Print Serving name (priority|regular), or No one waiting.
- Push each serve onto a stack with enough information to undo it, including the consecutive-priority counter as it was.
- UNDO pops the stack, puts the person back at the front of their own line, restores the counter and prints Undo: name back to the front of line.
- STATUS prints Priority: ... | Regular: ... | Served: ... using - for an empty list.
03 Input and output
- One command per line
- One line per SERVE, UNDO and STATUS
ARRIVE Ana R
ARRIVE Ben P
ARRIVE Carla R
ARRIVE Dante P
ARRIVE Ella P
SERVE
SERVE
SERVE
UNDO
SERVE
STATUSServing Ben (priority)
Serving Dante (priority)
Serving Ana (regular)
Undo: Ana back to the front of regular
Serving Ana (regular)
Priority: Ella | Regular: Carla | Served: Ben, Dante, AnaMatch the sample exactly first, then write at least two test inputs of your own.
04 Edge cases to test
- SERVE with both lines empty
- UNDO with nothing served
- Only priority people waiting after two priority serves
- Lower-case commands
05 Starter outline and hints
priority, regular = deque; history = stack; run = 0SERVE: decide line → pop front → push (name, line, run) → update runUNDO: pop → push front → run = saved
Hint 1
Store the counter value inside each history entry; recomputing it later is error-prone.
06 Practice rubric
| Criterion | Sub-skills | Look for | Pts |
|---|---|---|---|
| Queues | Queues | FIFO order in both lines; the fairness rule exactly as written. | 30 |
| Undo stack | Stacks | LIFO undo that restores the person and the counter. | 30 |
| Control flow | ConditionalIteration | Clear command dispatch and empty-line handling. | 20 |
| Status output | Queues | Matches the sample format, including -. | 20 |
| Total | 100 | ||
Our own weighting for practice. It is not the DICT scoring scheme.
07 Self-check
Tick only what you have verified by running your own program.
Loading saved checks…
08 Reference solution
Both versions compiled and reproduced the sample output on Java 21 and .NET 8 when this site was built. Reference solutions are in Java and C#. The 2023 FAQ copy also lists C, C++ and Visual Basic 6.0 / .NET; the same structure carries over, but you will need your language's own file and database APIs.
Try the whole brief first. The reference is one correct approach, not the only one, and reading it early removes most of the practice value.
09 Exam-day habits
- Work without internet search to simulate exam conditions.
- Type the sample input yourself and compare your output line by line before you look at the reference.