Frontend focused interviews often don’t care about DSA at all.
And for those of us that remember studying DSA at school/college, all the examples felt purely algorithmic (for good reason), but there were hardly any examples or guidance on how the products we use every day leverage this concept.
“Will I ever need this?”
You’ve asked this a lot, haven’t you? 👀
Here are a few data structures that you can leverage in your React app today! 👇
Table of Contents
- Introduction
- Arrays: Your Go-to in State Management
- Objects and Hash Maps: Normalized Data Store for Efficiency
- Doubly Linked Lists: Navigation with Context
- Stacks: Undo/Redo Functionality with Immutable Behavior
- Queues: Managing Sequential API Calls
- Trees: Rendering Recursive Components
- Graphs: Building Complex Data Relationships and Navigation
- Conclusion
Related reading:
Going from SDE1 to SDE2, and beyond! 🚀 What it actually takes.
Jayant Bhawal for Middleware ・ Jun 10
1. Arrays 🧩: Your Go-to in State Management
Arrays are everywhere in React. If you need help understanding how .map() or .filter() work, you’re probably seeing this post a little too soon! But don’t worry—once you get comfortable with these array methods, you’ll see how essential they are for rendering lists, managing component states, and transforming data.
2. Objects and Hash Maps 🗺️: Normalized Data Store for Efficiency
In a React app, when you deal with a large collection of entities like users or posts, normalizing your data into objects (hash maps) can make reading and updating much more efficient. Instead of working with a deeply nested structure, you map entities by their IDs.
Example: Reading from a normalized store with IDs
const postsById = {
1: { id: 1, title: 'First Post', content: 'Content of first post' },
2: { id: 2, title: 'Second Post', content: 'Content of second post' }
};
const postIds = [1, 2];
function PostList() {
return (
<div>
{postIds.map(id => (
<Post key={id} post={postsById[id]} />
))}
</div>
);
}
function Post({ post }) {
return (
<div>
<h2>{post.title}</h2>
<p>{post.content}</p>
</div>
);
}
This pattern allows for efficient data access, especially with large datasets where updates or reads need to happen quickly without re-rendering the entire collection.
3. Doubly Linked Lists 🔗: Navigation with Context
Doubly linked lists are useful when you need context from both the previous and next elements—think of navigating a photo gallery where each image displays its neighboring images for reference. Instead of using an index, we'll store the current node directly in the component state.