What is a Data Structure? Resource - aloalgo

What is a Data Structure?

A data structure is a way of organizing and storing data so that it can be accessed and modified efficiently. Think of it like choosing the right container for your stuff: a bookshelf for books, a filing cabinet for documents, a queue for people waiting in line.The right data structure can make your algorithm fast. The wrong one can make it impossibly slow. That's why understanding data structures is essential for coding interviews.

Why Data Structures Matter

Different data structures excel at different operations:
  • Need to look up items by key instantly? Use a hash map.
  • Need to maintain sorted order? Use a tree or heap.
  • Need to process items in order? Use a queue.
  • Need to undo operations? Use a stack.
Choosing the wrong data structure can turn an O(1) operation into O(n), making your code thousands of times slower for large inputs.

Core Data Structures Overview

1. Array

A contiguous block of memory storing elements of the same type. Elements are accessed by index.
Use when: You need fast access by position, or you're iterating through elements in order.

2. Hash Map (Dictionary)

Stores key-value pairs with O(1) average lookup, insert, and delete. Uses a hash function to map keys to positions.
Use when: You need to quickly look up values by a key, count occurrences, or check if something exists.

3. Set

A collection of unique elements with O(1) lookup. Like a hash map but only stores keys, no values.
Use when: You need to track unique elements or quickly check membership.

4. Stack

Last-In-First-Out (LIFO) structure. Think of a stack of plates: you add and remove from the top only.
Use when: You need to reverse order, match parentheses, track state for backtracking, or implement undo.

5. Queue

First-In-First-Out (FIFO) structure. Think of a line at a store: first person in line is first to be served.
Use when: You need to process items in the order they arrived, like BFS traversal or task scheduling.

6. Linked List

A sequence of nodes where each node points to the next. Unlike arrays, elements aren't contiguous in memory.
Use when: You need efficient insertions/deletions in the middle, or the problem explicitly uses linked lists.

7. Tree

A hierarchical structure with a root node and children. Binary trees have at most 2 children per node. Binary Search Trees (BST) maintain sorted order.
Use when: Data has hierarchical relationships, you need sorted data with fast insert/delete, or the problem involves tree traversal.

8. Heap (Priority Queue)

A tree-based structure that maintains the min (or max) element at the root. Useful when you repeatedly need the smallest or largest element.
Use when: You need to repeatedly find the min/max, implement Dijkstra's algorithm, or solve "top K" problems.

9. Graph

A collection of nodes (vertices) connected by edges. Can be directed or undirected, weighted or unweighted.
Use when: Data has relationships between entities, like social networks, maps, or dependencies.

Quick Reference: Time Complexities

Data StructureAccessSearchInsertDelete
ArrayO(1)O(n)O(n)O(n)
Hash Map/SetO(1)O(1)O(1)O(1)
Stack/QueueO(n)O(n)O(1)O(1)
Linked ListO(n)O(n)O(1)O(1)
Binary Search TreeO(log n)O(log n)O(log n)O(log n)
HeapO(1)*O(n)O(log n)O(log n)
*Heap access is O(1) only for min/max element

Choosing the Right Data Structure

Ask yourself these questions:
  1. What operations do I need? - Frequent lookups? Insertions? Deletions? Finding min/max?
  2. What's the access pattern? - Random access by index? Sequential iteration? LIFO/FIFO?
  3. Do I need ordering? - Sorted order? Insertion order? No order needed?
  4. Are there duplicates? - Need to track counts? Only unique values?

Next Steps

Now that you understand the fundamental data structures, you're ready to dive into specific topics. Start with Arrays & Hash Maps - the most common data structures in coding interviews.
Was this helpful?
© 2026 aloalgo. All rights reserved.