Contents

Computer Science › Data Structures

Data Structure

A way of organizing data so certain operations are efficient.

Also known as: data structures, DS, ADT, abstract data type

A data structure is a way of organizing data in memory so that certain operations on it are efficient. Choosing the right one is often the difference between a program that’s instant and one that crawls.

StructureGood atWeak at
Array / dynamic arrayaccess by position, appendinginserting in the middle, searching by value
Linked listcheap inserts and removals when you hold the nodeaccess by position
Hash table (dictionary, set)lookup, insert and delete by key, on average constant timeordered traversal
Stacklast-in, first-out (undo, call stack)anything else
Queuefirst-in, first-out (task lines)
Tree, binary search treeordered data, hierarchy, searching in log time
Heap / priority queuequickly getting the smallest or largestarbitrary lookup
Graphrelationships and networks

How to choose

Ask what you’ll do most:

  • Look things up by key? A dictionary.
  • Keep things ordered, or find ranges? A sorted structure or a tree.
  • Process in arrival order? A queue.
  • Check “have I seen this?” A set.
  • Always grab the next-most-urgent item? A priority queue.
seen = set()                 # O(1) membership instead of scanning a list
for item in items:
    if item in seen: ...

This is what Big O notation helps you compare.

Abstract vs concrete

A stack or queue is an abstract idea (what operations it supports). It can be built on top of an array or a linked list. Languages give you ready-made versions, so you rarely implement them yourself, but you must know their costs.

Practical advice

  • Learn what your language’s built-in types are good at.
  • Measure before worrying about tiny differences (premature optimization).
  • Interviews love these, and real code uses them constantly, mostly lists, dictionaries and sets.