Choose a Representation

Only after state and operations: array or map, list or rows. What operations are frequent, what lookup is needed, does order matter, do duplicates matter, how large can it get — with the complexity annotated and DSA one link away.

Choosing a Representation

Only after the state and the operations are known can "array or map or rows?" be answered — because the answer is whichever fits the access pattern the operations describe, and before the operations exist there is no access pattern to fit.

Q · You know what the cart must remember and what can happen to it. How do you decide how to hold it in memory — and why is that question unanswerable any earlier?
Array Cart vs Map Cart
▶ lab

The same cart held two ways: an array, where adding an existing product is a scan, and a map keyed by product id, where it is a key lookup. Simplicity, lookup, ordering, serialisation and memory each move, and for ten items none of the movement is visible.

Q · Two engineers implement the same cart, one with an array and one with a map. Both pass every test. Which one is right, and what would have to change for the other to be?
Why This Data Structure?
▶ lab

Five questions — what operations are frequent, what lookup is needed, does order matter, do duplicates matter, how large can it get — produce a recommendation with the case where it flips. The answer is derived, not remembered.

Q · A reviewer asks "why is this a map?" and you do not have an answer beyond "it seemed right". What five questions would have produced the answer, and how do you run them on a concept you have never seen?
Complexity, Annotated Not Asserted

addItem on an array is O(n) because it scans; on a map O(1) average because it hashes. Write that beside the operation where it is true, link the structure that explains it, and say the honest thing: for ten items the difference is invisible.

Q · Someone says "this should be O(1)". Where does that claim belong, how do you check it, and when is it true and still irrelevant?
Order, Duplicates, Size

Three questions flip the representation on their own: does order mean something, must each thing appear once, and what bounds how large it gets. Each has a case where the cart's answer changes and the array stops fitting.

Q · The cart is an array and it works. Which three changes to the requirements would make that the wrong choice, and how do you notice them arriving?
Multiple Correct Solutions

The array cart and the map cart both pass every test; so does a cart that filters instead of finds. A reference solution is not the right answer — it is one answer whose trade-offs are explained, and the ability to say why yours differs is the understanding.

Q · Your implementation differs from the reference. Is yours wrong, is the reference wrong, or are you looking at two correct answers — and how would you tell?