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! ?
Related reading:
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.
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 ({postIds.map(id => (); } function Post({ post }) { return ())} ); }{post.title}
{post.content}
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.
Doubly linked lists are useful when you needcontextfrom both the previous and next elements—think of navigating aphoto gallerywhere each image displays its neighboring images for reference. Instead of using an index, we'll store the current node directly in the component state.
Example:Doubly linked list for navigation between elements with context
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } add(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } } const imageList = new DoublyLinkedList(); imageList.add({ id: 1, src: 'image1.jpg', alt: 'First Image' }); imageList.add({ id: 2, src: 'image2.jpg', alt: 'Second Image' }); imageList.add({ id: 3, src: 'image3.jpg', alt: 'Third Image' }); function Gallery() { const [currentNode, setCurrentNode] = useState(imageList.head); return ({currentNode.prev && ( )} {currentNode.next && ( )}); }
In this React component:
Stacks allow you to manageundo/redooperations efficiently usingLast In, First Out(LIFO) logic. By using immutable operations (concat, slice), we can ensure state remains unmutated.
Example:Undo/Redo with immutable push and pop
const [undoStack, setUndoStack] = useState([]); const [redoStack, setRedoStack] = useState([]); const [formState, setFormState] = useState({ name: '', email: '' }); const updateForm = (newState) => { setUndoStack(prev => prev.concat([formState])); // Immutable push setRedoStack([]); // Clear redo stack setFormState(newState); }; const undo = () => { if (undoStack.length > 0) { const lastState = undoStack.at(-1); setUndoStack(prev => prev.slice(0, -1)); // Immutable pop setRedoStack(prev => prev.concat([formState])); // Move current state to redo setFormState(lastState); } }; const redo = () => { if (redoStack.length > 0) { const lastRedo = redoStack.at(-1); setRedoStack(prev => prev.slice(0, -1)); // Immutable pop setUndoStack(prev => prev.concat([formState])); // Push current state to undo setFormState(lastRedo); } };
Queues operate in aFirst In, First Out(FIFO) manner and are great for ensuring that tasks like API calls or notifications are processed in the correct order.
Example:Queueing API calls
const [apiQueue, setApiQueue] = useState([]); const enqueueApiCall = (apiCall) => { setApiQueue(prevQueue => prevQueue.concat([apiCall])); // Immutable push }; const processQueue = () => { if (apiQueue.length > 0) { const [nextCall, ...restQueue] = apiQueue; nextCall().finally(() => setApiQueue(restQueue)); // Immutable pop } };
Trees are commonly used in React when dealing with nested components likecomment threads,folder structures, ormenus.
Example:Rendering a comment tree recursively
const commentTree = { id: 1, text: "First comment", children: [ { id: 2, text: "Reply to first comment", children: [] }, { id: 3, text: "Another reply", children: [{ id: 4, text: "Nested reply" }] } ] }; function Comment({ comment }) { return (); }{comment.text}
{comment.children?.map(child => ())}
Another popular post that might be relevant to you:
Example 1:Routing between multiple views
You can represent routes between pages as a graph, ensuring flexible navigation paths in an SPA.
const routesGraph = { home: ['about', 'contact'], about: ['home', 'team'], contact: ['home'], }; function navigate(currentRoute, targetRoute) { if (routesGraph[currentRoute].includes(targetRoute)) { console.log(`Navigating from ${currentRoute} to ${targetRoute}`); } else { console.log(`Invalid route from ${currentRoute} to ${targetRoute}`); } }
Example 2:User relationship modeling
Graphs are perfect for modelingsocial connectionsor any kind of relationship where multiple entities are interconnected.
const usersGraph = { user1: ['user2', 'user3'], user2: ['user1', 'user4'], user3: ['user1'], user4: ['user2'] }; function findConnections(userId) { return usersGraph[userId] || []; } console.log(findConnections('user1')); // Outputs: ['user2', 'user3']
Note: We use graphs to show reviewer dependencies in Middleware.
Those DSA classes might have felt abstract back in the day, but data structures are powering the world around you in React.
Objects, stacks, queues, linked lists, trees, and graphs are more than just theory — they’re the backbone of the clean, efficient, and scalable apps you build every day.
So the next time you manage state in a queue or handle complex UI logic, remember: you’ve been training for this since school. ?
Let me know which data structures you’ve been using the most!
The above is the detailed content of Frontend Dev + Data Structures & Algorithms: How DSA Can Power Your React App ⚡. For more information, please follow other related articles on the PHP Chinese website!