An algorithm is a step-by-step set of instructions for solving a problem or accomplishing a task. Think of it like a recipe: given some inputs (ingredients), follow a series of steps to produce an output (the dish).
A Simple Example
Let's say you want to find the largest number in a list. Here's an algorithm to do it:
Start with the first number and call it "largest so far"
Look at each remaining number one by one
If the current number is bigger than "largest so far", update it
After checking all numbers, "largest so far" is your answer
Why Do Algorithms Matter?
1. Efficiency Not all solutions are equal. An algorithm that takes 1 second is better than one that takes 1 hour for the same problem. In interviews, you're often asked to find the most efficient solution.
2. Scalability A solution that works for 10 items might be too slow for 10 million. Understanding algorithms helps you write code that scales.
3. Problem-Solving Framework Algorithms give you patterns and techniques to approach new problems. Once you learn common patterns, many problems become variations of things you've seen before.
Time Complexity: Measuring Efficiency
We measure how fast an algorithm runs using Big O notation. It describes how the runtime grows as the input size increases.Common Time Complexities (fastest to slowest):
O(1) - Constant: Same time regardless of input size (e.g., accessing an array element)
O(log n) - Logarithmic: Halves the problem each step (e.g., binary search)
O(n) - Linear: Time grows proportionally with input (e.g., finding max in a list)
O(n²) - Quadratic: Nested loops over input (e.g., bubble sort)
O(2ⁿ) - Exponential: Doubles with each addition to input (e.g., naive recursive fibonacci)
Space Complexity
Algorithms also use memory. Space complexity measures how much extra memory an algorithm needs as input grows.
O(1) space: Uses a fixed amount of extra memory
O(n) space: Extra memory grows with input size
Often there's a tradeoff: you can make an algorithm faster by using more memory, or save memory by accepting slower runtime.
Algorithmic Thinking
When faced with a problem, ask yourself:
What are the inputs and outputs? - Be crystal clear about what you're given and what you need to produce.
What's the brute force solution? - Start simple. Even if it's slow, it helps you understand the problem.
Can I do better? - Look for patterns, redundant work, or data structures that could help.
What are the edge cases? - Empty input? Single element? Negative numbers? Duplicates?
Common Algorithm Categories
Throughout this guide, you'll learn algorithms in these categories:
Searching - Finding elements (binary search, BFS, DFS)
Sorting - Ordering elements (merge sort, quick sort)
Two Pointers - Using multiple pointers to traverse data
Sliding Window - Processing contiguous subarrays
Recursion & Backtracking - Breaking problems into smaller subproblems
Dynamic Programming - Optimizing by storing intermediate results
Graph Algorithms - Traversing and analyzing connected data
Next Steps
Now that you understand what algorithms are, the next step is to learn about data structures - the containers that hold your data and make certain operations efficient. Together, algorithms and data structures form the foundation of computer science and coding interviews.