cs-041 — The Queue Reorder
| Field | Value |
|---|---|
| id | cs-041 |
| difficulty | Beginner |
| topics | lists, slicing |
| lecture | lecture-11 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | none |
| objective | state inputs, outputs, and assumptions, then solve a small instance by hand |
| solution | held by the instructor; discussed after the attempt |
Problem
Move the last k people of a queue to the front, preserving order. Slicing plus concatenation; discuss negative indices.
Your deliverable is the rearranged list for the worked instance, the slice arithmetic, and a note on what aliasing would change. If the problem is ambiguous anywhere, state the assumption you are making and carry on — a stated assumption is part of a good answer.
Examples
- Input: queue [A, B, C, D, E], k = 2 (last 2 to front)
- Output: [D, E, A, B, C]; k = 0 -> same; k = 5 -> same; k = 7 -> same (wrap)
Solve for the worked instance first. Then ask what would change if one number, name, or condition were replaced by something stranger.
What to notice
- q[-k:] + q[:-k] with k = k % n; test k=0 before celebrating (negative slice surprises).
- Slices are half-open: they make boundaries explicit instead of hidden in loop conditions.
- Aliasing is the difference between copying a list and copying a name.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Find the smallest change to the task that makes the slice solution wrong."
Constraints and assumptions
- Difficulty: Beginner (no prior experience assumed).
- Topics: lists, slicing.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes for a first attempt; partial answers with clear reasoning are worth more than silent struggling.
- Optional stretch (not required): describe an alternative approach and say when it beats the obvious one.
Hints and extension
- First nudge: identify the pattern the worked instance uses before computing anything.
- If stuck after three minutes, write the input/output line you expect and trace the smallest example by hand.
- After the reveal, attempt the extension below before the next lecture.
- Extension challenge: Convert your in-place solution to a copying one and compare the trade-offs.