🧱 What are Data Structures & Algorithms?

Every program you've ever used β€” Google Search, Instagram, Uber, M-Pesa β€” works because someone chose the right way to organize data and the right procedure to process it.

Data structures are ways to organize and store data so it can be accessed and modified efficiently. Think of them like containers: a list is a row of boxes, a dictionary is a phonebook, a tree is a family tree, a graph is a road map.

Algorithms are step-by-step procedures to solve specific problems. Searching for a name in a phonebook? That's an algorithm. Sorting a playlist by most played? Also an algorithm. Navigating from Nairobi to Mombasa on Google Maps? A graph algorithm.

Together, data structures and algorithms form the foundation of every software system. Choose the wrong data structure and your app crawls. Pick the wrong algorithm and your server crashes under load.

Common data structures you'll learn

  • Arrays / Lists: Ordered collection of items. Fast to access by index, slow to insert in the middle.
  • Hash Maps / Dictionaries: Key-value pairs. Lightning-fast lookups β€” O(1) on average.
  • Stacks & Queues: LIFO (last in, first out) and FIFO (first in, first out). Think undo history and printer queues.
  • Trees: Hierarchical structures. File systems, org charts, HTML DOM β€” all trees.
  • Graphs: Networks of connected nodes. Social networks, maps, the internet itself.

Common algorithms you'll learn

  • Searching: Binary search, BFS, DFS β€” finding things efficiently.
  • Sorting: Merge sort, quick sort β€” putting things in order.
  • Dynamic Programming: Breaking big problems into overlapping subproblems.
  • Greedy Algorithms: Making the locally optimal choice at each step.

The key insight: DSA isn't about memorizing code β€” it's about learning to think systematically about problems. Once you master the patterns, you can solve problems you've never seen before.

🎯 Why DSA matters for interviews

FAANG companies (Google, Meta, Amazon, Microsoft, Apple) and top startups test DSA in their technical interviews. It's the universal language of technical hiring β€” regardless of what programming language you use or what role you're applying for.

It's not just Silicon Valley. Companies across India (Flipkart, Swiggy, Razorpay, Zerodha) and Africa (Andela, Paystack, Flutterwave) test DSA too. If you want a software engineering role at any top company, you need to speak this language fluently.

Why do companies test DSA?

  • Problem-solving ability: Can you break down a complex problem into smaller pieces?
  • Code quality: Can you write clean, correct, efficient code under pressure?
  • Communication: Can you explain your thinking clearly?
  • Optimization mindset: Can you recognize when a solution is too slow and improve it?
🏒
Interview Scenario
You're in a Google interview

The interviewer says: "Given a list of numbers, find two that add up to a target. Return their indices."

This is the Two Sum problem β€” the most famous interview question in the world. Over 10 million developers have attempted it on LeetCode. By the end of this lesson, you'll solve it two different ways.

How do you approach this? You don't just start coding. You follow a framework.

🧠 The problem-solving framework

Every great DSA solution follows these 5 steps. This framework works for any interview problem, not just Two Sum.

Step 1: Understand the problem

Read the problem carefully. Ask clarifying questions. What are the inputs? What are the outputs? What are the constraints?

For Two Sum: Given a list of integers nums and an integer target, return the indices of two numbers that add up to target.

Step 2: Work through examples

Before writing any code, trace through concrete examples by hand.

Example: nums = [2, 7, 11, 15], target = 9. We need two numbers that sum to 9. Check: 2 + 7 = 9. Answer: indices [0, 1].

Step 3: Brute force first

Always start with the simplest solution β€” even if it's slow. A working slow solution is infinitely better than no solution. For Two Sum: check every possible pair of numbers.

Step 4: Optimize

Once you have a working brute force, ask: "Can I do better?" Look for repeated work. Use a smarter data structure. For Two Sum: instead of checking every pair, use a hash map to remember numbers you've already seen.

Step 5: Code it up

Now β€” and only now β€” write the code. Test it with your examples. Handle edge cases.

πŸ’‘ Interview tip

In a real interview, talk through steps 1-4 before writing any code. Interviewers want to see your thought process, not just the final answer. A candidate who explains their thinking while writing a brute force solution often scores higher than one who silently jumps to an optimal solution.

⏱️ Time vs Space complexity (preview)

Before we code the two solutions, you need to understand one critical concept: not all solutions are created equal.

Time complexity: How fast is your solution? As the input grows, how much longer does it take?

Space complexity: How much memory does your solution use? As the input grows, does it need more storage?

The trade-off: You can often make code faster by using more memory, or use less memory at the cost of speed. The Two Sum problem is a perfect example β€” the brute force uses almost no extra memory but is slow. The hash map approach is fast but uses extra memory.

We'll cover Big O notation in depth in the next lesson. For now, just know:

  • O(nΒ²) means "for every element, check every other element" β€” slow for large inputs
  • O(n) means "go through the list once" β€” fast even for huge inputs

Is your solution fast enough? Does it use too much memory? These are the questions every interviewer asks β€” and every senior engineer thinks about daily.

🐒 Code: Brute force Two Sum

The brute force approach checks every possible pair of numbers. For each number, we look at every other number after it. If they sum to the target, we return their indices.

This is O(nΒ²) β€” two nested loops, each going through the list. For a list of 10,000 numbers, that's 100 million comparisons.

Python brute_force_two_sum.py
Output
[0, 1] [1, 2]

It works! But it's slow. For each element at index i, we scan every element after it at index j. That's roughly n * (n-1) / 2 comparisons β€” O(nΒ²).

Why start here? In interviews, a correct brute force solution is always better than an incorrect optimized one. Get it working first, then improve.

πŸš€ Code: Optimized Two Sum with hash map

The key insight: as we walk through the list, for each number we know what its complement would be (target - num). If we've already seen that complement, we're done. We use a Python dictionary (hash map) to remember every number we've encountered and its index.

Python optimized_two_sum.py
Output
[0, 1] [1, 2]

One pass through the list. O(n) time, O(n) space. For 10,000 numbers, that's 10,000 operations instead of 100 million. A 10,000x speedup.

How it works step by step

Let's trace through nums = [2, 7, 11, 15], target = 9:

  1. i=0, num=2: complement = 9 - 2 = 7. Is 7 in seen? No. Store {2: 0}.
  2. i=1, num=7: complement = 9 - 7 = 2. Is 2 in seen? Yes! Return [seen[2], 1] = [0, 1].

Done in just 2 steps. The brute force would have also found it quickly here, but imagine a list with millions of entries where the answer is near the end β€” the difference becomes massive.

Comparing the two approaches

βš–οΈ
Comparison
Brute force vs Hash map
  • Brute force: O(nΒ²) time, O(1) space. Simple, but slow for large inputs.
  • Hash map: O(n) time, O(n) space. Fast, but uses extra memory for the dictionary.
  • Trade-off: We spend O(n) extra memory to gain a massive speedup. In practice, memory is cheap β€” time is expensive.
  • Interview verdict: The hash map solution is what interviewers want to see. But always explain the brute force first to show your reasoning process.
πŸ’‘ The pattern

This "hash map to avoid repeated work" pattern shows up in dozens of interview problems. Master it here and you'll recognize it everywhere: duplicate detection, frequency counting, grouping, and more.

πŸ—ΊοΈ What you'll learn in this track

This DSA track is organized into 6 arcs, building from foundations to advanced topics. Every lesson uses Python, focuses on interview patterns, and includes hands-on coding.

  • Arc A β€” Foundations: Big O notation, arrays, strings, hash maps, two pointers, sliding window. The building blocks every interview requires.
  • Arc B β€” Linear Structures: Linked lists, stacks, queues, deques. Understand how data flows through sequential structures.
  • Arc C β€” Recursion & Trees: Recursion, binary trees, BSTs, heaps, tries. The recursive thinking that unlocks tree problems.
  • Arc D β€” Graphs: Graph representation, BFS, DFS, shortest path, topological sort. Model real-world networks and relationships.
  • Arc E β€” Advanced Patterns: Dynamic programming, greedy algorithms, backtracking. The techniques that separate good from great.
  • Arc F β€” Interview Prep: Mock problems, pattern recognition, time management. Put it all together for real interviews.

By the end of this track, you'll have solved 50+ classic problems and built a problem-solving toolkit that works at any company β€” from Flipkart in Bangalore to Andela in Nairobi to Google in Mountain View.

🐍 Try It Yourself

Now it's your turn. Implement the Two Sum function below. Start with brute force, then try the hash map approach. Click Run to test your solution with Pyodide (Python running in your browser).

🐍 Try It Yourself β€” Python
Output
Click β–Ά Run to execute...
Mini Challenge: Three Sum Thinking

Task: Think about how you'd extend Two Sum to Three Sum: given a list of numbers, find three numbers that add up to a target. You don't need to code it yet β€” just think through the framework:

  • What would the brute force look like? How many nested loops?
  • What would the time complexity be?
  • Can you reduce it by sorting the array first and using two pointers?

Write your thoughts as comments in the editor above, or sketch it on paper.

Hint: Brute force Three Sum uses 3 nested loops β€” O(nΒ³). But if you sort the array first (O(n log n)), you can fix one number, then use the Two Sum two-pointer technique on the remaining array. That brings it down to O(nΒ²). This is a classic optimization pattern: sort first, then use pointers.

Test your knowledge

Question 1 of 5
What is a data structure?
Question 2 of 5
What is the time complexity of the brute force Two Sum solution?
Question 3 of 5
In the optimized Two Sum, what data structure stores previously seen numbers?
Question 4 of 5
What is the first step in the problem-solving framework?
Question 5 of 5
Why does the hash map Two Sum run in O(n) time instead of O(nΒ²)?
0
/5
Great job!

Up next

Next lesson: Big O Notation β†’ β€” Learn to measure how fast your code runs and how much memory it uses. The language every interviewer speaks.

Africa & India's Best AI Academy
πŸ€– AI-first πŸŽ“ Placement Assistance

You just solved Two Sum.
Imagine what you'll build next.

Join the Best AI Programs β€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses β†’