Ace FAANG Interviews
Data Structures
The Building Blocks of Code
When you're solving a coding problem, the data you're working with needs a place to live. A data structure is just a way of organizing and storing data so you can access and modify it efficiently. Choosing the right one is like choosing the right tool for a job. You wouldn't use a hammer to turn a screw.
When preparing for software engineering interviews, particularly at companies like Google, Amazon, Microsoft, or Meta, data structures form the foundation of almost every technical problem you'll encounter.
Let's start with the most fundamental data structure of all: the array.
Array
noun
A data structure that stores a collection of elements, each identified by at least one array index or key. Elements are stored in contiguous memory locations.
An array is like a numbered row of boxes. You can put one item in each box, and you can instantly access any box if you know its number (its index). This direct access is super fast, which is the array's main advantage. In most languages, array indices start at 0.
So, if you have an array of numbers [10, 20, 30, 40, 50], the number 10 is at index 0, 20 is at index 1, and so on. Accessing array[2] immediately gives you 30.
The downside? Arrays have a fixed size. When you create one, you decide how many boxes it has. If you need more space, you have to create a new, bigger array and copy everything over. Adding or removing an element in the middle is also slow because you have to shift all the subsequent elements.
# A simple array (or list in Python) of integers
my_array = [10, 20, 30, 40, 50]
# Accessing an element by its index (fast!)
print(my_array[2]) # Output: 30
# Adding an element to the middle requires shifting (slower)
# Let's insert 25 at index 2
my_array.insert(2, 25) # my_array is now [10, 20, 25, 30, 40, 50]
What if you need a structure that can easily grow and shrink? That's where linked lists come in.
Linked List
noun
A linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next.
A linked list is a chain of nodes. Each node contains a piece of data and a pointer to the next node in the chain. The first node is called the head, and the last node points to null, signaling the end of the list.
Because nodes can be scattered anywhere in memory, linked lists are incredibly flexible. Adding or removing a node is as simple as changing a couple of pointers. No need to shift elements around. The tradeoff is that you can't jump directly to the middle of the list. To find the 100th element, you have to start at the head and follow the pointers 99 times.
Rules for Access
Some data structures aren't defined by their underlying implementation (like an array or linked list) but by the rules they enforce for adding and removing data. Stacks and queues are the two most common examples. They are abstract data types, meaning we care more about what they do than how they are built.
Stack
noun
A data structure that follows the Last-In, First-Out (LIFO) principle. The last element added to the stack will be the first one to be removed.
A stack works like a stack of plates. You can only add a new plate to the top, and you can only take a plate from the top. The last plate you put on is the first one you take off. This is called Last-In, First-Out (LIFO).
The main operations are:
- Push: Add an element to the top.
- Pop: Remove the element from the top.
- Peek (or Top): Look at the top element without removing it.
Stacks are used for things like the 'undo' functionality in a text editor or managing function calls in a program.
The cousin of the stack is the queue.
Queue
noun
A data structure that follows the First-In, First-Out (FIFO) principle. The first element added to the queue will be the first one to be removed.
A queue is like a line at a checkout counter. The first person to get in line is the first person to be served. This is called First-In, First-Out (FIFO).
The main operations are:
- Enqueue: Add an element to the back of the line.
- Dequeue: Remove the element from the front of the line.
- Peek (or Front): Look at the front element without removing it.
Queues are perfect for managing tasks in order, like print jobs, requests to a web server, or breadth-first search in graphs.
Structures for Fast Lookups
Imagine you need to store a massive phone book and look up numbers instantly. An array would be slow (you'd have to search through it), and a linked list would be even slower. For this, we need a hash table.
Hash Table
noun
A data structure that implements an associative array abstract data type, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.
A hash table stores data as key-value pairs. For our phone book, the key would be a person's name, and the value would be their phone number.
Here's the magic: it uses a special hash function to convert the key (the name) into an array index. For example, a hash function might take "Lisa Smith" and compute the index 4. The program then stores Lisa's number in the array at index 4.
When you want to look up Lisa's number, you just run her name through the hash function again. It gives you 4, and you jump directly to that index. This makes lookups, insertions, and deletions incredibly fast on average, with a time complexity of .
Sometimes, two different keys can produce the same index. This is called a collision. There are various ways to handle this, such as storing a linked list of items at that index, but the core principle of fast access remains.
Hierarchical and Networked Data
Not all data is linear. Sometimes data has relationships that are hierarchical or form a complex network. For these cases, we use trees and graphs.
Think of a file system on your computer. You have a root directory, folders within folders, and files inside those. That's a tree structure.
A tree is a data structure made of nodes with a parent-child relationship. It starts with a single top node called the root. Each node can have zero or more child nodes. Nodes with no children are called leaf nodes. A very common type is the binary tree, where each node has at most two children.
Trees are essential for representing anything with a hierarchy, like an organization chart, the DOM in HTML, or even suggestions in an auto-complete feature (using a special kind of tree called a Trie).
Finally, we have graphs. What if the relationships aren't strictly parent-child? What if any node can connect to any other node?
Graph
noun
A data structure consisting of a set of vertices (or nodes) and a set of edges that connect pairs of vertices.
A graph is a collection of nodes (called vertices) and the connections between them (called edges). Think of a social network: each person is a vertex, and a friendship is an edge connecting two vertices.
Edges can have a direction (e.g., on Twitter, you can follow someone without them following you back—a directed graph) or be mutual (like a friendship on Facebook—an undirected graph). They can also have a weight, representing a cost or distance, like on a map showing cities and the miles between them.
Graphs are incredibly powerful and model all sorts of real-world systems, from the internet to airline routes to dependencies in a project.
Let's review these fundamental concepts.
Ready to test your knowledge? Give these questions a try.
You are building a feature to manage print jobs. The first job sent to the printer should be the first one printed. Which data structure best models this requirement?
What is the primary advantage of using an array over a linked list?
Understanding these data structures—their strengths, weaknesses, and how they operate—is the first major step toward acing technical interviews and writing efficient, effective code.