What Is an Algorithm? Meaning, Types, and Examples

An algorithm is a finite list of clear steps that turns an input into an output. Every step must have one meaning, and the list must stop after a limited number of steps.

A recipe is the everyday version of the idea. It names the ingredients, gives ordered steps, and ends with a dish.

In computer science the word is stricter. The method must work for every valid input, not for one lucky case.

A worked example

Here is an algorithm that finds the largest number in a list.

  1. Set best to the first number.
  2. Read each remaining number in order.
  3. If that number is larger than best, set best to it.
  4. When the list ends, return best.

Give it the list 4, 9, 2. It sets best to 4, then to 9, then keeps 9, then returns 9.

The method reads each item once. For a list of n items it does about n comparisons.

The five properties of an algorithm

Textbooks list five properties. A method that misses any one of them is not an algorithm.

PropertyWhat it means
InputIt takes zero or more defined inputs.
OutputIt produces at least one result.
DefinitenessEvery step is exact and has one meaning.
FinitenessIt stops after a limited number of steps.
EffectivenessEach step is simple enough to carry out by hand.

A loop with no exit fails finiteness. A step that says "sort it somehow" fails definiteness.

The four main types of algorithms

Algorithms are grouped by the design method behind them. Four methods cover most problems you will meet in study and in interviews.

TypeCore ideaExamples
Brute forceTry every candidate answer.Linear search, bubble sort
Divide and conquerSplit the problem, solve each part, then join the parts.Merge sort, binary search
GreedyTake the best choice available right now.Huffman coding, activity selection
Dynamic programmingSolve each smaller case once, store it, reuse it.Knapsack, longest common subsequence

Brute force is the simplest and the slowest. It is still useful as a first working answer and as a check on a faster one.

Dynamic programming applies when the same smaller case appears again and again. Storing each answer once removes the repeated work.

Divide and conquer vs greedy

These two get mixed up often, so here is the split.

Divide and conquer breaks a problem into smaller copies of the same problem. It solves each copy and then combines the results.

Greedy never makes copies. It builds one answer, picks the best option at each step, and never changes a past choice.

QuestionDivide and conquerGreedy
SubproblemsSmaller copies of the same problemNone
Combine stepYes, the parts are mergedNo
Revisits a choiceNot neededNever
Always correctYes, when the split and merge are correctOnly when the greedy choice is provably safe
Common costO(n log n), as in merge sortO(n log n), mostly from sorting the input first

Greedy is shorter to write and often faster to run. It is also wrong on many problems.

Use it only when you can argue that the best local choice cannot block a better final answer.

Where machine learning algorithms fit

A machine learning algorithm uses the word in a wider sense. It is still a step-by-step method, but the steps adjust numbers inside a model.

Sorting gives the same output for the same input every time. Training gives a model whose output depends on the data it saw.

Linear regression, decision trees, k-means and gradient descent are the common names. They belong to a separate branch of the field.

How an algorithm is measured

Two costs matter: time and memory.

Time is counted in basic operations, not in seconds, because seconds depend on the machine. Memory is counted as extra storage beyond the input itself.

Both are written with Big O notation, which reports how the cost grows as the input grows.

NotationSteps for 1,000 itemsTypical source
O(1)1A hash map lookup
O(log n)about 10Binary search
O(n)1,000One pass over a list
O(n log n)about 10,000Merge sort
O(n squared)1,000,000Two nested loops

The gap between the last two rows is the reason sorting beats nested loops on large inputs.

How to Prepare

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Which is the easiest field in software engineering?
What questions are asked in the Uber interview?
What should a software engineer CV look like?
What does it mean to "program to an interface"?
What is negotiation in software engineering?
Why is it called mock interview?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.