Algorithms and Data Structures for GenAI
This is definitely a bonus lesson, as including it in the curriculum for studying AI Agents is a bit much. This knowledge will be useful to about 10% of students who will be writing agents from scratch. However, I believe that knowledge of algorithms and data structures (A&DS) will give you a lot - in architectural thinking, for designing advanced systems, optimizing complex workflows and multi-agents.
We have analyzed the 11 most popular algorithms and data structures used in AI Agents.
If you get stuck, use ChatGPT: "Explain this algorithm to me like I'm 5 years old"
Steps
0. Options for studying the material
-
I don't want to spend more than 15 minutes - watch one video and leave this module
-
Recommended path - Watch one theory of your choice, solve one problem using ChatGPT as a teacher
Graphs and genetic algorithms are needed to develop awareness, but are not required for study.
Prompts for ChatGPT
Prompt for solving problems:
You are a mentor who helps a student solve a problem from LeetCode. Use the Socratic method: do not give ready-made answers, but instead lead the student to the solution through questions. Help to understand the conditions of the problem, suggest the correct strategy, analyze edge cases, choose data structures and step by step lead to the solution. Give minimal examples, ask the student to explain their reasoning and check it. The goal is to develop thinking, not just solve the problem.
Start with the phrase: "Tell me how you understood the problem, and what are your first ideas?"
Prompt to explain the condition of the problem or algorithm:
Explain the TOPIC as if I were 5 years old. Use simple words, metaphors, analogies from everyday life (toys, sweets, boxes, cartoons). Avoid technical terms unless you explain them very simply. Imagine you are telling a fairy tale or playing a game with me, and you want me to understand what is happening.
0.1. 10 Key Data Structures We Use Every Day
10 Key Data Structures We Use Every Day - 8:42 video
1. Linked List
Where is it used in AI Agents?
- You can use linked lists to store and process data in AI agents. For example, you can use linked lists to store dialogue history, a list of tasks, or to store data about a subject area.
Video resources:
LeetCode tasks:
2. Array
Where is it used in AI Agents?
Used for processing chat messages, storing embeddings and feature vectors of models. Allows you to quickly work with batches of dialogues. Knowledge of arrays is the foundation for effective AI systems!
Video resources:
- (skip if you know arrays well) Arrays - Introduction to Data Structures
3. Hash Table
Where is it used in AI Agents?
Like a magic cabinet with key-drawers: we quickly find answers to frequently asked questions (like "how to reset your password?"), remember the context of the dialogue ("you asked about the weather") or cache popular recipes. They help to work faster than searching in a huge basket of toys!
Video resources:
LeetCode tasks:
- Design HashMap
- NeetCode video: Two Sum - Leetcode 1
- ✦ Group Anagrams
- ✦ Most Common Word
- Recommended video on an important topic for NLP: What is Levenshtein Distance?
- ✦✦ Edit Distance
4. Queue
Where is it used in AI Agents?
Like a taxi dispatcher 🚖: we order requests to the API, buffer messages under load, plan agent tasks. Guarantees stability and consistency of processing — the foundation for scalable systems!
Video resources:
LeetCode tasks:
5. Recursion
Where is it used in AI Agents?
Like a matryoshka doll 🪆: we process nested dialogues ("what if..."), traverse scenario trees (multi-level menu), parse complex requests into parts. Allows agents to "think" step by step — from general to details, while maintaining the context of the chain of reasoning!
Examples:
- Processing clarifying questions within the answer
- Generating chains of dependent tasks (scheduler)
- Recursive analysis of JSON structures from the API
- Automatic branching of dialogues by conditions
- Building hierarchies of knowledge (for example, topics → subtopics → facts)
Video resources:
LeetCode tasks:
6. Divide and Conquer
Where is it used in AI Agents?
Like a Swiss knife 🔪: we break complex tasks (analyzing 100+ pages of PDF, generating reports) into subtasks, process parts of the request in parallel, combine the results. Allows agents to "digest" gigabytes of data without overloading — your secret for working with Big Data!
Examples:
- Parallel processing of documents
- Hierarchical analysis of user goals
- Optimizing response time through parallelism
- Recursive refinement of request details
- Dividing code into modules (NLU → logic → response)
Video resources:
LeetCode tasks:
7. Sliding Window
Where is it used in AI Agents?
Like a moving radar 🚨: we analyze streaming data (chat histories, API logs), find patterns in real time, manage the context window for RAG search. Allows agents to "slide" through data without reloading — your key to processing long dialogues!
Examples:
- Searching for repetitions in user requests
- Dynamic caching of context (N last messages)
- Processing long texts in chunks for RAG
- Monitoring anomalies in streaming data
- Optimizing LLM tokens through the "relevance window"
Video resources:
LeetCode tasks:
8. Two Pointers
Where is it used in AI Agents?
Like synchronous scanners 🔍: we compare versions of answers, analyze dialogue history from both ends, optimize RAG search. Allows agents to find perfect matches in O(n) — your tool for instant answers!
Examples:
- Comparing different versions of prompts
- Analyzing chat context from start/end
- Searching for contradictions in user data
- Optimizing RAG through double search
- Collapsing duplicate messages
Video resources:
LeetCode tasks:
9. Map-Reduce
Where is it used in AI Agents?
Like an industrial combine 🌾: we process terabytes of data (chat logs, web scraping), distribute tasks between nodes, aggregate results. Online research agents use it for parallel analysis of 100+ sources — your key to Big Data!
Examples:
- Parallel processing of millions of requests
- Aggregating data from different APIs
- Statistics on the use of LLM tokens
- Analysis of large error logs
- Training models on distributed data
Video resources:
LeetCode tasks:
E10. Graphs ✦
Where is it used in AI Agents?
Graphs are the foundation of complex interactions: we manage dialogue scenarios through tree-like structures, analyze relationships between entities (people-events-locations), optimize request processing chains. Used in recommendation systems based on a knowledge graph and detection of cyclic dependencies in agent workflows.
Examples:
- Dialogue trees for scenarios
- Analysis of social connections in chats
- Optimization of processing routes
- Recommendations through a knowledge graph
- Detecting cycles in task chains
Video resources:
LeetCode tasks: