> For the complete documentation index, see [llms.txt](https://blog.indrajitvijayakumar.in/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.indrajitvijayakumar.in/data-structures/introduction-to-data-structures.md).

# Introduction to Data Structures

Data structures is a <mark style="background-color:green;">**way of**</mark> <mark style="background-color:green;">**organizing and storing data in a computer**</mark> <mark style="background-color:green;"></mark><mark style="background-color:green;">so that it can be</mark> <mark style="background-color:green;"></mark><mark style="background-color:green;">**accessed and used efficiently.**</mark>

## Why do we need Data Structures?

* Data structures are needed because they **provide efficient ways to organize and manipulate data, which is crucial for optimizing memory usage, improving algorithm efficiency, and enhancing overall application performance.**
* Data structures are fundamental to computer science and software development, enabling the creation of scalable and efficient systems.

## Advantages of using Data Structures

* **Efficient Data Storage**
  * Data structures facilitate the efficient storage and access of data, reducing memory usage and maximizing available resources.
* **Improved Algorithm Efficiency**
  * Well-designed data structures can significantly enhance the performance of algorithms by providing faster data access and manipulation.
* **Better Data Organization**
  * Data structures provide a way of organizing data in a meaningful and efficient manner, making it easier to access and manipulate. They allow developers to organize their data and present it in a logical manner, resulting in more manageable programs.
* **Code Reusability**
  * Data structures enable the creation of reusable code components that can be utilized across different projects, saving development time and effort. Reusable data structures can be used in many different applications, reducing the time and effort required to write and maintain code.
* **Problem Solving**
  * Data structures enable programmers to solve complex problems efficiently by providing the right tools for organizing and manipulating data effectively. They provide a way of modeling real-world problems and solving them in a more efficient and elegant manner.

## Different kinds of Data Structures

### Linear data structures

* A data structure in which **data elements are arranged sequentially or linearly, where each element is attached to its previous and next adjacent elements**, is called a linear data structure.
* *Examples: array, stack, queue, etc.*
  * **Static data structure**&#x20;
    * **Static data structure has a fixed memory size**. It is easier to access the elements in a static data structure. 
    * *Example: static array*
  * **Dynamic data structure**
    * **In a dynamic data structure, the size is not fixed**. The size can be randomly changed during runtime which may be considered efficient concerning the memory (space) complexity of the code <mark style="background-color:blue;">\[Only required amount of memory can be used, thereby reducing memory wastage]</mark>. 
    * *Examples: dynamic array, linked list, stack and queue*

### Non-linear data structures

* Data structures where **data elements are not placed sequentially or linearly** are called non-linear data structures.
* *Examples: tree, graph etc.*

<figure><img src="/files/twCIl9QIsJ4NeSyGMj08" alt=""><figcaption><p>Different kinds of data structures</p></figcaption></figure>

## Top 8 Data Structures for coding interviews

Reference - [Top 8 Data Structures - Neetcode](https://neetcode.io/courses/lessons/8-data-structures)

1. **Arrays**
   * The most fundamental data structure. Arrays are stored contiguously in memory.
2. **Linked Lists**
   * These also store an ordered list of elements, but the values are not contiguous in RAM. Linked list nodes have pointers connecting them.
3. **HashMaps**
   * Probably the most useful data structure as well. They use a hash function to map keys to indexes within an array. This allows us to have an amortized time of O(1) for most operations.
4. **Queues**
   * Typically used to process elements in the same order as they are added. These follow a FIFO (first-in first-out approach). Queues can be double-ended, which allow you to add and remove elements from both the front and the back.
5. **Trees**
   * A Binary Tree is an ordered tree data structure where each node is connected to at most two more nodes (called the left and the right child). Being ordered means we can perform DFS and BFS in O(log n) time.
6. **Tries/Prefix Trees**
   * Here, each node represents a character in the alphabet and can have at most 26 children. This allows us to save space when inserting words. Tries are useful for auto-complete features and allow us to search for words using their prefix.
7. **Heaps**
   * Heaps are a nearly complete binary tree, with potentially the last level being not full. They are implemented with arrays under the hood. Heaps serve as priority queues, and always have the smallest (min-heap) or the largest (max-heap) item stored at the root node.
8. **Graphs**
   * A graph is a bunch of nodes (vertices) connected by edges. Linked Lists and Trees are also special type of graphs, but a more general graph can have an arbitrary number of vertices connected to it. A good way to represent graphs in interviews is through an adjacency list.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://blog.indrajitvijayakumar.in/data-structures/introduction-to-data-structures.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
