Java traps · Chapter 6 of 16

Removing while iterating

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Everyday traps
  • 3 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Java traps are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Repro.java: a cancelled order left in the handover

    Write reproduce() in Repro.java. Make an ArrayList of three Sweep.Order records, in this order: SBC-10431 with status "ready", SBC-10432 "cancelled", SBC-10433 "cancelled". Give the list to Sweep.dropCancelled, then return the ids of the orders left in your list, in order, as a List<String>.

    The check runs your reproduce() twice: beside the original Sweep.java it must return ["SBC-10431", "SBC-10433"] (SBC-10433, cancelled, left in the list), and beside a fixed Sweep.java it must return ["SBC-10431"]. So it must really sweep your list and then look at it. No real waiting and no reading input: reproduce() must finish within 3 seconds.

  2. Task 2

    Sweep.java: every cancelled order leaves the handover

    Fix dropCancelled(orders) in Sweep.java so that it removes every order whose status is "cancelled" from the list it was given, the same list object (the handover screen holds it), keeps the other orders in their order, never throws, and returns how many it removed. It must work with cancelled orders side by side, three in a row, the first, the last, every order, none, and an empty list.

    Keep the log lines: sweep: dropped <id>, once for each order removed, and sweep: <n> cancelled removed, <m> handed over at the end. The check uses its own orders and looks at the list it passed in: a fix that builds a new list and leaves the caller's list as it was is as wrong as the bug.

  3. Task 3

    SweepTest.java: a test that would have caught it

    Write a JUnit test class, SweepTest.java, that would have caught this bug before a cancelled order reached the morning shift. The check runs your file three times: beside the original Sweep.java, where at least one test must fail; beside the fixed Sweep.java, where every test must pass; and beside a wrong fix that loops by index and calls remove(i), where at least one test must fail.

    So put two cancelled orders side by side, sweep the list, and check the list you passed in: no cancelled order is left in it and the others are, in order. No real waiting: each test must finish within 3 seconds.

What you will learn

  • Repro.java: a cancelled order left in the handover
  • Sweep.java: every cancelled order leaves the handover
  • SweepTest.java: a test that would have caught it

More in Everyday traps

Build "Removing while iterating" yourself

Tapri Counter runs on Java, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Java traps are free. This one comes with the all labs plan.