Choose a Java collection from the operations and guarantees the caller needs. A list, a set and a map solve different questions: ordered positions, membership without duplicate elements under its equality rules, and lookup from keys to values. Naming a class is only the start of the design answer.

The example targets Java 21. It uses a deque for waiting work and a map for labels. Run the class with assertions enabled.

Why are waiting work and keyed lookup different?

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;

class Example {
    public static void main(String[] args) {
        Deque<String> waiting = new ArrayDeque<>();
        waiting.addLast("A");
        waiting.addLast("B");
        assert "A".equals(waiting.removeFirst());
        assert "B".equals(waiting.peekFirst());
        Map<String, String> labels = new HashMap<>();
        labels.put("A", "ready");
        labels.put("A", "held");
        assert labels.size() == 1;
        assert "held".equals(labels.get("A"));
    }
}

The deque models a chosen removal order. The map models one current value per key. Putting the same key again changes the associated value; it does not create a second positional row as a list insertion would.

Which questions should come before the class choice?

  • Must iteration preserve insertion order, sorted order or neither?
  • Can duplicate values occur, and what should they mean?
  • Are null elements or keys allowed by the chosen implementation?
  • Does the caller need positional access, membership or lookup?
  • Who owns mutation, and can several threads access the object?

These are not interchangeable properties. HashMap does not promise insertion-order iteration. ArrayDeque does not accept null elements. A design that depends on a behavior should name and verify the specific implementation's contract.

Is every List mutable?

No. The interface alone does not promise that every operation is supported by every instance. Factory methods and wrappers can produce lists with restrictions. A list view can also have different ownership behavior from an independent copy.

When discussing a returned collection, say whether a caller may modify it and whether changes are shared with another object. An unmodifiable view is not necessarily a snapshot of independently owned data.

What does a set use to recognize duplicates?

The answer depends on the implementation and its ordering/equality contract. A hash-based set relies on suitable equality and hashing. A sorted collection introduces ordering rules that must also be understood. Do not combine these into a claim that every set uses a hash table.

If a key or element's relevant comparison state changes during use, the structure may no longer support the intended lookup behavior. Stable value objects can simplify that boundary.

How should performance be explained?

Discuss the operations the workload uses and the documented assumptions behind their costs. Do not attach one complexity label to a whole collection. Fast lookup does not automatically mean fast ordered iteration, low memory cost or good behavior under concurrent access.

The tiny assertion example does not measure throughput. A benchmark claim would need a workload, JVM setup, warm-up strategy and actual observations. None are implied here.

How would the answer change for concurrency?

A plain HashMap is not a general solution to concurrent mutation. You would need a suitable ownership strategy, synchronization or a collection designed for the required concurrent operations. Even a concurrent collection does not automatically make a sequence of separate operations one atomic business action.

For practice, add a requirement that labels must appear in first-insertion order. Explain which part of the contract changes and choose a documented implementation for it. Changing one requirement should lead to a reasoned choice, not a new memorized favorite class.

References and next practice

docs.oracle.com — java maps

java question collection · Browse interview topics · Practise a question in Interview Challenge · How to Answer Technical Interview Questions Out Loud