FMTechnical notesPortfolio
Back to the blog

Computer Science · Beginner

Data structures: choosing where information lives

The right structure makes a frequent operation simple. The wrong one forces you to traverse, copy, or synchronize data constantly.

After reading this
  • Compare frequent operations
  • Choose by access and update patterns
  • Understand stacks, queues, and maps
01 / 03

Ask the right question

Do not ask which structure is best. Ask which operation you need most often: index access, key lookup, tail insertion, or arrival order.

  • Array: order and index access.
  • Set: unique values.
  • Map: key lookup.
  • Stack: last in, first out.
  • Queue: first in, first out.
02 / 03

A cart and an index

A cart can be an array because order matters. An id index can be a Map because finding without scanning matters.

js
const productsById = new Map(
  products.map((product) => [product.id, product])
);

productsById.get('prod-42');
03 / 03

Every choice has a cost

Two structures can speed up reads but must stay synchronized on writes. Measure the dominant operation before adding indexes.

Structure picker● interactive
ARRAYSETMAPQUEUE
Practice

Choose the structure

Design browser navigation history and a printer job queue.

Show a hint

History returns to the latest state; printers respect arrival order.