Learn With Examples https://learnwithexamples.org/ Lets Learn things the Easy Way Mon, 27 Jul 2026 16:43:02 +0000 en-US hourly 1 https://wordpress.org/?v=7.0.2 https://i0.wp.com/learnwithexamples.org/wp-content/uploads/2026/07/cropped-learnwithexamples-icon.png?fit=32%2C32&ssl=1 Learn With Examples https://learnwithexamples.org/ 32 32 228207193 The Fundamental Theorem of Calculus — Explained with Real Examples https://learnwithexamples.org/fundamental-theorem-of-calculus/ https://learnwithexamples.org/fundamental-theorem-of-calculus/#respond Mon, 27 Jul 2026 16:43:01 +0000 https://learnwithexamples.org/?p=804 The Fundamental Theorem of Calculus — Explained with Real Examples Learn · With · Examples A car’s speedometer and odometer are secretly the two halves of the most important theorem…

The post The Fundamental Theorem of Calculus — Explained with Real Examples appeared first on Learn With Examples.

]]>
The Fundamental Theorem of Calculus — Explained with Real Examples

Learn · With · Examples

A car’s speedometer and odometer are secretly the two halves of the most important theorem in calculus. One measures a rate, the other accumulates it — and the Fundamental Theorem of Calculus is simply the precise statement of how differentiation and integration undo each other. This guide builds it from that one everyday example, with interactive demos, worked problems, and code.

📖 20 min read 📈 FTC Part 1 explorer 📐 FTC Part 2 explorer 🎬 Area accumulation demo ❓ 6-question quiz

Section 01

The Big Idea — Two Operations, One Relationship

For most of calculus, derivatives and integrals feel like two completely separate tools. A derivative measures how fast something is changing at an instant — a slope. An integral measures the total accumulation of something over an interval — an area. They seem to answer opposite kinds of questions.

The Fundamental Theorem of Calculus (FTC) is the discovery that they are not separate at all. They are inverse operations — the same relationship that addition has with subtraction, or multiplication has with division. Differentiate an integral and you get back the original function. Integrate a derivative and you get back the original function (up to a constant). This single fact is why calculus works as one unified subject instead of two disconnected topics.

💡 The Key Idea

Every accumulation (integral) has an accumulation rate (derivative), and that rate is just the original function you were accumulating. Differentiation undoes integration. That’s the entire theorem — everything else is precise bookkeeping around that one idea.

2
Parts to the theorem — one about derivatives of integrals, one about evaluating integrals
1670s
Decade Newton and Leibniz independently formalized this connection
F(b)−F(a)
The entire computational shortcut FTC Part 2 hands you
Rectangles you’d need to sum by hand without this theorem — FTC replaces that with 2 substitutions

Section 02

Formal Statement — FTC Part 1 and Part 2

If F(x) = ∫[a to x] f(t) dt,   then F'(x) = f(x)
FTC PART 1 — the derivative of an accumulation function gives back the original function
∫[a to b] f(x) dx = F(b) − F(a)
FTC PART 2 — where F is ANY antiderivative of f (F’ = f)

Part 1 tells you that integration and differentiation are inverse processes. Part 2 is the practical payoff: it hands you a way to compute the exact value of a definite integral without summing infinitely many rectangles — just find an antiderivative and plug in the two endpoints.

⚠ Continuity Requirement

Both parts require f to be continuous on the relevant interval. If f has a jump, a hole, or shoots to infinity somewhere in [a,b], the theorem doesn’t directly apply without extra care — this is why calculus courses spend so much time on continuity before ever reaching FTC.

Section 03

Interactive: FTC Part 1 — Differentiating Accumulation

Pick a function f(t). Watch its accumulation function F(x) get built via integration, then differentiated right back to f(x) — confirming Part 1 in each case.

  FTC Part 1 Explorer
Starting function: f(t) = t
Step 1 — Integrate: F(x) = ∫₀ˣ t dt = x²/2
Step 2 — Differentiate F(x): F'(x) = d/dx [x²/2] = x
F'(x) = x = f(x) ✓ — exactly matches the original function, confirming FTC Part 1
Starting function: f(t) = 2t + 3
Step 1 — Integrate: F(x) = ∫₀ˣ (2t+3) dt = x² + 3x
Step 2 — Differentiate F(x): F'(x) = d/dx [x²+3x] = 2x + 3
F'(x) = 2x + 3 = f(x) ✓ — the accumulation function’s rate of growth is precisely f itself
Starting function: f(t) = cos(t)
Step 1 — Integrate: F(x) = ∫₀ˣ cos(t) dt = sin(x)
Step 2 — Differentiate F(x): F'(x) = d/dx [sin(x)] = cos(x)
F'(x) = cos(x) = f(x) ✓ — works identically even for trigonometric functions

Section 04

Interactive: FTC Part 2 — Evaluating Definite Integrals

Four definite integrals, each solved the FTC way: find an antiderivative, then subtract F(a) from F(b). No rectangles, no limits of Riemann sums.

  FTC Part 2 Explorer
Evaluate: ∫₀³ x² dx
Antiderivative: F(x) = x³/3
F(3) − F(0) = 27/30 = 9 − 0
Result = 9 — this is the exact area under y = x² from x=0 to x=3
Evaluate: ∫₁⁴ (2x+1) dx
Antiderivative: F(x) = x² + x
F(4) − F(1) = (16+4) − (1+1) = 20 − 2
Result = 18 — matches the trapezoid area you’d get geometrically, confirming the shortcut
Evaluate: ∫₀^π sin(x) dx
Antiderivative: F(x) = −cos(x)
F(π) − F(0) = (−cos π) − (−cos 0) = 1 − (−1)
Result = 2 — the area under one full hump of the sine curve
Evaluate: ∫₀² eˣ dx
Antiderivative: F(x) = eˣ
F(2) − F(0) = 1 ≈ 7.389 − 1
Result ≈ 6.389 — exponential functions are their own antiderivative, so this is especially quick

Section 05

The Odometer and Speedometer Model

Every car dashboard already contains a working demonstration of FTC. The speedometer shows your instantaneous speed — a rate. The odometer shows your total distance traveled — an accumulation of that rate over time.

1

The odometer IS an accumulation function

odometer(t) = ∫₀ᵗ speed(τ) dτ + odometer(0). It’s literally integrating your speed over time to build up total distance.

2

The speedometer IS the derivative of the odometer

speed(t) = d/dt [odometer(t)]. At any instant, your speed is exactly how fast the odometer reading is changing. This is FTC Part 1, playing out on your dashboard.

3

Total distance = the odometer difference

Distance traveled between two times a and b = odometer(b) − odometer(a) = ∫ₐᵇ speed(t) dt. This is FTC Part 2 — you don’t need to track every instant of speed, just two odometer readings.

🚗 Why This Analogy Is Exact, Not Approximate

This isn’t a loose metaphor — it’s mathematically precise. Speed genuinely is the derivative of position, and position genuinely is the integral of speed. FTC isn’t describing something *like* a speedometer and odometer; a speedometer and odometer literally are a real-time physical instance of the theorem.

Section 06

Interactive: Watching Area Accumulate

Let f(t) = t (a straight line through the origin). As x grows, the shaded area under the line from 0 to x grows too — that shaded area IS F(x). Click through increasing values of x and watch the accumulation function build up, one snapshot at a time.

  Area Accumulation — f(t) = t
x=1 1 0
F(1) = 1²/2 = 0.5 Shaded area = 0.5
x=2 2 0
F(2) = 2²/2 = 2 Shaded area = 2
x=3 3 0
F(3) = 3²/2 = 4.5 Shaded area = 4.5
x=4 4 0
F(4) = 4²/2 = 8 Shaded area = 8

Notice the area doesn’t grow at a constant rate — it grows FASTER as x increases, because the height (f(x)=x) is also growing. That growing rate of area is exactly f(x) itself: FTC Part 1 in action.

Section 07

Why the Two Parts Are Really One Theorem

Part 1 and Part 2 can feel like two separate facts, but they’re two views of the exact same relationship, just pointed in opposite directions.

Part 1 direction
Start with a function → integrate it → differentiate the result → land back where you started. Integrate, then differentiate = identity.
Part 2 direction
Start with a function → find its antiderivative (undo differentiation) → use it to measure total accumulation. Differentiate, then integrate = identity (up to a constant).

🔄 The Inverse-Operation Pattern

This mirrors √(x²) = x and (√x)² = x — squaring and square-rooting undo each other in both directions. Differentiation and integration have exactly this same inverse relationship, just operating on functions instead of numbers.

Section 08

Worked Example — Projectile Motion

A ball is thrown upward. Its velocity (accounting for gravity) is v(t) = −9.8t + 20 meters per second, where t is measured in seconds. Find the ball’s total displacement between t = 0 and t = 2 seconds.

1

Recognize this as FTC Part 2

Displacement is the integral of velocity. We need ∫₀² v(t) dt, and FTC Part 2 tells us to find an antiderivative and subtract endpoint values.

2

Find the antiderivative

F(t) = −4.9t² + 20t  (check: F'(t) = −9.8t + 20 = v(t) ✓)

3

Evaluate F(2) and F(0)

F(2) = −4.9(4) + 20(2) = −19.6 + 40 = 20.4    F(0) = 0

4

Subtract

Displacement = F(2) − F(0) = 20.4 − 0 = 20.4 meters

v(t) = −9.8t + 20   (velocity)
F(t) = −4.9t² + 20t   (antiderivative — position)

∫₀² v(t) dt = F(2) − F(0)
= (−4.9×4 + 40) − 0
= 20.4

The ball travels 20.4 meters (net) in the first 2 seconds.

⚠ Displacement vs Total Distance

This calculation gives net displacement. If the ball goes up and then starts falling back down within those 2 seconds, the total distance traveled (odometer-style) would be larger, since up-then-down distances partially cancel in a plain integral. Total distance requires integrating |v(t)| instead.

Section 09

Common Mistakes to Avoid

➖

Reversing F(a) and F(b)

The formula is F(b) − F(a), upper bound minus lower bound — never the other way around. Flipping it flips the sign of your answer.

➕

Worrying about “+C”

For definite integrals, the constant of integration always cancels: (F(b)+C) − (F(a)+C) = F(b) − F(a). Any antiderivative works — you don’t need to find “the” one.

🚫

Ignoring discontinuities

If f has a vertical asymptote or jump inside [a,b], you can’t blindly apply FTC across it — the interval must be split or treated as an improper integral.

🔀

Confusing definite and indefinite

∫f(x)dx (indefinite) is a family of functions plus C. ∫ₐᵇf(x)dx (definite) is a single number. Only definite integrals get evaluated via FTC Part 2 directly.

Section 10

Real-World Applications

🚀

Physics — Motion

Displacement from velocity, velocity from acceleration, work done from a variable force — all direct FTC applications in mechanics.

💰

Economics

Total cost is the integral of marginal cost. Consumer and producer surplus are computed as areas — definite integrals evaluated via FTC.

💊

Medicine

Total drug absorbed into the bloodstream over time is the integral of the concentration-rate function — critical for dosage modeling.

⚡

Electrical Engineering

Total electric charge is the integral of current over time: Q = ∫ I(t) dt — a direct real-world use of FTC Part 2.

📊

Probability

A cumulative distribution function (CDF) is the integral of a probability density function (PDF) — FTC Part 1 connects the two directly.

🌱

Biology

Total population growth over a period is the integral of the growth-rate function, used constantly in ecological and epidemiological modeling.

Section 11

Differentiation vs Integration — Comparison Table

AspectDifferentiationIntegration
Geometric meaningSlope of the tangent lineArea under the curve
Physical meaningInstantaneous rate of changeTotal accumulation over an interval
Notationf'(x) or dy/dx∫f(x)dx
Result typeAnother functionA function (indefinite) or a number (definite)
UndoesIntegrationDifferentiation
Everyday exampleSpeedometer readingOdometer reading

Section 12

Code Examples — Python

Verifying FTC symbolically with SymPy

Python
from sympy import *

x, t = symbols('x t')

# FTC Part 1: differentiate an accumulation function
f = t
F = integrate(f, (t, 0, x))       # F(x) = x**2/2
check = diff(F, x)                    # should equal f
print(F, "->", check)              # x**2/2 -> x

# FTC Part 2: evaluate a definite integral
result = integrate(x**2, (x, 0, 3))
print(result)                        # 9

result2 = integrate(sin(x), (x, 0, pi))
print(result2)                       # 2

Numerical integration with SciPy (matching the analytic answer)

Python
from scipy.integrate import quad
import numpy as np

# ∫₀² e^x dx  — should be ~6.389
result, error = quad(lambda x: np.exp(x), 0, 2)
print(f"{result:.4f}")                 # 6.3891

# Projectile displacement: v(t) = -9.8t + 20, from t=0 to t=2
displacement, error = quad(lambda t: -9.8*t + 20, 0, 2)
print(f"{displacement:.1f} meters")     # 20.4 meters

Section 13

Knowledge Quiz

Click a question to expand it, then pick your answer.

FTC establishes that differentiation and integration undo each other — much like addition and subtraction. This is why calculus works as one unified subject, and it’s what makes Part 2’s shortcut for evaluating definite integrals possible.
The speedometer shows instantaneous speed — the rate of change (derivative) of position. The odometer accumulates that speed over time — the integral. speed(t) = d/dt[odometer(t)] is FTC Part 1 in action on your dashboard.
Antiderivative: F(x) = x² + x. F(4) = 16+4 = 20. F(1) = 1+1 = 2. F(4) − F(1) = 20 − 2 = 18.
(F(b)+C) − (F(a)+C) = F(b) − F(a) + C − C = F(b) − F(a). The constant cancels algebraically regardless of its value, which is why you can pick ANY antiderivative when applying FTC Part 2.
Integrating a rate function over an interval gives the TOTAL accumulated quantity over that interval — exactly like integrating speed gives total distance. Here, integrating the fill rate gives total volume added in those 10 minutes.
FTC requires continuity on the entire interval [a,b]. Since 1/x² blows up to infinity at x=0, and 0 is inside [-1,1], the function isn’t continuous across the whole interval — this must be treated as an improper integral with special care, not a direct FTC application.

The post The Fundamental Theorem of Calculus — Explained with Real Examples appeared first on Learn With Examples.

]]>
https://learnwithexamples.org/fundamental-theorem-of-calculus/feed/ 0 804
Real-Life Examples of Permutations and Combinations https://learnwithexamples.org/real-life-examples-of-permutations-and-combinations/ https://learnwithexamples.org/real-life-examples-of-permutations-and-combinations/#respond Mon, 27 Jul 2026 16:15:00 +0000 https://learnwithexamples.org/?p=800 Real-Life Examples of Permutations and Combinations — Explained Simply Learn · With · Examples Lottery odds, poker hands, phone lock codes, Olympic podiums, pizza orders — they’re all counting problems…

The post Real-Life Examples of Permutations and Combinations appeared first on Learn With Examples.

]]>
Real-Life Examples of Permutations and Combinations — Explained Simply

Learn · With · Examples

Lottery odds, poker hands, phone lock codes, Olympic podiums, pizza orders — they’re all counting problems in disguise, and they all come down to one question: does order matter? This guide explains permutations and combinations using nothing but real, everyday examples, with interactive scenario galleries and full worked calculations.

📖 19 min read 🔀 Perm-or-combo identifier 🏅 Permutation gallery 🎟 Combination gallery ❓ 6-question quiz

Section 01

The Difference — A Lock Code vs. A Fruit Bowl

Imagine two everyday situations. First: you’re setting a 3-digit code on a padlock using digits 1, 2, and 3. The code 1-2-3 is completely different from 3-2-1 — they open different locks (or rather, only the exact sequence you set will open yours). Order matters. This is a permutation.

Second: you’re picking 3 fruits from a bowl to make a smoothie — an apple, a banana, and an orange. It doesn’t matter if you grabbed the apple first or the orange first — you end up with the exact same smoothie ingredients either way. Order doesn’t matter. This is a combination.

💡 The One Question That Decides Everything

Every counting problem in this entire topic reduces to a single question: if I rearrange the same items, do I get something different? Yes → permutation. No → combination. Everything else is just formula mechanics built on top of that one distinction.

n!
Factorial — the building block behind every permutation and combination formula
nPr
Permutations — ordered selections
nCr
Combinations — unordered selections
1/13.98M
Odds of matching all 6 numbers in a 6/49 lottery — a real combination calculation

Section 02

Formal Definitions — The nPr and nCr Formulas

Both formulas start from the same n items, choosing r of them — they only differ in whether order counts.

P(n,r) = n! / (n − r)!
Permutations — the number of ORDERED ways to choose r items from n
C(n,r) = n! / [r! (n − r)!]
Combinations — the number of UNORDERED ways to choose r items from n

🔗 How They’re Related

Notice combinations = permutations ÷ r!. That’s not a coincidence — every unordered group of r items can be arranged in r! different orders. Combinations “collapse” all those orderings into one, because order doesn’t matter. C(n,r) = P(n,r) / r!

Section 03

Interactive: Permutation or Combination?

Click through 5 real scenarios. Try to guess before reading the reasoning — this is the exact judgment call you’ll need to make on every word problem you encounter.

  Scenario Identifier
“Assign gold, silver, and bronze medals to 3 of 10 finalists.”
PERMUTATION
Gold, silver, and bronze are three different roles. Alice-gold/Bob-silver is a completely different outcome from Bob-gold/Alice-silver, even though the same two people are involved. Order (which position each person lands in) changes the result.
“Choose 4 pizza toppings from a menu of 12.”
COMBINATION
A pizza with pepperoni-mushroom-olive-onion is the exact same pizza as onion-olive-mushroom-pepperoni. The order you listed the toppings in doesn’t create a different pizza — only the final set of 4 toppings matters.
“Set a 4-digit lock code using digits 0–9.”
PERMUTATION
1-2-3-4 and 4-3-2-1 are different codes that open different locks. Position matters — this is a permutation (specifically one that allows repeated digits, covered later).
“Pick 2 co-captains from a 15-person sports team.”
COMBINATION
Both co-captains hold the identical role — there’s no “first co-captain” and “second co-captain.” Choosing Sam-then-Priya gives the exact same pair of co-captains as Priya-then-Sam.
“Arrange 6 unique paintings in a row along a gallery wall.”
PERMUTATION
Each painting occupies a specific position on the wall (1st, 2nd, 3rd…). Swapping two paintings’ positions creates a visibly different arrangement — position matters, so this is a permutation.

Section 04

The Factorial Foundation

Both formulas are built entirely from factorials, so it’s worth getting comfortable with them first. n! (read “n factorial”) means multiply every whole number from n down to 1.

5! = 5 × 4 × 3 × 2 × 1 = 120
The number of ways to arrange 5 distinct items in a row
nn!Real meaning
0!1By definition — there’s exactly 1 way to arrange nothing
1!1One item — only one arrangement possible
2!22 books on a shelf: AB or BA
3!63 runners crossing the finish line, in order
4!244 people seated around a table
5!1205 songs in a playlist order
6!7206 books arranged on a shelf
10!3,628,80010 runners in a full race ranking

⚠ Why 0! = 1

This trips people up constantly. Think of factorial as “number of ways to arrange these items.” With zero items, there’s exactly one way to arrange them: do nothing. An empty arrangement is still one valid arrangement — so 0! = 1, not 0.

Section 07

The “Does Order Matter?” Test

A reliable 3-step process for any word problem you encounter:

1

Swap two of your chosen items — does the outcome change?

If choosing Alice-then-Bob gives a genuinely different result than Bob-then-Alice (different roles, different positions), order matters → permutation. If it’s the same outcome either way → combination.

2

Check for distinct roles or positions

Ranks (1st/2nd/3rd), seats, digit positions, and named roles (president/treasurer) all signal permutation. A “set” or “group” with no internal distinction signals combination.

3

Check if repetition is allowed

If the same item can be chosen more than once (like PIN digits or dice rolls), you need the “with repetition” formula — covered in Section 9 — regardless of whether order matters.

⚠ The Most Common Mistake

Students often default to combinations because the formula “feels simpler.” But most everyday scenarios with roles, rankings, sequences, or codes are permutations. Always run the swap test in Step 1 before picking a formula.

Section 08

Worked Example — Building a Real Password System

Let’s combine everything into one realistic problem: a website requires passwords with exactly 2 different letters (no repeats) followed by 3 digits (digits CAN repeat). How many total passwords are possible?

1

Break it into two independent parts

Part A: choosing 2 different letters in order. Part B: choosing 3 digits, repeats allowed. We’ll solve each separately, then multiply.

2

Solve Part A — the letters

2 letters, no repeats, order matters (AB ≠ BA as passwords) → this is a permutation. P(26,2) = 26 × 25 = 650 possible letter pairs.

3

Solve Part B — the digits

3 digits, repeats allowed, order matters → permutation with repetition. 10 × 10 × 10 = 1,000 possible digit sequences.

4

Apply the multiplication principle

Every letter-pair can be combined with every digit-sequence — so multiply the two results together.

Part A (letters): P(26,2) = 26 × 25 = 650
Part B (digits): 10³ = 1,000

Total passwords = 650 × 1,000
= 650,000 possible passwords

🔗 The Multiplication Principle

Whenever a problem splits into independent stages (letters, THEN digits), multiply the number of possibilities at each stage. This single rule — the fundamental counting principle — is what lets you break any complex real-world counting problem into smaller, solvable permutation and combination pieces.

Section 09

Permutations and Combinations with Repetition

The formulas above assume each item can only be chosen once. Real life often allows repeats — here’s how the math changes.

Permutations WITH repetition — n choices, repeated r times, order matters. This is how PIN codes and license plates are counted.
C(n + r − 1, r)
Combinations WITH repetition (“stars and bars”) — choosing r items from n types, repeats allowed, order doesn’t matter

Worked Example — Ice Cream Scoops

An ice cream shop has 5 flavors. You order 3 scoops, and you’re allowed to repeat a flavor (like 2 scoops chocolate + 1 scoop vanilla). Order doesn’t matter — a cup with chocolate-chocolate-vanilla is the same order regardless of which scoop went in first.

n = 5 flavors, r = 3 scoops, repetition allowed, order doesn’t matter
C(n + r − 1, r) = C(5 + 3 − 1, 3) = C(7,3)
= 7! / [3! × 4!] = (7×6×5) / 6
= 35 possible ice cream orders

Section 10

Pascal’s Triangle Connection

Every entry in Pascal’s Triangle IS a combination value. Row n, position k gives you exactly C(n,k) — no calculation required, just count and look it up.

n=01
n=11   1
n=21   2   1
n=31   3   3   1
n=41   4   6   4   1
n=51   5   10   10   5   1
n=61   6   15   20   15   6   1

Look at row n=6: the entries are C(6,0)=1, C(6,1)=6, C(6,2)=15, C(6,3)=20, and so on. Each number is the sum of the two numbers diagonally above it — which is exactly Pascal’s Rule: C(n,k) = C(n−1,k−1) + C(n−1,k).

💡 Where You’ve Actually Seen This

Pascal’s Triangle is also the coefficients of a binomial expansion — (a+b)⁶ expands using exactly the row-6 numbers: 1, 6, 15, 20, 15, 6, 1. It’s the same combinatorics showing up in algebra, probability, and even genetics (Punnett square ratios follow these same patterns).

Section 11

Real-World Domains

🔐

Cryptography

Key space size for a cipher is a permutation calculation — the number of possible keys directly determines how brute-forceable an encryption scheme is.

🧬

Genetics

Counting possible DNA sequences, or the number of ways alleles can combine in offspring, relies directly on permutation and combination formulas.

🏆

Tournament Brackets

The number of possible ways a single-elimination bracket can play out, or how many unique round-robin schedules exist, is pure combinatorics.

📅

Scheduling

Assigning employees to shifts, or students to exam time slots, is a permutation/combination problem — especially when constraints (no repeats, fixed roles) apply.

🧪

Quality Control

Choosing a random sample of r items from a batch of n to inspect for defects is a classic combination — order of inspection doesn’t matter.

🎲

Game Design

Loot drop tables, card game deck compositions, and probability balancing in games all rely on combinatorics to calculate fair odds.

Section 12

Code Examples — Python

Using math.perm() and math.comb() (Python 3.8+)

Python
import math

# Olympic podium — 3 medals from 8 finalists, order matters
print(math.perm(8, 3))     # 336

# Lottery — choose 6 numbers from 49, order doesn't matter
print(math.comb(49, 6))    # 13983816

# Password letters — 2 of 26, no repeats, order matters
print(math.perm(26, 2))    # 650

# Pizza toppings — choose 3 of 8, order doesn't matter
print(math.comb(8, 3))     # 56

# PIN code — permutation WITH repetition (not built into math module)
pin_combos = 10 ** 4
print(pin_combos)          # 10000

# Ice cream scoops — combination WITH repetition (stars and bars)
ice_cream = math.comb(5 + 3 - 1, 3)
print(ice_cream)          # 35

Generating the actual arrangements with itertools

Python
from itertools import permutations, combinations

runners = ['A', 'B', 'C', 'D']

# All ordered podium outcomes (top 3 of 4 runners)
podiums = list(permutations(runners, 3))
print(len(podiums))      # 24  =  4P3
print(podiums[:3])
# [('A','B','C'), ('A','B','D'), ('A','C','B'), ...]

# All unordered committees (any 3 of 4 people)
committees = list(combinations(runners, 3))
print(len(committees))   # 4  =  4C3
print(committees)
# [('A','B','C'), ('A','B','D'), ('A','C','D'), ('B','C','D')]

Section 13

Knowledge Quiz

Click a question to expand it, then pick your answer.

If swapping the order of your chosen items changes the result (different roles, positions, or sequence), it’s a permutation. If the same items in any order count as the same outcome, it’s a combination.
0! = 1 by definition. Think of factorial as “number of ways to arrange these items” — with zero items, there’s exactly one way to arrange them: the empty arrangement. This convention also keeps the nPr and nCr formulas working correctly when r = n.
The three roles are distinct (line leader ≠ door holder ≠ paper collector), so order/role matters — this is a permutation. P(20,3) = 20 × 19 × 18 = 6,840.
A hand of {A♠, K♥, 7♦, 3♣, 2♠} is the same hand no matter which card was dealt first. Since order doesn’t affect the outcome, it’s counted with combinations: C(52,5) = 2,598,960.
PIN digits can repeat (like 1-1-2-2) and order matters (position 1 ≠ position 2). This is “permutation with repetition”: n choices raised to the power of r positions → 10⁴ = 10,000.
Every entry in Pascal’s Triangle is a combination: row n, position k gives C(n,k). Row 6’s entries (1,6,15,20,15,6,1) are C(6,0) through C(6,6) — and indeed C(6,3) = 20.

The post Real-Life Examples of Permutations and Combinations appeared first on Learn With Examples.

]]>
https://learnwithexamples.org/real-life-examples-of-permutations-and-combinations/feed/ 0 800
REST APIs Explained — How Web Services Talk to Each Other https://learnwithexamples.org/rest-apis-explained/ https://learnwithexamples.org/rest-apis-explained/#respond Mon, 27 Jul 2026 14:46:02 +0000 https://learnwithexamples.org/?p=791 REST APIs Explained — How Web Services Talk to Each Other, With Real Examples Learn· With · Examples Every time an app checks the weather, posts a tweet, or processes…

The post REST APIs Explained — How Web Services Talk to Each Other appeared first on Learn With Examples.

]]>
REST APIs Explained — How Web Services Talk to Each Other, With Real Examples

Learn· With · Examples

Every time an app checks the weather, posts a tweet, or processes a payment, it’s almost certainly talking to a REST API behind the scenes. This guide breaks REST down from first principles — what makes an API “RESTful,” how HTTP methods map to real actions, and how to read a request and response like a developer — with real, working examples throughout.

📖 19 min read 🔀 HTTP methods explorer 🚦 Status code explorer 🎯 Endpoint design gallery ❓ 6-question quiz

Section 01

What is a REST API? — The Waiter Analogy

Imagine a restaurant. You (the customer) never walk into the kitchen to cook your own food. Instead, you tell a waiter what you want from a menu, the waiter takes that request to the kitchen, and brings back your food. You don’t need to know how the kitchen works internally — you just need to know the menu and how to order.

A REST API (Representational State Transfer, Application Programming Interface) is exactly this waiter. Your app (the client) doesn’t access a company’s database directly. Instead, it sends a structured request to the API, the API talks to the “kitchen” (the server’s internal systems), and sends back a structured response — usually as JSON. REST is simply a set of conventions for how that request and response should be shaped, so any client and any server can understand each other without a custom, one-off arrangement.

💡 The Key Idea

REST is not a technology, a library, or a protocol you install — it’s an architectural style: a set of design rules for building APIs on top of HTTP. If an API follows these rules, it’s called “RESTful.”

2000
Year REST was defined — in Roy Fielding’s PhD dissertation
83%+
Of public APIs today are REST or REST-like (source: various API registries)
6
Architectural constraints that define “RESTful”
JSON
The dominant data format for REST responses today

Section 02

The 6 REST Constraints — Formal Definition

Roy Fielding defined REST as a set of architectural constraints. An API that satisfies all of them is truly “RESTful” — though in practice, most real-world APIs relax one or two (usually “code on demand,” which is optional anyway).

🖥

1. Client-Server

The client (UI) and server (data storage) are separate. Either can change independently as long as the interface between them stays the same.

🚫

2. Stateless

Every request must contain all information needed to process it. The server stores no client session state between requests.

💾

3. Cacheable

Responses must explicitly state whether they can be cached, so clients can reuse data and reduce unnecessary requests.

🔌

4. Uniform Interface

A consistent way to identify resources (URLs) and interact with them (HTTP methods) — the core idea that makes REST predictable.

🧱

5. Layered System

The client can’t tell if it’s talking directly to the server or through intermediaries like load balancers or caches — and doesn’t need to.

📦

6. Code on Demand (optional)

Servers can optionally send executable code (like JavaScript) to extend client functionality. The only constraint that’s optional.

Section 03

Interactive: HTTP Methods Explorer

REST maps standard HTTP methods onto CRUD operations (Create, Read, Update, Delete). Click each method below to see a real request and its typical response for a simple blog post API.

  HTTP Methods Explorer — /api/posts
GET /api/posts/42

Retrieves data. Read-only — calling it 100 times never changes anything on the server. This “no side effects” property is called safe.

Request
GET /api/posts/42 HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGci…
Response — 200 OK
{
  “id”: 42,
  “title”: “Understanding REST”,
  “published”: true
}
POST /api/posts

Creates a new resource. Calling it twice creates two separate posts — it is not idempotent. The server decides the new resource’s ID.

Request
POST /api/posts HTTP/1.1
Content-Type: application/json

{
  “title”: “New Post”,
  “body”: “Hello world”
}
Response — 201 Created
Location: /api/posts/108

{
  “id”: 108,
  “title”: “New Post”,
  “published”: false
}
PUT /api/posts/42

Replaces a resource entirely. You must send the FULL object — any field you omit gets wiped out. Calling it twice with the same body gives the same result — it is idempotent.

Request
PUT /api/posts/42 HTTP/1.1
Content-Type: application/json

{
  “title”: “Understanding REST (Updated)”,
  “body”: “Full new content…”,
  “published”: true
}
Response — 200 OK
{
  “id”: 42,
  “title”: “Understanding REST (Updated)”,
  “published”: true
}
PATCH /api/posts/42

Partially updates a resource — only send the fields you want to change. Everything else stays untouched. Usually idempotent in practice, though not strictly guaranteed by spec.

Request
PATCH /api/posts/42 HTTP/1.1
Content-Type: application/json

{
  “published”: true
}
Response — 200 OK
{
  “id”: 42,
  “title”: “Understanding REST”,
  “published”: true // only this changed
}
DELETE /api/posts/42

Removes a resource. Calling it twice has the same end state (the resource is gone both times) — it is idempotent, even though the second call typically returns 404 instead of 200.

Request
DELETE /api/posts/42 HTTP/1.1
Authorization: Bearer eyJhbGci…
Response — 204 No Content
(empty body — resource successfully deleted)
MethodCRUD actionSafe?Idempotent?
GETReadYesYes
POSTCreateNoNo
PUTReplaceNoYes
PATCHPartial updateNoUsually
DELETERemoveNoYes

⚠ Safe vs Idempotent — Not the Same Thing

Safe means the request causes no side effects at all (read-only). Idempotent means calling it multiple times produces the same end state as calling it once — but it can still have side effects the first time. DELETE isn’t safe (it changes data) but it IS idempotent (deleting an already-deleted item leaves the same end state).

Section 04

Anatomy of a REST Request

Every REST request is built from four parts. Understanding each one is the key to reading any API’s documentation.

1

Method + URL — what and where

The HTTP method (GET, POST, etc.) says what action to take. The URL identifies which resource — e.g. /api/users/17/orders means “orders belonging to user 17.”

2

Headers — metadata about the request

Key-value pairs describing the request: Authorization (who’s asking), Content-Type (what format the body is in), Accept (what format you want back).

3

Query parameters — filtering and options

Appended to the URL after a ?, used for filtering, sorting, and pagination: /api/posts?published=true&limit=10&sort=-date

4

Body — the actual data (POST/PUT/PATCH only)

The payload being sent, almost always JSON today. GET and DELETE requests typically have no body — everything they need is in the URL and headers.

https://api.example.com/v1/users/17/orders?status=shipped&limit=5
Protocol → Host → API version → Resource path → Query parameters

Section 05

Interactive: HTTP Status Code Explorer

Every response includes a 3-digit status code. The first digit tells you the category at a glance. Click each category to see the codes you’ll actually encounter.

  HTTP Status Code Explorer
200
OK
Standard success — request worked, response body contains the result
201
Created
A new resource was successfully created — typically returned by POST
204
No Content
Success, but nothing to return — common for DELETE requests
301
Moved Permanently
The resource has a new permanent URL — update your bookmarks/code
304
Not Modified
Cached version is still valid — used with conditional caching headers
400
Bad Request
The request is malformed — invalid JSON, missing required field
401
Unauthorized
Missing or invalid authentication credentials
403
Forbidden
You’re authenticated, but not allowed to access this resource
404
Not Found
The resource doesn’t exist at this URL
409
Conflict
The request conflicts with the current state — e.g. duplicate entry
429
Too Many Requests
You’ve hit the rate limit — slow down and retry later
500
Internal Server Error
Something broke on the server — not your fault, report it
502
Bad Gateway
A server acting as a gateway got an invalid response upstream
503
Service Unavailable
Server is overloaded or down for maintenance — try again shortly

Section 06

Resource Naming — Nouns, Not Verbs

The single most common REST mistake is putting an action inside the URL. REST already has actions — they’re the HTTP methods. URLs should describe what you’re acting on (a noun), not what to do to it (a verb).

❌ Bad:  POST /api/createUser
✅ Good: POST /api/users   ← the verb “create” comes from POST itself

❌ Bad:  GET /api/getUserById?id=17
✅ Good: GET /api/users/17   ← the ID is part of the resource path

❌ Bad:  POST /api/deletePost/42
✅ Good: DELETE /api/posts/42   ← DELETE method + noun path

✅ Naming Conventions That Scale

Use plural nouns for collections (/users not /user), nest relationships logically (/users/17/orders), use hyphens not underscores in multi-word paths (/blog-posts not /blog_posts), and keep casing consistent — lowercase is the near-universal standard.

Section 08

Statelessness Explained

Statelessness is the constraint developers misunderstand most often. It does not mean “the app has no persistent data” — a database is state, and REST APIs use them constantly. It means the server keeps no memory of the client between requests.

❌ Stateful (not RESTful)

Request 1: “Log in as Alice” → server remembers “this connection is Alice” in memory.
Request 2: “Get my orders” → server assumes it’s still talking to Alice based on that memory.
If request 2 hits a different server in a load-balanced cluster, it has no idea who you are.

✅ Stateless (RESTful)

Request 1: “Log in as Alice” → server returns a token.
Request 2: “Get my orders” + Authorization: Bearer <token> in the header.
Every request proves who it is on its own. Any server in the cluster can handle it — no shared memory required.

💡 Why It Matters

Stateless APIs scale horizontally with almost no effort — spin up 100 identical servers behind a load balancer, and any of them can handle any request, because no request depends on “remembering” a previous one. This is a major reason REST became the dominant style for cloud-scale web services.

Section 09

Worked Example — A Full Request/Response Cycle

Let’s trace a complete real-world flow: a mobile app updating a user’s profile picture URL.

1

App builds the request

The app knows the user’s ID (17) and has a stored auth token from login. It decides this is a partial update, so PATCH is the right method.

2

Request sent over HTTPS

PATCH /api/v1/users/17 with headers Authorization and Content-Type: application/json, body {“avatar_url”: “https://cdn.example.com/img/17.jpg”}.

3

Server validates the token

The server checks the Authorization header against its token store — this is the “prove who you are on every request” statelessness in action.

4

Server updates the database

Only the avatar_url field is changed for user 17 — every other field (name, email, bio) stays exactly as it was, because this is PATCH, not PUT.

5

Server responds

200 OK with the full updated user object in the body, so the app can immediately re-render the UI without a separate GET request.

PATCH /api/v1/users/17 HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9…
Content-Type: application/json

{ “avatar_url”: “https://cdn.example.com/img/17.jpg” }

— response —

HTTP/1.1 200 OK
Content-Type: application/json

{ “id”: 17, “name”: “Alice”, “avatar_url”: “https://cdn.example.com/img/17.jpg”, “updated_at”: “2026-07-27T10:15:00Z” }

Section 10

REST vs SOAP vs GraphQL vs gRPC

StyleData formatBest forDownside
RESTJSON (usually)Public APIs, general web/mobile backends — simple, cacheable, widely understoodOver/under-fetching — fixed response shape per endpoint
SOAPXMLEnterprise systems needing strict contracts, built-in security standards (banking, healthcare)Verbose, heavyweight, steep learning curve
GraphQLJSONComplex UIs needing flexible, precise data shapes in one round tripHarder to cache; more complex server implementation
gRPCProtocol Buffers (binary)High-performance internal microservice-to-microservice communicationNot human-readable; harder to debug and test manually

🎯 Bottom Line

REST remains the default choice for public-facing APIs because of its simplicity and universal HTTP tooling support. GraphQL shines when clients need flexible queries. gRPC dominates internal service-to-service calls where raw speed matters most.

Section 11

Real-World REST APIs

💳

Stripe API

Widely considered a gold-standard REST API — consistent noun-based URLs, excellent status code usage, thorough documentation with live examples.

🐙

GitHub API

Fully RESTful with clean resource nesting (/repos/{owner}/{repo}/issues) — a commonly cited teaching example for good API design.

🌦

OpenWeatherMap

A simple, classic public REST API — a single GET request with query parameters returns current weather data as JSON.

📸

Instagram Graph API

Uses REST conventions for reading and publishing content, layered with OAuth for authentication and permission scopes.

🎵

Spotify Web API

REST endpoints for tracks, playlists, and playback control — used by nearly every third-party Spotify app and integration.

🗺

Google Maps API

Geocoding, directions, and places endpoints — all accessed via simple GET requests with query parameters.

Section 12

Code Examples — Python, JavaScript, cURL

Python — using the requests library

Python
import requests

# GET — fetch a resource
response = requests.get(
    "https://api.example.com/v1/posts/42",
    headers={"Authorization": "Bearer YOUR_TOKEN"}
)
print(response.status_code)   # 200
print(response.json())         # parsed dict

# POST — create a resource
response = requests.post(
    "https://api.example.com/v1/posts",
    headers={"Authorization": "Bearer YOUR_TOKEN"},
    json={"title": "New Post", "body": "Hello!"}
)
print(response.status_code)   # 201

# PATCH — partial update
response = requests.patch(
    "https://api.example.com/v1/posts/42",
    headers={"Authorization": "Bearer YOUR_TOKEN"},
    json={"published": True}
)

JavaScript — using fetch

JavaScript
// GET — fetch a resource
const res = await fetch("https://api.example.com/v1/posts/42", {
  headers: { "Authorization": "Bearer YOUR_TOKEN" }
});
const data = await res.json();
console.log(res.status, data);

// POST — create a resource
const created = await fetch("https://api.example.com/v1/posts", {
  method: "POST",
  headers: {
    "Authorization": "Bearer YOUR_TOKEN",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({ title: "New Post", body: "Hello!" })
});

cURL — quick command-line testing

Bash
# GET request
curl -X GET https://api.example.com/v1/posts/42 \
  -H "Authorization: Bearer YOUR_TOKEN"

# POST request with JSON body
curl -X POST https://api.example.com/v1/posts \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"title": "New Post", "body": "Hello!"}'

# DELETE request
curl -X DELETE https://api.example.com/v1/posts/42 \
  -H "Authorization: Bearer YOUR_TOKEN"

Section 13

Knowledge Quiz

Click a question to expand it, then pick your answer.

Statelessness means the server doesn’t remember previous requests from a client. Every request must be self-contained — including authentication (like a Bearer token). This is different from having a database, which is perfectly fine in REST.
PATCH is specifically for partial updates — send only the fields you want to change. PUT is for full replacement and requires sending the entire resource; any field you omit typically gets wiped out or reset.
REST URLs should be nouns (resources), and the action comes from the HTTP method. “getUserOrders” duplicates the verb that GET already expresses, and using POST for a read operation breaks caching and violates the “safe” property that reads should have.
GET is both safe and idempotent (read-only, repeatable). DELETE is idempotent but not safe — it changes data (removes a resource) but calling it again on an already-deleted resource leaves the same end state. POST is neither — it has side effects AND repeated calls create multiple new resources.
204 No Content is the standard success response for DELETE — the resource is gone, so there’s nothing meaningful to return in the body. 201 Created is for POST (resource creation), and 404/500 indicate errors, not success.
This is convention, not a hard HTTP rule — but it’s followed almost universally because it makes APIs self-explanatory: GET /users returns a list (the collection), GET /users/17 returns one item from that collection. Consistency here reduces guesswork for anyone integrating with the API.

The post REST APIs Explained — How Web Services Talk to Each Other appeared first on Learn With Examples.

]]>
https://learnwithexamples.org/rest-apis-explained/feed/ 0 791
AI Hallucinations: Why They Happen — Explained with Real Examples https://learnwithexamples.org/ai-hallucinations-explained-with-real-examples/ https://learnwithexamples.org/ai-hallucinations-explained-with-real-examples/#respond Mon, 27 Jul 2026 14:28:51 +0000 https://learnwithexamples.org/?p=787 AI Hallucinations: Why They Happen — Explained with Real Examples Learn · With · Examples Ask a language model a question and it can answer with total confidence — complete…

The post AI Hallucinations: Why They Happen — Explained with Real Examples appeared first on Learn With Examples.

]]>
AI Hallucinations: Why They Happen — Explained with Real Examples

Learn · With · Examples

Ask a language model a question and it can answer with total confidence — complete with dates, citations, and code — while being completely wrong. This isn’t a bug that occasional updates will fix. It is a direct consequence of how these models work. This guide breaks down exactly why hallucinations happen, with interactive examples, real cases, and the techniques used to reduce them.

📖 17 min read 🎲 Next-token prediction explorer 🖼 Hallucination gallery 🌡 Temperature demo ❓ 6-question quiz

Section 01

What is an AI Hallucination?

An AI hallucination is when a language model generates output that is fluent, confident, and completely wrong — a fabricated fact, a citation to a paper that doesn’t exist, a function that was never in the library, a quote nobody said. The term comes from human perception, where a hallucination is seeing something that isn’t there. The model isn’t “seeing” anything, but the analogy fits: it is generating something that has no basis in reality, with the same fluency as something true.

Think of a brilliant friend who has read almost everything but never admits when they don’t know something. Ask them a question and they will answer instantly and confidently — sometimes correctly, sometimes by smoothly inventing a plausible-sounding answer. They are not lying, because lying requires knowing the truth and hiding it. They genuinely cannot tell the difference between “I recall this” and “this sounds right.” That is a language model.

⚖ Real Case — Fabricated Legal Citations

In 2023, lawyers in a U.S. federal case submitted a brief containing citations to six court cases — complete with quotes and docket numbers. None of the cases existed. A chatbot had fabricated them entirely, and they read as completely legitimate legal citations. The court sanctioned the lawyers involved.

3–27%
Estimated hallucination rate across leading models on open-domain factual questions
0%
Built-in mechanism for a model to know it’s wrong — confidence is not accuracy
1 token
At a time — the entire generation process, with no global fact-check step
2016
Term first applied to neural network outputs, borrowed from image captioning research

Section 02

Intrinsic vs Extrinsic — The Formal Definition

Researchers split hallucinations into two categories, depending on what the output contradicts:

Intrinsic Hallucination
The output contradicts the source material it was given. Ask it to summarise a document, and it states something the document explicitly says is false.
Extrinsic Hallucination
The output cannot be verified from the given source at all — it adds new “facts” that are neither confirmed nor denied by the source material or reality.
P(next token | previous tokens)
This is all a language model actually computes — a probability distribution over the next token, not a fact-lookup

💡 The Core Insight

A language model is not a database with a retrieval system attached. It is a function that predicts the statistically most likely next word given everything before it. Truth is not a variable in that computation — plausibility is. Most of the time plausible and true are the same thing, because it learned from mostly-true text. But nothing in the mechanism enforces truth. When they diverge, you get a hallucination.

Section 03

Interactive: Next-Token Prediction Explorer

Every word a language model produces is chosen from a probability distribution over its entire vocabulary. Click a prompt below to see simplified next-token probabilities. Notice what happens when no single answer clearly dominates — that flat, spread-out distribution is exactly where hallucinations are born.

  Next-Token Prediction Explorer
Prompt: “The capital of France is ___
Paris
97%
Lyon
1.5%
Marseille
0.8%
(other)
0.7%
✅ Low hallucination risk. One token dominates overwhelmingly — this fact appeared millions of times in training data in exactly this pattern. The model is essentially certain.
Prompt: “The Battle of Hastings took place in the year ___
1066
78%
1067
9%
1086
6%
(other)
7%
⚠ Moderate risk. The correct answer still leads clearly, but nearby wrong years have real probability mass — with unlucky sampling, a wrong-but-plausible year could get selected.
Prompt: “The third-longest river in Paraguay by tributary count is ___
Río Apa
14%
Río Aquidabán
12%
Río Ypané
11%
Río Tebicuary
10%
(other, spread thin)
53%
🔴 High hallucination risk. No token dominates — this is an obscure, likely under-documented fact. The model will still confidently output ONE of these river names in fluent prose, with no indication of its uncertainty. This is the classic hallucination signature: a flat probability distribution rendered as a confident sentence.

⚠ The Fluency Trap

Notice that all three answers above would read equally confidently in a generated sentence — “The third-longest river in Paraguay by tributary count is the Río Apa.” Nothing in the grammar or tone signals that this answer had only 14% probability versus the capital-of-France answer’s 97%. Fluency is constant; certainty is invisible.

Section 04

Why They Happen — 6 Root Causes

🎯

1. Next-Token Prediction, Not Fact-Checking

The model’s only job during generation is “what word comes next.” There is no separate module that checks generated claims against a database of true facts.

📚

2. Training Data Gaps and Noise

If a topic was rare, contradictory, or wrong across the training corpus, the model absorbs that gap or error as if it were reliable pattern.

🎲

3. Sampling Randomness

Generation involves randomness (temperature, top-p sampling) by design — to avoid robotic repetitive text. That same randomness can select a low-probability, incorrect token.

🔌

4. No Real-Time Grounding

Without a retrieval or search tool connected, the model only has frozen knowledge from training — it cannot look anything up to double-check itself mid-answer.

📊

5. Miscalibrated Confidence

Models are trained to sound helpful and fluent, not to output calibrated uncertainty. There’s no built-in “I’m only 14% sure” phrasing — everything comes out sounding equally confident.

🗜

6. Lossy Compression of Knowledge

A model with billions of parameters still compresses trillions of words of training text. Like a JPEG at low quality, fine details blur — and the model fills gaps with its best statistical guess.

🧠 An Analogy: Compression, Not Storage

A language model does not store facts the way a database stores rows. It stores statistical patterns compressed from its training data. Recalling a fact is more like reconstructing a blurry JPEG than reading a row from a spreadsheet — usually accurate, but sometimes the reconstruction fills gaps with a plausible-looking artifact that was never actually there.

Section 06

Interactive: Temperature and Sampling

“Temperature” controls how randomly a model samples from its next-token probability distribution. Lower temperature makes it pick the highest-probability token almost every time. Higher temperature flattens the distribution, giving low-probability (and potentially wrong) tokens a real chance of being chosen. Click through the same prompt at three different temperatures.

  Temperature Comparison — same prompt, three settings
Prompt: “List three lesser-known facts about the founding of the city of Timbuktu.”
temp = 0.0deterministicalways picks top token
1. Timbuktu was founded around the 5th century CE by Tuareg nomads as a seasonal camp.
2. It grew into a major center of trade in salt, gold, and books.
3. The University of Sankore made it a hub of Islamic scholarship by the 14th century.
✅ Lowest hallucination risk — but not zero. At temp 0 the model always picks its single most likely token, giving the most “average” and well-supported answer it has. Repeatable and conservative, though it can still be confidently wrong if the top token itself is incorrect.
temp = 0.7balancedsome randomness
1. Timbuktu’s name may derive from “Tin Abutut,” referring to a well tended by a woman named Buktu.
2. Its libraries once held an estimated 700,000 manuscripts.
3. By the 1300s it briefly rivaled Cairo as a center of Islamic learning in West Africa.
⚠ Moderate risk — this is the typical default used in chat products. More varied and interesting phrasing, but occasional lower-probability claims slip in (exact manuscript counts and etymology claims are exactly the kind of specific-sounding numbers most likely to be invented).
temp = 1.5highly randomcreative but unstable
1. Timbuktu was reportedly founded in 1204 by the merchant-explorer Amara Keita after a caravan dispute.
2. Its original name, “Tinbouctu,” is said to mean “the place of the hidden well of stars.”
3. Early manuscripts suggest the city briefly minted its own gold currency called the “sanu-tomo.”
🔴 High risk — specific names, dates, and invented terms appear with total fluency and confidence. This is temperature-driven hallucination in its purest form: low-probability, largely fabricated tokens getting selected and then compounding into an entirely invented but very readable narrative.

Section 07

Types of Hallucinations

TypeWhat happensExample
FactualStates something false about the worldWrong birth year for a historical figure
FaithfulnessContradicts a document it was explicitly givenSummarizing a report and stating a conclusion the report actually rejects
CitationInvents sources, papers, or linksA perfectly formatted but nonexistent journal reference
LogicalReasoning steps don’t actually follow from each otherA multi-step math “proof” where step 3 doesn’t follow from step 2
EntityMerges or invents people, places, organizationsAttributing a real quote to the wrong (but plausible) person
TemporalGets timelines or sequences wrongSaying an event happened before its actual cause

Section 08

How Hallucinations Are Measured

Researchers use standardized benchmarks to compare how often different models hallucinate. These typically involve asking models factual questions where a subtly wrong but plausible answer is a common human misconception — deliberately testing the gap between “sounds right” and “is right.”

BenchmarkWhat it testsApproach
TruthfulQACommon misconceptions817 questions designed so a plausible-sounding wrong answer mimics common false beliefs
HaluEvalGeneral hallucination detection35,000 samples across QA, summarization, and dialogue tasks
FActScoreFactual precision of long-form textBreaks generated biography-style text into atomic facts and verifies each one
SelfCheckGPTConsistency-based detectionSamples the same prompt multiple times — inconsistent answers signal likely hallucination

⚠ No Benchmark is Perfect

Hallucination rates vary wildly depending on domain, question difficulty, and whether the model has retrieval tools available. A single “hallucination rate” percentage for a model is always a simplification — the real rate depends heavily on what you’re asking.

Section 09

Real-World Consequences

⚖

Legal Filings

Multiple documented cases of lawyers submitting briefs with fabricated case citations generated by chatbots, leading to court sanctions and case dismissals.

🏥

Medical Information

Confidently wrong drug interaction or dosage claims are especially dangerous because the fluent, authoritative tone makes errors hard to spot without independent verification.

🛫

Customer Service Bots

An airline’s chatbot invented a bereavement fare policy that didn’t exist. A tribunal ruled the airline was still bound by what its bot had promised the customer.

💻

Software Supply Chain

Attackers have started publishing malicious packages under names that AI coding assistants commonly hallucinate as real library names — a technique called “slopsquatting.”

📰

News Summarization

AI news summary tools have attributed claims to sources that never made them, misrepresenting the actual reporting underneath.

🎓

Academic Research

Students and researchers citing AI-fabricated sources have had papers rejected or retracted after reviewers discovered the citations don’t exist.

Section 10

Mitigation Techniques

TechniqueHow it worksEffectiveness
RAG (Retrieval-Augmented Generation)Retrieves real documents and grounds the answer in them before generatingHigh — directly addresses the “no grounding” root cause
Lower temperatureReduces randomness in token selection for factual tasksModerate — reduces but doesn’t eliminate risk
Chain-of-thought promptingForces the model to show reasoning steps, making errors easier to catchModerate — helps with logical, not factual, hallucinations
Self-consistency checkingGenerate multiple answers and check if they agreeModerate — inconsistency signals uncertainty
RLHF fine-tuningTrains the model to say “I don’t know” when uncertainModerate — helps but models remain overconfident on edge cases
Citation requirementsForces the model to link every claim to a real retrievable sourceHigh — makes fabrication immediately checkable
Human reviewA person verifies high-stakes outputs before they’re usedHighest — the only currently reliable safeguard for critical use cases

Section 11

Worked Example — Grounding with RAG

Retrieval-Augmented Generation (RAG) is the most effective widely-used mitigation. Instead of relying purely on the model’s compressed training memory, the system first retrieves real, current documents and feeds them into the prompt as context.

❌ Without RAG
Prompt: “What is our company’s current refund policy?”

Model has no access to your company’s actual policy documents — it generates a plausible-sounding generic refund policy based on common patterns from training data, presented as if it were your specific company’s real policy.
✅ With RAG
Prompt: “What is our company’s current refund policy?”

System first retrieves your actual policy document, inserts its real text into the prompt, then asks the model to answer using only that text. The model now has real grounding — reducing (not eliminating) fabrication risk to whether the model accurately reads the provided text.

✅ Why Grounding Helps but Doesn’t Solve It

RAG dramatically reduces factual hallucinations because the model is now paraphrasing real retrieved text instead of reconstructing from compressed memory. But it doesn’t eliminate the risk entirely — the model can still misread, misquote, or blend the retrieved documents with its own memory (an intrinsic hallucination against the very source it was given).

Section 12

Code Example — Sampling Parameters

Here is how temperature and top-p (nucleus sampling) are actually set when calling a language model API — the same parameters demonstrated in the interactive Temperature demo above.

Python
import anthropic

client = anthropic.Anthropic()

# Low temperature — for factual, deterministic tasks
# Minimizes risk of low-probability token selection
response_factual = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=500,
    temperature=0.0,  # deterministic, most-likely token every time
    messages=[{"role": "user", "content": "What year did WWII end?"}]
)

# Higher temperature — for creative, exploratory tasks
# Accepts more hallucination risk in exchange for variety
response_creative = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=500,
    temperature=1.0,
    messages=[{"role": "user", "content": "Write a whimsical short story about rain."}]
)

# RAG pattern — ground the model in real retrieved text
def answer_with_grounding(question, retrieved_docs):
    context = "\n\n".join(retrieved_docs)
    prompt = f"""Answer ONLY using the context below.
If the answer is not in the context, say "I don't have that information."

Context:
{context}

Question: {question}"""

    return client.messages.create(
        model="claude-sonnet-5",
        max_tokens=500,
        temperature=0.0,  # low temp — stick close to retrieved facts
        messages=[{"role": "user", "content": prompt}]
    )

⚠ Temperature Zero Isn’t a Cure

Setting temperature to 0.0 reduces randomness-driven hallucination, but does nothing for hallucinations that come from a genuinely wrong top-probability token — if the model’s single most confident answer is itself incorrect, temperature 0 will reproduce that error every single time, consistently.

Section 13

Knowledge Quiz

Click a question to expand it, then pick your answer.

A hallucination is fabricated content generated with the same fluency and confidence as accurate content. It’s not intentional deception (the model has no concept of “hiding” the truth) and it’s not simply a matter of opinion — it’s a factual or logical error presented as fact.
Language models compute a probability distribution over the next token and sample from it. There is no built-in fact-verification step in that process — plausibility, not truth, is what’s being optimized during generation. Truth and plausibility usually align, but not always.
Intrinsic hallucinations contradict the provided source — e.g., summarizing a document and stating something the document explicitly denies. This is distinct from extrinsic hallucination, where the model adds unverifiable new claims not confirmed OR denied by the source.
Temperature controls how randomly tokens are sampled from the probability distribution. Higher temperature flattens the distribution, giving unlikely (and often wrong) tokens more chance of selection. But temperature 0 isn’t a fix either — if the single most probable token is itself wrong, temperature 0 will reproduce that error consistently every time.
RAG retrieves real, current documents and inserts them into the prompt as context, letting the model paraphrase actual text instead of reconstructing facts from lossy compressed memory. It significantly reduces — but does not fully eliminate — hallucination risk, since the model can still misread or misquote the retrieved text.
The model has learned the statistical *pattern* of citations extremely well — journal names, volume numbers, page ranges, author formatting — even when the specific paper doesn’t exist. This makes fabricated citations look completely legitimate at a glance, as demonstrated by real legal cases where fake case citations passed initial review.

The post AI Hallucinations: Why They Happen — Explained with Real Examples appeared first on Learn With Examples.

]]>
https://learnwithexamples.org/ai-hallucinations-explained-with-real-examples/feed/ 0 787
What is a Limit? — The Foundation of Calculus Explained with Real Examples https://learnwithexamples.org/limit-the-foundation-of-calculus/ https://learnwithexamples.org/limit-the-foundation-of-calculus/#respond Thu, 16 Jul 2026 14:44:01 +0000 https://learnwithexamples.org/?p=769 The post What is a Limit? — The Foundation of Calculus Explained with Real Examples appeared first on Learn With Examples.

]]>
What is a Limit? — The Foundation of Calculus Explained with Real Examples

Calculus · Mathematics · Beginner

What is a Limit? — The Foundation of Calculus Explained with Real Examples

Every idea in calculus — derivatives, integrals, continuity — is built on one concept: the limit. It answers the question “what value does a function approach as we get infinitely close to a point?” This guide explains limits from scratch, with real examples, a live table explorer, a graph visualiser, and worked problems.

📖 18 min read 📊 Live limit explorer 📈 Interactive graph 🧮 Worked examples 💻 Python code ❓ Quiz included

Section 01

The Intuition — Approaching Without Arriving

Imagine you are walking toward a wall. With every step you halve the remaining distance. You get closer and closer — but the question a limit asks is not “did you reach the wall?” It asks: “what value are you approaching?”

This distinction is the entire heart of calculus. A limit describes the behaviour of a function near a point — not necessarily at the point itself. The function might not even be defined at that exact point, yet the limit can still exist perfectly clearly.

💡 The Key Idea

The limit of f(x) as x approaches a is the value that f(x) gets arbitrarily close to as x gets arbitrarily close to a — from either side. The actual value of f(a) is irrelevant to the limit.

A concrete example — the speed of a car

At exactly the instant t = 2 seconds, what is a car’s speed? Speed is distance divided by time — but over exactly zero seconds, that is 0/0, which is undefined. Yet the car clearly has a speed. Limits solve this: we measure the average speed over smaller and smaller time intervals approaching zero, and ask what value that average approaches. That approaching value is the instantaneous speed — and it is a limit.

lim
The notation used — read as “the limit as x approaches a”
Approaches — x gets close to a but never has to equal a
L
The limit value — what f(x) gets arbitrarily close to
ε-δ
The rigorous definition — epsilon-delta, used in real analysis

Section 02

The Formal Definition of a Limit

We write a limit like this:

lim f(x) = L    (as x → a)
Read as: “The limit of f(x) as x approaches a equals L”

This means: for every number ε > 0 (no matter how tiny), there exists a δ > 0 such that whenever 0 < |x − a| < δ, we have |f(x) − L| < ε. In plain English: we can make f(x) as close to L as we like, by making x close enough to a.

📌 Plain English Translation

As x inches toward a (from either side, never touching), f(x) inches toward L. The closer x gets to a, the closer f(x) gets to L. If f(x) settles on a single value L — that is the limit.

Worked Example — a simple limit

Find: lim (x² − 1) / (x − 1) as x → 1

Plugging in x = 1 directly gives 0/0 — undefined. But factor the numerator: (x² − 1) = (x − 1)(x + 1). Cancel the (x − 1) terms:

lim (x² − 1)/(x − 1)  as x → 1
= lim (x − 1)(x + 1) / (x − 1)  as x → 1
= lim (x + 1)  as x → 1    (cancelled (x-1), valid since x ≠ 1)
= 1 + 1 = 2

The function (x² − 1)/(x − 1) is undefined at x = 1 — there is a hole in the graph. But as x approaches 1 from either side, the function approaches 2. The limit exists and equals 2, even though f(1) does not exist.

Section 03

Interactive Limit Table Explorer

Choose a function and a target value. The table shows f(x) for values of x approaching the target from both sides — getting closer and closer. Watch the values converge to the limit.

  Limit Table Explorer

Section 04

Interactive Limit Graph Visualiser

See the same functions plotted. The vertical dashed line marks the target x value. The horizontal dashed line marks the limit L. Notice the function approaches L as it nears the dashed line — even if there is a hole at that exact point.

  Limit Graph Visualiser

Section 05

One-Sided Limits — Left and Right

Sometimes a function approaches different values depending on which side of a you come from. We call these one-sided limits:

Left-Hand Limit
lim f(x) as x → a⁻
x approaches a from the left (values smaller than a)
Right-Hand Limit
lim f(x) as x → a⁺
x approaches a from the right (values larger than a)

✅ The Golden Rule

The two-sided limit lim f(x) as x → a exists if and only if both one-sided limits exist and are equal:

lim f(x) = L  ⟺  lim f(x) [x→a⁻] = lim f(x) [x→a⁺] = L

Example — the jump function f(x) = |x|/x

This function equals −1 for all negative x and +1 for all positive x. As x → 0⁻ (from the left), f(x) → −1. As x → 0⁺ (from the right), f(x) → +1. The two sides disagree — so the two-sided limit does not exist.

⚠️ When the Limit Does Not Exist

A limit fails to exist when: the left and right limits are different (jump), the function oscillates infinitely without settling (like sin(1/x) near 0), or the function grows without bound toward ±∞.

Section 06

Limit Laws — The Arithmetic of Limits

Once you know individual limits, you can combine them using the Limit Laws — just like rules of arithmetic, but for limits. Assume lim f(x) = L and lim g(x) = M as x → a.

LawStatementExample
Sum Rulelim [f(x) + g(x)] = L + Mlim (x² + x) = lim x² + lim x = 4 + 2 = 6 at x→2
Difference Rulelim [f(x) − g(x)] = L − Mlim (x² − x) = 4 − 2 = 2 at x→2
Product Rulelim [f(x) · g(x)] = L · Mlim (x · x²) = 2 · 4 = 8 at x→2
Quotient Rulelim [f(x)/g(x)] = L/M  (M ≠ 0)lim (x²/x) = 4/2 = 2 at x→2
Power Rulelim [f(x)]ⁿ = Lⁿlim (x+1)³ = (3)³ = 27 at x→2
Constant Rulelim c = clim 7 = 7 — constants are their own limit
Squeeze TheoremIf g(x) ≤ f(x) ≤ h(x) and lim g = lim h = L, then lim f = LUsed to prove lim sin(x)/x = 1 as x→0

Section 07

Evaluation Techniques — 4 Methods

🔌

1. Direct Substitution

If f is continuous at a, simply plug in x = a. Works for polynomials, trig functions, exponentials. Always try this first.

✂️

2. Factoring

For 0/0 indeterminate forms — factor numerator and/or denominator, cancel the common (x − a) factor, then substitute.

✖️

3. Rationalisation

When square roots cause the 0/0 form — multiply numerator and denominator by the conjugate to eliminate the root.

📐

4. Squeeze Theorem

Bound the tricky function between two easier functions with the same limit. Classic use: proving lim sin(x)/x = 1 as x→0.

Worked Example — Rationalisation

Find: lim (√x − 2) / (x − 4) as x → 4

Direct substitution gives 0/0. Multiply by the conjugate (√x + 2)/(√x + 2):

(√x − 2)/(x − 4) × (√x + 2)/(√x + 2)
= (x − 4) / [(x − 4)(√x + 2)]
= 1 / (√x + 2)    (cancelled (x−4))
→ 1 / (√4 + 2) = 1/4 as x → 4

Section 08

Special Limits — The Ones Every Calculus Student Needs

These four limits appear constantly in calculus. Memorise them — they are used in derivative proofs, Taylor series, and differential equations.

LimitValueWhy it matters
lim sin(x)/x   (x→0) 1 Foundation of the derivative of sin(x). Proved using the Squeeze Theorem.
lim (1−cos x)/x   (x→0) 0 Pairs with sin(x)/x in derivative proofs for trigonometric functions.
lim (1 + 1/n)ⁿ   (n→∞) e ≈ 2.71828 Definition of Euler’s number e. Foundation of exponential functions and natural log.
lim (eˣ − 1)/x   (x→0) 1 Used to prove the derivative of eˣ is itself — the most important derivative in calculus.

🎯 Proving lim sin(x)/x = 1 with the Squeeze Theorem

For 0 < x < π/2, we can prove geometrically that: cos(x) ≤ sin(x)/x ≤ 1 Since lim cos(x) = 1 and lim 1 = 1 as x→0, by the Squeeze Theorem lim sin(x)/x = 1. This result underpins the derivative of every trigonometric function.

Section 09

Limits and Continuity — The Connection

A function f is continuous at x = a if three conditions all hold simultaneously:

1

f(a) is defined

The function has an actual value at x = a — no hole, no division by zero, no undefined expression.

2

lim f(x) as x → a exists

The left-hand limit and right-hand limit both exist and are equal.

3

lim f(x) = f(a)

The limit equals the actual function value. The function arrives exactly where it was heading.

Type of DiscontinuityWhat breaksExampleLimit exists?
Removable (hole)f(a) undefined or ≠ limit(x²−1)/(x−1) at x=1Yes — L = 2
JumpLeft ≠ right limit|x|/x at x=0No
InfiniteFunction → ±∞1/x at x=0No (diverges)
OscillatingNo settled valuesin(1/x) at x=0No

Section 10

Limits at Infinity

What happens to f(x) as x grows without bound? This is a limit at infinity, written lim f(x) as x → ∞. The value L (if it exists) is called a horizontal asymptote of the graph.

lim (3x² + 2x) / (x² + 5)   as x → ∞
Divide every term by x² (the highest power) → lim (3 + 2/x) / (1 + 5/x²) = 3/1 = 3
FunctionLimit as x → ∞Interpretation
1/x0Approaches the x-axis — horizontal asymptote at y=0
x / (x + 1)1Horizontal asymptote at y=1
Grows without bound — no horizontal asymptote
(3x+1) / (2x−5)3/2Leading coefficients determine the asymptote
Exponential growth — grows faster than any polynomial
e⁻ˣ0Exponential decay — approaches x-axis

📐 Rule of Thumb for Rational Functions

Compare the degree of numerator (top) and denominator (bottom):

• Top degree < bottom degree → limit = 0
• Top degree = bottom degree → limit = ratio of leading coefficients
• Top degree > bottom degree → limit = ±∞ (no horizontal asymptote)

Section 11

Real-World Uses of Limits

🚗

Instantaneous Speed

A speedometer shows speed at one instant — not an average. This is the limit of average speed as the time interval shrinks to zero. The foundation of derivatives.

💰

Compound Interest

When interest compounds continuously, the formula uses e — which is itself defined as a limit: e = lim (1 + 1/n)ⁿ as n → ∞. Banks literally use limits to calculate your savings.

🎮

Physics Engines

Game physics uses numerical limits constantly — approximating instantaneous velocity, forces and collisions by computing values over smaller and smaller time steps.

📡

Signal Processing

The Fourier transform — used in audio compression (MP3), image compression (JPEG), and 5G — is defined using integrals, which are themselves defined using limits of sums.

🏥

Drug Concentration

Pharmacologists model how drug concentration in your blood approaches zero over time using limits as t → ∞. This determines dosage intervals.

🌡️

Heat Transfer

Newton’s law of cooling describes how an object’s temperature approaches room temperature — modelled as a limit of an exponential function as time → ∞.

Section 12

Python Code

Computing limits numerically

Python
import numpy as np

def numerical_limit(f, a, side='both', steps=10):
    """Estimate limit of f as x → a by computing f near a."""
    results = []
    for i in range(1, steps+1):
        h = 10**(-i)   # 0.1, 0.01, 0.001, ...
        if side in ('both', 'right'):
            results.append((f'x={a+h:.{i}f}', f(a + h)))
        if side in ('both', 'left'):
            results.append((f'x={a-h:.{i}f}', f(a - h)))
    return results

# Example 1: lim (x²−1)/(x−1) as x → 1
f1 = lambda x: (x**2 - 1) / (x - 1)
for label, val in numerical_limit(f1, 1):
    print(f"{label:12} → f(x) = {val:.8f}")
# All values approach 2.0

# Example 2: lim sin(x)/x as x → 0
f2 = lambda x: np.sin(x) / x
for label, val in numerical_limit(f2, 0, side='right'):
    print(f"{label:14} → {val:.10f}")
# All values approach 1.0

Symbolic limits with SymPy

Python
from sympy import *

x = Symbol('x')

# lim (x²−1)/(x−1) as x → 1
expr1 = (x**2 - 1) / (x - 1)
print(limit(expr1, x, 1))         # 2

# lim sin(x)/x as x → 0
print(limit(sin(x)/x, x, 0))     # 1

# lim (1 + 1/x)^x as x → ∞  (definition of e)
print(limit((1 + 1/x)**x, x, oo)) # E  (Euler's number)

# lim (√x−2)/(x−4) as x → 4
print(limit((sqrt(x)-2)/(x-4), x, 4))  # 1/4

# One-sided limits
print(limit(Abs(x)/x, x, 0, '+'))  # 1  (right)
print(limit(Abs(x)/x, x, 0, '-'))  # -1 (left)

# Limit at infinity
print(limit((3*x**2 + 2*x) / (x**2 + 5), x, oo))  # 3

Section 13

Knowledge Quiz

Six questions to test your understanding of limits.

  Limits Quiz
Question 1 of 6

The post What is a Limit? — The Foundation of Calculus Explained with Real Examples appeared first on Learn With Examples.

]]>
https://learnwithexamples.org/limit-the-foundation-of-calculus/feed/ 0 769
AVL Trees Explained — Self-Balancing Trees That Never Get Lopsided https://learnwithexamples.org/avl-trees-explained/ https://learnwithexamples.org/avl-trees-explained/#respond Thu, 16 Jul 2026 11:34:28 +0000 https://learnwithexamples.org/?p=758 AVL Trees Explained — Self-Balancing Trees That Never Get Lopsided Data Structures · Algorithms Learn With Examples A regular binary search tree can degrade into a linked list — making…

The post AVL Trees Explained — Self-Balancing Trees That Never Get Lopsided appeared first on Learn With Examples.

]]>
AVL Trees Explained — Self-Balancing Trees That Never Get Lopsided

Data Structures · Algorithms

Learn With Examples

A regular binary search tree can degrade into a linked list — making every search O(n) instead of O(log n). AVL trees fix this with automatic rotations that keep the tree perfectly balanced after every insert and delete. This guide walks you through the balance factor, all four rotation types, and lets you build and balance an AVL tree interactively in real time.

📖 20 min read 🌲 Live AVL tree builder 🔄 Rotation visualiser 💻 Python code ❓ Quiz included

Section 01

The Problem with Unbalanced BSTs

A Binary Search Tree (BST) is a beautiful data structure: every node’s left child is smaller, every right child is larger. Searching is O(log n) — you eliminate half the tree at each step. In theory.

In practice, the order you insert values determines the shape of the tree. Insert values in sorted order — 1, 2, 3, 4, 5 — and you get this:

That is not a tree — it is a linked list. Searching for 5 now requires visiting every single node. Your O(log n) search has degraded to O(n). The whole advantage of a BST is gone.

O(log n)
Balanced BST search — halves remaining nodes each step
O(n)
Degenerate BST search — visits every node in the worst case
1962
Year AVL trees were invented by Adelson-Velsky and Landis
±1
Maximum allowed balance factor in any AVL node

⚠️ The Real-World Risk

Any application that inserts data in sorted or near-sorted order — timestamps, auto-incrementing IDs, alphabetical names — is at risk of BST degeneration. This is why production databases and language runtimes use self-balancing trees, not plain BSTs.

Section 02

What is an AVL Tree?

An AVL tree (named after its inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. It maintains all the BST properties — left child smaller, right child larger — but adds one additional constraint:

|height(left subtree) − height(right subtree)| ≤ 1
For every node in the tree — at every level

After every insertion or deletion, the AVL tree checks this constraint at every affected node. If any node becomes unbalanced, it performs one or two rotations — local restructuring operations — to restore balance. The tree never needs to be rebuilt from scratch.

📏

Height of a Node

The number of edges on the longest path from that node to a leaf. A leaf node has height 0. A null pointer has height −1.

⚖️

Balance Factor

BF = height(left) − height(right). In a valid AVL tree, every node’s BF is −1, 0, or +1. Any other value triggers a rotation.

🔄

Rotations

Local restructuring of 2–3 nodes that restores balance without violating the BST property. There are four types: LL, RR, LR, RL.

📐

Guaranteed Height

An AVL tree with n nodes has height at most 1.44 × log₂(n). A degenerate BST has height n. This gap is enormous for large n.

Section 03

The Balance Factor — The Key Metric

Every node in an AVL tree stores a balance factor (BF) — the difference between the height of its left subtree and the height of its right subtree.

Balance FactorMeaningAVL Valid?Action
BF = −2Right subtree is 2 tallerInvalidRotate left (RR or RL)
BF = −1Right subtree is 1 tallerValidNo action needed
BF = 0Both subtrees equal heightValid — perfectNo action needed
BF = +1Left subtree is 1 tallerValidNo action needed
BF = +2Left subtree is 2 tallerInvalidRotate right (LL or LR)

📌 Stored or Computed?

In most implementations the balance factor (or simply the height) is stored at each node and updated during insertions and deletions. This makes it O(1) to check — no need to traverse the subtree to measure height every time.

Python
class AVLNode:
    def __init__(self, key):
        self.key    = key
        self.left   = None
        self.right  = None
        self.height = 1   # new node starts at height 1

def get_height(node):
    return node.height if node else 0

def get_balance(node):
    return get_height(node.left) - get_height(node.right) if node else 0

def update_height(node):
    node.height = 1 + max(get_height(node.left), get_height(node.right))

Section 04

The Four Rotations — How AVL Rebalances

When the balance factor at a node becomes ±2, the AVL tree performs a rotation. There are four types, each handling a different pattern of imbalance. They all share one beautiful property: they restore balance while preserving the BST ordering property.

CaseWhen it happensBF of unbalanced nodeBF of childFix
LL (Left-Left)Insertion in left subtree of left child+2≥ 0Single right rotation
RR (Right-Right)Insertion in right subtree of right child−2≤ 0Single left rotation
LR (Left-Right)Insertion in right subtree of left child+2< 0Left rotate child, then right rotate node
RL (Right-Left)Insertion in left subtree of right child−2> 0Right rotate child, then left rotate node

Right Rotation (LL case)

The unbalanced node z has BF = +2. Its left child y is taller. We rotate right around z — y takes z’s position, z becomes y’s right child.

Python — Right Rotation
def rotate_right(z):
    y = z.left          # y is the left child of z
    T3 = y.right        # T3 is the right subtree of y

    # Perform rotation
    y.right = z         # z becomes the right child of y
    z.left  = T3        # T3 moves to z's left (BST property preserved)

    # Update heights (z first, then y since y is now higher)
    update_height(z)
    update_height(y)

    return y             # y is the new root of this subtree

Left Rotation (RR case)

Python — Left Rotation
def rotate_left(z):
    y = z.right         # y is the right child of z
    T2 = y.left         # T2 is the left subtree of y

    # Perform rotation
    y.left  = z         # z becomes the left child of y
    z.right = T2        # T2 moves to z's right

    update_height(z)
    update_height(y)

    return y             # y is the new root

🔄 Double Rotations

LR and RL cases require two rotations. For LR: first rotate the left child leftward (turning LR into LL), then rotate the unbalanced node rightward. For RL: first rotate the right child rightward (turning RL into RR), then rotate the unbalanced node leftward. After a double rotation the tree is always balanced.

Section 05

Interactive Rotation Visualiser

Click any rotation to see the before and after states side by side. Notice how the BST ordering property is preserved — an in-order traversal of both trees gives the same sequence.

  Rotation Visualiser
Before (Unbalanced)
After (Balanced)
Click a rotation type above to see how it works.

Section 06

Live AVL Tree Builder

Insert and delete values to build your own AVL tree. The tree rebalances automatically after every operation. Balance factors are shown inside each node — green means balanced, red means a rotation just happened.

  AVL Tree Builder
Insert a value to begin building the AVL tree.

Section 07

Insertion Algorithm — Step by Step

AVL insertion combines standard BST insertion with a recursive rebalancing pass back up to the root.

1

Insert like a normal BST

Recursively traverse the tree comparing the new key with each node. Go left if smaller, right if larger. Insert at the correct null position.

2

Update heights on the way back up

As the recursion unwinds, update the height of each ancestor node: height = 1 + max(height(left), height(right)).

3

Check balance factor at each ancestor

Compute BF = height(left) − height(right) at each node. If |BF| ≤ 1, the node is balanced. Continue up the tree.

4

Identify the case and rotate if BF = ±2

Determine which of the four cases applies (LL, RR, LR, RL) by checking the balance factor of the child. Perform the appropriate single or double rotation.

5

Continue checking upward

After a rotation, continue checking balance factors up to the root. In practice at most one rotation is needed per insertion (but deletion may need O(log n) rotations).

Python — Full AVL Insert
def insert(node, key):
    # Step 1: Standard BST insertion
    if not node:
        return AVLNode(key)

    if key < node.key:
        node.left  = insert(node.left,  key)
    elif key > node.key:
        node.right = insert(node.right, key)
    else:
        return node  # duplicate key — ignore

    # Step 2: Update height
    update_height(node)

    # Step 3: Get balance factor
    bf = get_balance(node)

    # Step 4: Determine case and rotate
    # LL Case
    if bf > 1 and key < node.left.key:
        return rotate_right(node)

    # RR Case
    if bf < -1 and key > node.right.key:
        return rotate_left(node)

    # LR Case
    if bf > 1 and key > node.left.key:
        node.left = rotate_left(node.left)
        return rotate_right(node)

    # RL Case
    if bf < -1 and key < node.right.key:
        node.right = rotate_right(node.right)
        return rotate_left(node)

    return node  # node is balanced — no rotation needed

Section 08

Time and Space Complexity

OperationAverageWorst CaseWhy
SearchO(log n)O(log n)Height is always ≤ 1.44 log₂(n) — guaranteed
InsertO(log n)O(log n)BST insert + at most 2 rotations + height updates up to root
DeleteO(log n)O(log n)BST delete + up to O(log n) rotations on the path back to root
SpaceO(n)O(n)One node per element; each stores key, left, right, height

✅ The Guarantee that Matters

The key advantage of AVL trees over plain BSTs is that all operations are O(log n) in the worst case — guaranteed, regardless of insertion order. A plain BST can degrade to O(n). The cost is slightly higher constant factors due to rotation bookkeeping.

Section 09

AVL vs Red-Black Trees vs Plain BST

PropertyPlain BSTAVL TreeRed-Black Tree
Search worst caseO(n)O(log n)O(log n)
Insert worst caseO(n)O(log n)O(log n)
Rotations per insert0At most 2At most 2
Rotations per delete0O(log n)At most 3
Balance strictnessNoneStrict (BF ≤ 1)Loose (height ≤ 2 log n)
Search speedVariesFaster (shorter tree)Slightly slower
Insert/Delete speedVariesSlightly slowerFaster
Best use caseStatic dataRead-heavy workloadsWrite-heavy workloads
Used inSimple lookupsDatabases, compilersLinux kernel, Java TreeMap

💡 Which to Choose?

Choose AVL trees when your workload is read-heavy — the stricter balance means a shorter tree and faster lookups. Choose Red-Black trees when your workload is write-heavy — fewer rotations on deletion makes writes cheaper. In practice, most language standard libraries use Red-Black trees (Java’s TreeMap, C++ std::map) for their balanced write performance.

Section 10

Real-World Uses of AVL Trees

ApplicationHow AVL trees help
Database indexesMany database engines use AVL or similar balanced trees for in-memory indexes where read performance is critical. Guaranteed O(log n) lookup regardless of data insertion order.
Compilers (symbol tables)Compilers store identifiers (variable names, functions) in AVL trees for fast O(log n) lookup during compilation. GCC historically used AVL trees.
Memory allocatorsSome memory allocators track free memory blocks in AVL trees, enabling fast O(log n) search for a block of the right size.
Geometry / computational geometrySweep-line algorithms for intersections, polygon clipping, and spatial queries use balanced BSTs (including AVL) to maintain the event queue.
Network routing tablesFast prefix lookups for IP routing can be implemented with AVL trees in software routers, giving guaranteed lookup time per packet.
Python’s sortedcontainers libraryThe SortedList, SortedDict, and SortedSet in Python’s popular sortedcontainers library use B-tree variants inspired by AVL balancing.

Section 11

Complete Python Implementation

Python — Complete AVL Tree
class AVLNode:
    def __init__(self, key):
        self.key = key; self.left = self.right = None; self.height = 1

class AVLTree:
    def _h(self, n): return n.height if n else 0
    def _bf(self, n): return self._h(n.left) - self._h(n.right) if n else 0
    def _upd(self, n): n.height = 1 + max(self._h(n.left), self._h(n.right))

    def _rr(self, z):   # right rotation
        y = z.left; z.left = y.right; y.right = z
        self._upd(z); self._upd(y); return y

    def _lr(self, z):   # left rotation
        y = z.right; z.right = y.left; y.left = z
        self._upd(z); self._upd(y); return y

    def _balance(self, node, key):
        self._upd(node)
        bf = self._bf(node)
        if bf > 1:
            if key > node.left.key: node.left = self._lr(node.left)  # LR
            return self._rr(node)                                       # LL
        if bf < -1:
            if key < node.right.key: node.right = self._rr(node.right) # RL
            return self._lr(node)                                       # RR
        return node

    def insert(self, root, key):
        if not root: return AVLNode(key)
        if   key < root.key: root.left  = self.insert(root.left,  key)
        elif key > root.key: root.right = self.insert(root.right, key)
        else: return root
        return self._balance(root, key)

    def _min_node(self, n):
        while n.left: n = n.left
        return n

    def delete(self, root, key):
        if not root: return root
        if   key < root.key: root.left  = self.delete(root.left,  key)
        elif key > root.key: root.right = self.delete(root.right, key)
        else:
            if not root.left:  return root.right
            if not root.right: return root.left
            temp = self._min_node(root.right)
            root.key = temp.key
            root.right = self.delete(root.right, temp.key)
        return self._balance(root, root.key)

    def inorder(self, root):
        return (self.inorder(root.left) + [root.key] + self.inorder(root.right)) if root else []

# Usage
tree = AVLTree()
root = None
for v in [10, 20, 30, 40, 50, 25]:   # sorted order — would break plain BST
    root = tree.insert(root, v)

print("Inorder:", tree.inorder(root))    # → [10, 20, 25, 30, 40, 50]
print("Height:", root.height)             # → 3 (not 6 as it would be unbalanced)

root = tree.delete(root, 20)
print("After delete:", tree.inorder(root)) # → [10, 25, 30, 40, 50]

Section 12

Knowledge Quiz

Six questions to test your AVL tree understanding.

  AVL Trees Quiz
Question 1 of 6

The post AVL Trees Explained — Self-Balancing Trees That Never Get Lopsided appeared first on Learn With Examples.

]]>
https://learnwithexamples.org/avl-trees-explained/feed/ 0 758
Outliers in Statistics — How to Detect, Handle and Never Ignore Them https://learnwithexamples.org/outliers-in-statistics/ https://learnwithexamples.org/outliers-in-statistics/#respond Thu, 16 Jul 2026 11:22:02 +0000 https://learnwithexamples.org/?p=754 Outliers in Statistics — How to Detect, Handle and Never Ignore Them Statistics · Mathematics One rogue data point can wreck an average, break a machine learning model, or hide…

The post Outliers in Statistics — How to Detect, Handle and Never Ignore Them appeared first on Learn With Examples.

]]>
Outliers in Statistics — How to Detect, Handle and Never Ignore Them

Statistics · Mathematics

One rogue data point can wreck an average, break a machine learning model, or hide a medical breakthrough. Outliers are the most misunderstood values in statistics — sometimes noise, sometimes the most important signal in your dataset. This guide shows you how to find them, what to do with them, and when removing them is actually the wrong call.

📖 18 min read 🔍 Interactive outlier detector 📊 Live dot plot and box plot 💻 Python code ❓ Quiz included

Section 01

What is an Outlier?

An outlier is a data point that lies far away from the rest of the observations in a dataset. It is a value that is unusually high or low compared to the typical range of the data.

Consider the salaries of five employees: £28k, £31k, £29k, £33k, £420k. That last value — £420k — is an outlier. It could be the CEO’s salary, a data entry error, or a completely different type of employee included by mistake. Each explanation calls for a different response.

Dot plot — spot the outlier

📌 No Universal Threshold

There is no single agreed-upon definition of “how far is too far.” Different detection methods use different thresholds. An outlier is always defined relative to the rest of the data and the context of the problem.

1.5×
IQR — the classic Tukey fence threshold for mild outliers
IQR — extreme outlier threshold used in robust statistics
±3σ
Z-score threshold — only 0.27% of normal data falls beyond this
±2σ
Conservative threshold used in some clinical and quality contexts

Section 02

Why Outliers Matter — The Impact on Statistics

Outliers can have a dramatic or minimal effect depending on which statistical measure you use. The mean is highly sensitive. The median is almost immune. Understanding this asymmetry is one of the most practical skills in applied statistics.

Salary example — the CEO effect

A team of 10 employees. Nine earn between £28,000 and £35,000. One is the CEO earning £500,000.

Including CEO (outlier present)

£78,200
Mean salary — wildly misleading
Nobody actually earns near this figure

Median (resistant to outlier)

£31,000
Median salary — accurate picture
Reflects what most employees actually earn
StatisticSensitivity to OutliersWhy
MeanHighly sensitiveEvery value is used in the calculation — one extreme value pulls the result
MedianVery resistantOnly cares about position — outliers don’t change the middle value
ModeResistantOnly counts frequency — one extreme value rarely changes the most common
Standard DeviationHighly sensitiveSquared differences amplify the effect of values far from the mean
IQRVery resistantBased on middle 50% of data — outliers in the tails don’t affect it
Correlation (r)Very sensitiveA single outlier can flip the sign of r from positive to negative
Linear RegressionHighly sensitiveThe regression line is pulled toward influential outliers (leverage points)
RangeMaximally sensitiveDefined entirely by the minimum and maximum — outliers determine it

💡 Resistance vs Sensitivity

Statistics that use position (median, IQR, quantiles) are called robust or resistant — they are unaffected by outliers. Statistics that use all values in arithmetic calculations (mean, standard deviation, variance) are non-robust and can be severely distorted by even a single outlier.

Section 03

Types of Outliers

Not all outliers are the same. Understanding why a value is an outlier determines what you should do with it.

TypeCauseExampleAction
Error outlier Data entry mistake, sensor malfunction, recording error Age recorded as 220 instead of 22; a weight sensor reading 0kg due to calibration failure Investigate and correct or remove
Natural outlier Genuine extreme value — real variation in the population Usain Bolt’s 100m time in a dataset of sprinters; a billionaire in income data Keep — it is real and may be meaningful
Interesting outlier Unexpected anomaly that signals something important An unusually high credit card transaction (fraud detection); a sudden spike in disease cases Investigate further — it may be the most important data point
Structural outlier Data from a different subgroup accidentally mixed in Adult heights in a children’s dataset; a commercial property in a residential price dataset Separate into correct group; do not simply delete

⚠️ Classify Before You Act

The most important question is: why is this value an outlier? Answering that question determines everything — whether to keep it, remove it, investigate it, or report it. Blindly removing outliers without understanding their cause is one of the most common errors in data analysis.

Section 04

How to Detect Outliers — 4 Methods

📏

Z-Score Method

Calculate how many standard deviations each value is from the mean. Values with |z| > 3 are outliers. Best for normally distributed data.

z = (x − μ) / σ
Best for: normal distributions
📦

IQR / Tukey Fences

Uses the interquartile range. Values below Q1 − 1.5×IQR or above Q3 + 1.5×IQR are mild outliers. 3×IQR for extreme outliers. Robust to skewed data.

Lower = Q1 − 1.5×IQR
Upper = Q3 + 1.5×IQR
Best for: skewed data
🔬

Modified Z-Score

Uses the median and Median Absolute Deviation (MAD) instead of mean and standard deviation. More robust than regular Z-score. Values with |M| > 3.5 are outliers.

M = 0.6745 × (x − median) / MAD
Best for: small samples
👁️

Visual Methods

Box plots show outliers as individual dots beyond the whiskers. Scatter plots reveal spatial outliers. Histograms show isolated bars. Always visualise before calculating.

Best for: first exploration

IQR Method — Worked Example

Dataset: 12, 15, 14, 10, 18, 14, 13, 100, 16, 11

1

Sort the data

10, 11, 12, 13, 14, 14, 15, 16, 18, 100

2

Find Q1 and Q3

Q1 (25th percentile) = 11.5  |  Q3 (75th percentile) = 15.5

3

Calculate IQR

IQR = Q3 − Q1 = 15.5 − 11.5 = 4.0

4

Calculate fences

Lower fence = 11.5 − (1.5 × 4.0) = 5.5
Upper fence = 15.5 + (1.5 × 4.0) = 21.5

5

Flag outliers

The value 100 is above 21.5 — it is an outlier. All other values fall within [5.5, 21.5].

Section 05

Interactive Outlier Detector

Enter your own dataset (comma-separated numbers) and choose a detection method. The detector will flag outliers instantly and show you the full statistical breakdown.

  Outlier Detector
Sorted data — ■ outlier   ■ mild   ■ normal

Section 06

Visualising Outliers with Box Plots

A box plot (also called a box-and-whisker plot) is the standard visual tool for outlier detection. The box shows the IQR (Q1 to Q3). The line inside is the median. The whiskers extend to the Tukey fences. Individual dots beyond the whiskers are outliers.

Box plot — the anatomy of outlier detection
Box Plot ElementWhat it showsStatistical meaning
Left edge of boxQ125th percentile — 25% of data is below this
Line inside boxMedian (Q2)50th percentile — the true middle
Right edge of boxQ375th percentile — 75% of data is below this
Box widthIQR = Q3 − Q1Middle 50% of the data — resistant to outliers
WhiskersQ1 − 1.5×IQR to Q3 + 1.5×IQRThe “expected” range — Tukey fences
Individual dotsOutliersAny value beyond the whiskers

Section 07

How to Handle Outliers — 5 Strategies

After detecting an outlier, you have five options. The right one depends on the type of outlier and your analytical goal.

🗑️

1. Remove it

Delete the value from the dataset. Only appropriate for confirmed error outliers where the value is demonstrably wrong. Never remove without documenting why.

✏️

2. Correct it

Fix the error if the true value is known. A weight of 700kg that should be 70kg can be corrected. Always preferable to removal when the correct value is recoverable.

🔒

3. Cap it (Winsorising)

Replace the outlier with the nearest fence value (e.g. Q3 + 1.5×IQR). Keeps the data point in the analysis but limits its influence. Common in financial modelling.

📐

4. Transform the data

Apply a log, square root, or Box-Cox transformation to compress the scale. Outliers become less extreme. Useful when skewed data is the underlying issue.

🛡️

5. Use robust methods

Switch to statistics resistant to outliers: use median instead of mean, IQR instead of standard deviation, robust regression instead of OLS. Keeps all data intact.

✅ Decision Framework

Is it a data error? → Correct or remove it, document your decision.
Is it a real but extreme value? → Keep it, use robust statistics, report it.
Is it suspicious and unexplained? → Investigate before deciding.
Unsure? → Run your analysis both with and without the outlier and report both results.

Section 08

When You Should NEVER Remove an Outlier

The phrase “never ignore them” in the title of this article is deliberate. Outliers are sometimes the most important data points. Here is when removing them would be a serious mistake:

💳

Fraud Detection

An outlier transaction — an unusually large purchase in an unusual location — is the fraud. Removing outliers from a fraud detection model trains it to ignore the very signals it should catch.

Never remove
🦠

Disease Outbreaks

A sudden spike in illness cases in one region is an outlier in epidemiological data. That outlier is the outbreak. Smoothing it away could delay a public health response.

Never remove
🔬

Scientific Discovery

The discovery of penicillin began with an outlier — a petri dish where bacteria unexpectedly failed to grow near a mould. Fleming investigated rather than discarded.

Always investigate
🏭

Quality Control

A machine part that is far outside tolerance is an outlier in production data. That outlier signals equipment failure. Removing it from reports hides a critical manufacturing defect.

Never remove
🌍

Climate Science

Unusual temperature or CO₂ readings that deviate from historical patterns are outliers. These anomalies are often the most scientifically significant observations in the dataset.

Always investigate
📈

Market Crashes

Financial crises appear as extreme outliers in stock return data. Removing them from risk models (as many banks did pre-2008) leads to catastrophically underestimated risk.

Never remove

🔑 The Golden Rule

Never remove an outlier simply because it makes your analysis messier, your p-value higher, or your graph look cleaner. Outliers must be understood, not just eliminated. If you remove an outlier, document it explicitly, explain why, and consider reporting your analysis both ways.

Section 09

Real-World Examples

FieldThe OutlierTypeCorrect Action
Income dataBillionaires in a national income surveyNaturalUse median not mean; report separately; use log scale
Medical trialsPatient with extreme drug responseInterestingInvestigate — may indicate a genetic subgroup; never remove
Sports statisticsUsain Bolt’s 9.58s 100m world recordNaturalKeep — it is a genuine data point; report as exceptional
Customer dataCustomer age entered as 999ErrorRemove or set to null; flag for data quality review
ManufacturingComponent 50× outside toleranceInterestingHalt production; investigate equipment; never delete from log
House pricesLuxury penthouse in a suburb datasetStructuralAnalyse luxury and non-luxury properties separately
Network securityServer traffic spike at 3amInterestingInvestigate immediately — likely a breach or attack

Section 10

Python Code

IQR Method

Python
import numpy as np

data = np.array([12, 15, 14, 10, 18, 14, 13, 100, 16, 11, 9, 17])

Q1 = np.percentile(data, 25)
Q3 = np.percentile(data, 75)
IQR = Q3 - Q1

lower_fence = Q1 - 1.5 * IQR
upper_fence = Q3 + 1.5 * IQR

outliers = data[(data < lower_fence) | (data > upper_fence)]
clean    = data[(data >= lower_fence) & (data <= upper_fence)]

print(f"Q1={Q1}, Q3={Q3}, IQR={IQR}")
print(f"Fences: [{lower_fence:.1f}, {upper_fence:.1f}]")
print(f"Outliers: {outliers}")   # → [100]
print(f"Clean data: {clean}")

Z-Score Method

Python
from scipy import stats
import numpy as np

data = np.array([12, 15, 14, 10, 18, 14, 13, 100, 16, 11])

z_scores = np.abs(stats.zscore(data))
threshold = 3.0

outliers = data[z_scores > threshold]
print(f"Z-scores: {z_scores.round(2)}")
print(f"Outliers (|z| > {threshold}): {outliers}")  # → [100]

Modified Z-Score (most robust)

Python
import numpy as np

def modified_zscore(data):
    median = np.median(data)
    mad = np.median(np.abs(data - median))   # Median Absolute Deviation
    return 0.6745 * (data - median) / mad

data = np.array([12, 15, 14, 10, 18, 14, 13, 100, 16, 11])
m_scores = np.abs(modified_zscore(data))
outliers = data[m_scores > 3.5]
print(f"Outliers: {outliers}")  # → [100]

Winsorising (capping outliers)

Python
from scipy.stats import mstats
import numpy as np

data = np.array([12, 15, 14, 10, 18, 14, 13, 100, 16, 11])

# Winsorise at 5th and 95th percentiles
winsorised = mstats.winsorize(data, limits=[0.05, 0.05])
print(f"Original:   {data}")
print(f"Winsorised: {np.array(winsorised)}")
# 100 is replaced with the 95th percentile value

# Log transformation for right-skewed data
log_data = np.log1p(data)   # log(1+x) handles zeros safely
print(f"Log:        {log_data.round(2)}")
# 100 → 4.62, rest compressed into smaller range

Box plot visualisation with matplotlib

Python
import matplotlib.pyplot as plt
import numpy as np

data = [12, 15, 14, 10, 18, 14, 13, 100, 16, 11, 9, 17]

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))

# Box plot
bp = ax1.boxplot(data, vert=False, patch_artist=True,
                 flierprops=dict(marker='o', color='red', markersize=8))
bp['boxes'][0].set_facecolor('#99f6e4')
ax1.set_title('Box Plot — red dots are outliers')
ax1.set_xlabel('Value')

# Dot plot
ax2.scatter(data, [1]*len(data), alpha=0.7, s=80,
           c=['#e11d48' if x > 21 else '#0d9488' for x in data])
ax2.set_title('Dot Plot — red = outlier')
ax2.set_yticks([])

plt.tight_layout()
plt.show()

Section 11

Knowledge Quiz

Six questions to test your understanding of outliers in statistics.

  Outliers Quiz
Question 1 of 6

The post Outliers in Statistics — How to Detect, Handle and Never Ignore Them appeared first on Learn With Examples.

]]>
https://learnwithexamples.org/outliers-in-statistics/feed/ 0 754
What is Two-Factor Authentication and Why Does It Matter? https://learnwithexamples.org/what-is-two-factor-authentication/ https://learnwithexamples.org/what-is-two-factor-authentication/#respond Thu, 16 Jul 2026 06:49:56 +0000 https://learnwithexamples.org/?p=751 What is Two-Factor Authentication and Why Does It Matter? Cybersecurity · Technology Passwords alone are broken. Billions have been stolen, leaked, and sold on the dark web. Two-factor authentication (2FA)…

The post What is Two-Factor Authentication and Why Does It Matter? appeared first on Learn With Examples.

]]>
What is Two-Factor Authentication and Why Does It Matter?

Cybersecurity · Technology

Passwords alone are broken. Billions have been stolen, leaked, and sold on the dark web. Two-factor authentication (2FA) is the single most effective thing you can do to protect your accounts — and this guide explains exactly how it works, why it stops hackers, and how to set it up in minutes.

📖 16 min read 🔐 Live login simulator 🛡️ Attack scenario demo 🔑 Password strength tester ❓ Quiz included
🔓 Password only — vulnerable 🔑 Strong password — better 🛡️ Password + 2FA — secure

Section 01

The Problem — Why Passwords Fail

In theory, a strong, unique password protects your account. In practice, passwords fail in ways that have nothing to do with how strong they are.

24B
passwords exposed in data breaches in 2022 alone
80%
of hacking-related breaches involve stolen credentials
65%
of people reuse the same password across multiple sites
$6T
global cost of cybercrime annually — rising every year

Passwords fail for several main reasons — and crucially, none of them require the hacker to guess your password correctly:

Attack TypeHow it worksStops a strong password?
Data breachA website you use gets hacked. Your email and password hash is stolen and cracked.No
PhishingA fake login page tricks you into typing your password directly.No
Credential stuffingHackers try your leaked password from one site on thousands of others.Only if unique
Keylogger / malwareMalicious software records every keystroke you type.No
Brute forceAutomated tools try millions of password combinations per second.Only if very long

The Hard Truth

Even if your password is perfectly strong and completely unique, a data breach at any site you use can expose it. The site stores a hash of your password — if their hashing is weak, attackers can crack it. Your password being strong does not protect you from the site being breached.

Section 02

What is Two-Factor Authentication?

Two-factor authentication (2FA) requires you to prove your identity in two different ways before granting access. The three categories of authentication factors are:

🧠

Something you know

A password, PIN, or security question. Easy to steal, phish, or guess.

Weakest factor alone
📱

Something you have

Your phone, a hardware key, or a one-time code from an app. Much harder to steal remotely.

Strong
👆

Something you are

Biometrics — fingerprint, face recognition, iris scan. Extremely hard to replicate.

Strongest

The Core Idea

2FA combines at least two of these three categories. A hacker who steals your password (something you know) still cannot log in because they don’t have your phone (something you have). Both factors must be compromised simultaneously — which is exponentially harder.

Section 03

How 2FA Works — Step by Step

1

Enter your username and password

The server verifies your password is correct — but does not grant access yet. You have passed Factor 1.

2

The server requests a second factor

You are prompted for a one-time code, push notification, or hardware key press. The login is suspended — waiting for you to prove you physically have the second factor.

3

Your device generates or receives the code

An authenticator app generates a 6-digit TOTP code that changes every 30 seconds. The code is mathematically tied to your account and the current time.

4

You enter the code — server verifies it

The server runs the same time-based algorithm with your secret key. If your code matches what it expects at this moment, access is granted. The code expires in 30 seconds and can never be reused.

5

Access granted

Both factors verified. Even if a hacker had your password, they could not complete step 3 without physical access to your device.

Section 04

Interactive: Try a 2FA Login

Experience a two-factor login flow yourself. Use the credentials shown, then enter the one-time code that appears on your simulated device.

  2FA Login Simulator
1
2
💡 Try entering wrong credentials to see what happens.

Section 05

The 5 Types of 2FA — Ranked by Security

💬

SMS / Text Message

A 6-digit code sent to your phone via text. Vulnerable to SIM swapping attacks where hackers convince your carrier to transfer your number.

Weak — avoid if possible
📧

Email OTP

A code sent to your email. Only as secure as your email account. If your email is compromised, this 2FA factor is also compromised.

Weak
📱

Authenticator App (TOTP)

Apps like Google Authenticator or Authy generate codes locally on your device. No network needed. Not interceptable in transit.

Strong — recommended
🔔

Push Notification

A prompt appears on your phone asking “Was this you?” You tap Approve or Deny. Vulnerable to MFA fatigue attacks (spamming approvals until you accidentally approve).

Good
🗝️

Hardware Security Key

A physical USB/NFC device (YubiKey, Google Titan). Cryptographically proven. Immune to phishing — only works on the exact legitimate site.

Strongest

SMS 2FA and SIM Swapping

In a SIM swap attack, a hacker calls your mobile carrier, impersonates you with basic personal info (often bought from data breaches), and asks them to transfer your number to a SIM they control. All your SMS codes now go to the hacker. Use an authenticator app instead of SMS wherever possible.

Section 06

How 2FA Stops Hackers — Attack Scenarios

Let’s walk through exactly what happens when a hacker tries to break into an account — with and without 2FA enabled.

🔓 Scenario A: Password-only account — No 2FA
Day 0 — Data breach at a shopping site
Your password is leaked in a database dump sold on the dark web for $0.001.
Day 3 — Credential stuffing
Automated bots try your email and password on 10,000 websites. Your Gmail matches.
Day 3 — Account takeover
Hacker is logged in. They change the recovery email and password. You are locked out.
Day 4 — Cascade attack
Everything connected to that email is now compromised. Bank accounts, social media, work accounts.
🛡️ Scenario B: Same attack — With 2FA enabled
Day 0 — Data breach at a shopping site
Your password is leaked in the same database dump.
Day 3 — Credential stuffing attempt
Bot tries your email and password on Gmail. Password is accepted — but the server requests a 2FA code.
Day 3 — Attack blocked
Hacker cannot proceed. They do not have your phone. The code on your authenticator app changes every 30 seconds. Login attempt fails.
Day 3 — You are notified
You receive an alert about a failed login attempt. You change your password. Account remains completely secure.

The Numbers

According to Google’s research, adding an authenticator app blocks 99.9% of automated bot attacks, 99% of bulk phishing attacks, and 76% of targeted attacks. It is the single highest-impact security action an individual can take.

Section 07

Interactive: Password Strength Tester

See how long it would take to crack your password — and understand why even a strong password is not enough without 2FA.

  Password Strength Analyser

Section 08

How TOTP Codes Are Generated — The Math

TOTP stands for Time-based One-Time Password (RFC 6238). When you scan a QR code to set up an authenticator app, you share a secret key with the server. Both your app and the server independently compute codes using this formula:

T = floor(current_unix_time / 30) ← 30-second window
TOTP = HMAC-SHA1(secret_key, T) ← cryptographic hash
code = last 6 digits of TOTP ← the number you type

Because both your app and the server know the same secret key and the current time, they arrive at the same 6-digit code independently — without ever transmitting the code over a network. There is nothing to intercept in transit.

Python — TOTP from scratch
import hmac, hashlib, time, struct, base64

def generate_totp(secret_b32: str, digits: int = 6) -> str:
    # 1. Decode the base32 secret (what the QR code contains)
    key = base64.b32decode(secret_b32.upper())

    # 2. Get the current 30-second time window
    T = int(time.time()) // 30
    msg = struct.pack('>Q', T)

    # 3. Compute HMAC-SHA1
    h = hmac.new(key, msg, hashlib.sha1).digest()

    # 4. Dynamic truncation
    offset = h[-1] & 0x0F
    code = struct.unpack('>I', h[offset:offset+4])[0] & 0x7FFFFFFF

    # 5. Get last N digits
    return str(code % (10 ** digits)).zfill(digits)

# Production use: pip install pyotp
import pyotp
totp = pyotp.TOTP("JBSWY3DPEHPK3PXP")
print(totp.now())              # current 6-digit code
print(totp.verify("482156"))   # True / False

Why TOTP is secure

The code is computed locally — never sent over a network until you type it. It expires every 30 seconds. It uses HMAC-SHA1 which cannot be reversed to find the secret key. Even if an attacker intercepted a code, it would be useless within 30 seconds.

Section 09

How to Set Up 2FA — Step by Step

Step 1: Install an authenticator app

AppPlatformCloud BackupBest for
Google AuthenticatoriOS, AndroidYesSimple, widely supported
AuthyiOS, Android, DesktopYes (encrypted)Multi-device, backup
Microsoft AuthenticatoriOS, AndroidYesMicrosoft/work accounts
1PasswordAll platformsYesPassword manager + 2FA combined
AegisAndroid onlyLocal onlyOpen source, privacy-focused

Step 2: Enable 2FA on your accounts

1

Go to your account’s security settings

Look for “Security”, “Privacy”, “Two-factor authentication”, or “Login verification”. Every major service has this: Google, Apple, Amazon, GitHub, Instagram, Facebook.

2

Choose “Authenticator App” — not SMS

Select the authenticator app option when given a choice. This is more secure than SMS and works without mobile signal.

3

Scan the QR code

Open your authenticator app, tap the “+” button, and scan the QR code shown on screen. This transfers the secret key to your app.

4

Enter the first code to confirm

The website will ask you to enter the 6-digit code shown in your app to confirm it worked correctly.

5

Save your backup codes

Critical: Download or write down the backup/recovery codes provided. If you lose your phone, these codes let you regain access. Store them safely — offline, in a secure location.

Priority Order — Enable 2FA on these first

1. Email account — everything resets through email
2. Banking and financial apps
3. Password manager
4. Work accounts (Google Workspace, Microsoft 365)
5. Social media
6. Cloud storage (Dropbox, Google Drive, iCloud)

Section 10

Common Questions and Misconceptions

QuestionAnswer
“What if I lose my phone?”Use the backup codes you saved at setup. Or restore your authenticator app from cloud backup. Always save backup codes offline.
“Can 2FA be hacked?”Advanced real-time phishing can intercept codes if you’re tricked into a fake site that forwards them immediately. Hardware keys are immune even to this. TOTP apps are resistant for most users.
“Isn’t it annoying?”Most services only ask for 2FA on new devices or after a period of inactivity. Trusted devices are remembered. The friction is a few seconds versus your account being taken over.
“Is SMS 2FA better than nothing?”Yes — significantly. SMS 2FA stops the vast majority of automated attacks. Only upgrade to an authenticator app if you are a higher-value target. SMS is still far better than no 2FA.
“Does 2FA make passwords irrelevant?”No. 2FA is a second layer — you still need a strong, unique password as the first layer. Both layers being strong is the goal.
“What is the difference between 2FA and MFA?”MFA (multi-factor authentication) is the general term for using more than one factor. 2FA uses exactly two factors. All 2FA is MFA, but MFA can use three or more factors.

Section 11

Knowledge Quiz

Test what you have learned about two-factor authentication.

  Two-Factor Authentication Quiz
Question 1 of 6

The post What is Two-Factor Authentication and Why Does It Matter? appeared first on Learn With Examples.

]]>
https://learnwithexamples.org/what-is-two-factor-authentication/feed/ 0 751
Normal Distribution — Why Everything Forms a Bell Curve https://learnwithexamples.org/normal-distribution-why-everything-forms-a-bell-curve/ https://learnwithexamples.org/normal-distribution-why-everything-forms-a-bell-curve/#respond Thu, 16 Jul 2026 06:32:14 +0000 https://learnwithexamples.org/?p=744 Normal Distribution Explained — Why Everything Forms a Bell Curve Statistics · Mathematics Heights, exam scores, measurement errors, IQ, shoe sizes, rainfall — almost every naturally occurring dataset organises itself…

The post Normal Distribution — Why Everything Forms a Bell Curve appeared first on Learn With Examples.

]]>
Normal Distribution Explained — Why Everything Forms a Bell Curve

Statistics · Mathematics

Heights, exam scores, measurement errors, IQ, shoe sizes, rainfall — almost every naturally occurring dataset organises itself into the same iconic shape. This guide explains why, what the bell curve tells you, and how to use it — with live demos, a z-score calculator, and Python code.

📖 18 min read 📊 Live bell curve builder 🪙 Coin-flip simulation 🧮 Z-score calculator ❓ Quiz included

Section 01

What is a Normal Distribution?

A normal distribution (also called a Gaussian distribution or bell curve) is a way that data arranges itself around a central value — with most values clustered near the middle, and fewer and fewer values appearing as you move toward the extremes.

Plot it on a graph and you get the unmistakable bell shape: perfectly symmetric, peaked in the centre, tapering off equally on both sides. The curve never actually touches the x-axis — it just gets infinitely close at the extremes.

📌 Definition

A normal distribution is fully described by just two numbers: the mean (μ) — where the centre of the bell sits — and the standard deviation (σ) — how wide or narrow the bell is. Change either one and you get a different bell curve.

⚖️

Perfectly Symmetric

The left half is a mirror image of the right. The mean, median, and mode are all equal and sit at the exact centre.

🏔️

Unimodal

There is only one peak — the most common value. Values become progressively rarer as you move away from the centre.

Asymptotic Tails

The curve approaches but never reaches zero. Extreme values are possible — just extraordinarily rare.

📐

Total Area = 1

The area under the entire curve equals exactly 1 (or 100%). This lets us read off probabilities directly from areas.

Section 02

Why Does Nature Produce Bell Curves?

This is the deep question. Why should human heights, leaf sizes, or measurement errors all form a bell? The answer comes from one of the most powerful theorems in all of mathematics: the Central Limit Theorem.

The Central Limit Theorem says: whenever a quantity is the result of many small, independent, random influences added together — the result will be normally distributed, no matter what distribution each individual influence has.

🌿 Why height is normally distributed

Your height is influenced by hundreds of genetic variants, each adding or subtracting a tiny amount. Some push you taller, some shorter, most roughly cancel out. The sum of hundreds of tiny random effects → bell curve. The same logic applies to birth weight, exam scores, and manufacturing tolerances.

The logic in three steps

1

Many independent influences

The outcome is the sum (or average) of many separate random inputs. Human height has ~700 genetic variants each contributing a tiny effect.

2

Most cancel out

For every influence that pushes the result high, there’s usually one pushing it low. The middle outcome (where they mostly balance) is most common.

3

Extremes are rare combinations

Getting an extreme value requires almost all the influences to push the same direction simultaneously — increasingly unlikely the more extreme you go.

Section 03

Interactive: Coin Flips Become a Bell Curve

The clearest way to see the Central Limit Theorem is with coin flips. Flip a coin 10 times and count heads. Do it thousands of times. The histogram of “number of heads” will form a perfect bell curve.

Click Flip & Toss to simulate thousands of experiments. Watch the histogram grow into a bell shape before your eyes.

  Coin Flip Simulation — Central Limit Theorem

💡 What you are seeing

Each bar shows how many times that number of heads appeared. With few trials the bars are jagged and uneven. As you increase trials to 10,000 — the bars smooth into a near-perfect bell. This is the Central Limit Theorem in action.

Section 04

Key Properties of the Normal Distribution

The normal distribution has a precise mathematical formula, but you don’t need to memorise it — you need to understand what its two parameters mean.

📐 The Formula (for reference)

f(x) = (1 / σ√2π) × e−½((x−μ)/σ)²
where μ = mean, σ = standard deviation, e = Euler’s number ≈ 2.718

ParameterSymbolControlsEffect on curve
Mean μ (mu) Centre / location Shifts the bell left or right without changing shape
Standard Deviation σ (sigma) Width / spread Larger σ → flatter, wider bell. Smaller σ → taller, narrower bell
Variance σ² Spread (squared) σ² = σ × σ. Often used in formulas; σ is easier to interpret

⚠️ Mean = Median = Mode

In a perfect normal distribution, all three measures of central tendency are identical and sit at the peak of the bell. This is only true for symmetric distributions — skewed data breaks this equality.

Section 05

Interactive Bell Curve Builder

Drag the sliders to change the mean (μ) and standard deviation (σ). Watch how the bell curve shifts position and changes shape in real time.

  Bell Curve Builder
μ = 50 σ = 10 μ − σ = 40 μ + σ = 60 Range 68%: 40–60

Section 06

The 68-95-99.7 Empirical Rule

One of the most useful facts about the normal distribution is that fixed percentages of data always fall within 1, 2, and 3 standard deviations of the mean — regardless of what μ and σ actually are.

±1σ
Within 1 standard deviation of the mean
68%
68.27%
±2σ
Within 2 standard deviations of the mean
95%
95.45%
±3σ
Within 3 standard deviations of the mean
99.7%
99.73%

Worked Example — Adult Male Heights

Adult male heights in the UK are approximately normally distributed with μ = 175 cm and σ = 7 cm. Using the empirical rule:

RangeHeights% of menInterpretation
μ ± 1σ 168 cm – 182 cm 68.27% About 2 in 3 men
μ ± 2σ 161 cm – 189 cm 95.45% About 19 in 20 men
μ ± 3σ 154 cm – 196 cm 99.73% Nearly all men — only 1 in 370 outside this
Above 196 cm >3σ above mean 0.135% Extremely rare (1 in 740 men)

🏭 Six Sigma in Manufacturing

The famous “Six Sigma” quality standard means keeping defects within ±6 standard deviations — which means only 3.4 defects per million opportunities. This is why the empirical rule matters enormously in engineering and quality control.

Section 07

Z-Scores — Standardising Any Normal Distribution

A z-score tells you how many standard deviations a value is away from the mean. It lets you compare values from completely different normal distributions on the same scale.

z = (x − μ) / σ
x = your value  |  μ = mean  |  σ = standard deviation

What z-scores mean

Z-ScoreMeaningPercentile (approx)
z = 0Exactly at the mean50th percentile
z = +11 std dev above mean~84th percentile
z = −11 std dev below mean~16th percentile
z = +22 std devs above mean~97.7th percentile
z = −22 std devs below mean~2.3rd percentile
z = +33 std devs above mean~99.9th percentile

💡 Why z-scores are useful

A student scored 75 in Maths (μ=60, σ=10) and 82 in English (μ=75, σ=8). Which was the better performance relative to classmates?

Maths z = (75−60)/10 = +1.5  |  English z = (82−75)/8 = +0.875

The Maths score was relatively better — despite being a lower raw mark. Z-scores make this comparison possible.

Section 08

Interactive Z-Score Calculator

Enter any value, mean, and standard deviation to instantly calculate the z-score and see what percentile it corresponds to.

  Z-Score Calculator
z = 0.00

Section 09

Real-World Examples

The normal distribution appears across almost every domain of science, engineering, and everyday life.

📏

Human Heights

Adult heights within a gender and population are normally distributed. UK men: μ=175cm, σ=7cm. The tallest 2.5% are above ~189cm.

🎓

Exam Scores

When a test is well-designed, scores form a bell curve. Many standardised tests (SAT, IQ tests) are deliberately calibrated to produce μ=100, σ=15.

🏭

Manufacturing

A machine making bolts produces diameters that scatter around the target in a bell. Quality control uses σ to decide how many are defective.

📈

Stock Returns

Daily returns on a diversified index approximate a normal distribution. Risk models use σ (volatility) to estimate the probability of large losses.

🌧️

Rainfall & Temperature

Monthly average temperatures at a location over many years form a bell. Climate scientists use deviations from the mean to measure unusual weather.

⚕️

Medical Measurements

Blood pressure, cholesterol, birth weight — most biological measurements are normally distributed. “Normal range” usually means within ±2σ of the mean.

Section 10

When Data is NOT Normally Distributed

Not everything is a bell curve. Recognising when data isn’t normal is just as important as knowing when it is.

DistributionExampleShape
Right-skewed Income, house prices, city populations Long tail to the right. Most people earn little; a few earn millions.
Left-skewed Age at retirement, scores on an easy test Long tail to the left. Most values are high, few are very low.
Bimodal Height of mixed male/female population Two peaks — mixing two separate normal distributions.
Uniform Rolling a die, random number generators Flat — every value equally likely, no central peak.
Exponential Time between customer arrivals, time to equipment failure Drops steeply from zero — short times are far more common.
Power law Social media followers, earthquake magnitudes Extremely heavy tail. A tiny number dominate.

⚠️ Always check before assuming normality

Many statistical tests assume your data is normal. Using them on skewed data gives misleading results. Always plot your data first (histogram or Q-Q plot) and run a normality test like Shapiro-Wilk before applying normal-distribution methods.

Section 11

Python Code

Plotting a bell curve

Python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

mu, sigma = 175, 7   # UK male heights: mean=175cm, std=7cm

x = np.linspace(mu - 4*sigma, mu + 4*sigma, 300)
y = norm.pdf(x, mu, sigma)

plt.figure(figsize=(10, 5))
plt.plot(x, y, color='#6366f1', linewidth=2.5)

# Shade the 1σ, 2σ, 3σ regions
for n, color, label in [(1,'#10b981','68%'),(2,'#f59e0b','95%'),(3,'#ef4444','99.7%')]:
    mask = (x >= mu - n*sigma) & (x <= mu + n*sigma)
    plt.fill_between(x, y, where=mask, alpha=0.2, color=color, label=f'{label} within {n}σ')

plt.title('Normal Distribution of UK Male Heights')
plt.xlabel('Height (cm)')
plt.ylabel('Probability Density')
plt.legend()
plt.tight_layout()
plt.show()

Z-scores and probabilities

Python
from scipy.stats import norm

mu, sigma = 175, 7
x = 185

# Z-score
z = (x - mu) / sigma
print(f"Z-score: {z:.2f}")           # → 1.43

# Probability of being BELOW this height
p_below = norm.cdf(x, mu, sigma)
print(f"P(height < 185): {p_below:.1%}")  # → 92.4%

# Probability of being ABOVE this height
p_above = 1 - p_below
print(f"P(height > 185): {p_above:.1%}")  # → 7.6%

# Probability of being BETWEEN two values
p_between = norm.cdf(182, mu, sigma) - norm.cdf(168, mu, sigma)
print(f"P(168 < height < 182): {p_between:.1%}")  # → 68.3%

# What height is at the 90th percentile?
p90 = norm.ppf(0.90, mu, sigma)
print(f"90th percentile height: {p90:.1f} cm")  # → 184.0 cm

Testing if data is normally distributed

Python
from scipy.stats import shapiro, normaltest
import numpy as np

data = np.random.normal(loc=175, scale=7, size=100)

# Shapiro-Wilk test (best for n < 5000)
stat, p = shapiro(data)
print(f"Shapiro-Wilk: stat={stat:.3f}, p={p:.3f}")
# If p > 0.05 → fail to reject normality (data is likely normal)

# D'Agostino K² test
stat2, p2 = normaltest(data)
print(f"Normaltest:   stat={stat2:.3f}, p={p2:.3f}")

# Quick visual check
import scipy.stats as stats
import matplotlib.pyplot as plt
stats.probplot(data, dist="norm", plot=plt)
plt.title("Q-Q Plot — points on the line = normal")
plt.show()

Section 12

Knowledge Quiz

Six questions to test your understanding of the normal distribution.

  Normal Distribution Quiz
Question 1 of 6

The post Normal Distribution — Why Everything Forms a Bell Curve appeared first on Learn With Examples.

]]>
https://learnwithexamples.org/normal-distribution-why-everything-forms-a-bell-curve/feed/ 0 744
How Binary Search Works — with Visual Examples https://learnwithexamples.org/how-binary-search-works-visual-examples/ https://learnwithexamples.org/how-binary-search-works-visual-examples/#respond Wed, 15 Jul 2026 16:10:39 +0000 https://learnwithexamples.org/?p=736 How Binary Search Works — with Visual Examples Algorithms · Beginner Binary search is one of the most elegant algorithms ever written. It finds any value in a sorted list…

The post How Binary Search Works — with Visual Examples appeared first on Learn With Examples.

]]>
How Binary Search Works — with Visual Examples

Algorithms · Beginner

Binary search is one of the most elegant algorithms ever written. It finds any value in a sorted list of a million items in just 20 steps. This guide shows you exactly how — with live visualizations, code in 3 languages, and a speed comparison you can feel.

📖 15 min read 🎮 3 interactive demos 💻 Python · JS · Java ❓ Quiz at the end

The Problem Binary Search Solves

Imagine you have a sorted list of 1,000,000 numbers and you need to find whether the number 742,891 is in it. The naive approach — checking every single element one by one — would take up to a million comparisons in the worst case.

Binary search solves this in at most 20 comparisons. Not 20,000. Not 2,000. Twenty. That’s the power of dividing the problem in half at every step.

Linear Search 1,000,000 steps
Binary Search 20 steps

📌 One Requirement

Binary search only works on a sorted array. This is the most important rule. We’ll revisit why at the end.

The Phone Book Intuition

You’ve already used binary search in real life. When you look up a name in a phone book (or a word in a dictionary), you don’t start from page 1 and flip forward.

You open to the middle. If the name you want comes alphabetically before the middle page, you throw away the right half and repeat on the left. If it comes after, you throw away the left half and repeat on the right. Each time, you eliminate half the remaining possibilities.

💡 Key Insight

Every comparison eliminates half of the remaining elements. That’s why 1,000,000 items only needs log₂(1,000,000) ≈ 20 steps. The algorithm grows with the logarithm of the input, not the input itself.

How It Works — Step by Step

Binary search uses three pointers on the array: low, mid, and high. Here’s the algorithm:

1

Set low = 0, high = last index

Start with the full array in scope. Low points to the first element, high points to the last.

2

Calculate mid = (low + high) / 2

Find the middle index. In Python/Java use integer division: (low + high) // 2 to avoid float issues.

3

Compare array[mid] with target

Three possible outcomes: equal (found it! return mid), target is smaller (search left half: high = mid − 1), target is larger (search right half: low = mid + 1).

4

Repeat until found or low > high

If low exceeds high, the target is not in the array — return −1 (or null/None depending on your language).

⚠️ Integer Overflow Tip

In languages like Java/C++, computing (low + high) / 2 can overflow for very large arrays. The safe formula is: low + (high - low) / 2.

Live Visualizer — Try It Yourself

Enter any number between 1 and 99 and watch binary search find it step by step. The yellow cell is the current midpoint being checked, grey cells are eliminated, and green means found.

  Binary Search Visualizer
Press ▶ Search to start, or Step → to go one step at a time.

Code in Python, JavaScript & Java

Python

Python
def binary_search(arr, target):
    low, high = 0, len(arr) - 1

    while low <= high:
        mid = low + (high - low) // 2  # safe from integer overflow

        if arr[mid] == target:
            return mid              # found! return the index
        elif arr[mid] < target:
            low = mid + 1           # target is in right half
        else:
            high = mid - 1          # target is in left half

    return -1                       # not found


# Example usage
numbers = [2, 7, 13, 19, 25, 34, 37, 46, 58, 72]
result = binary_search(numbers, 37)
print(f"Found at index: {result}")  # → Found at index: 6
print(binary_search(numbers, 99))   # → -1 (not found)

JavaScript

JavaScript
function binarySearch(arr, target) {
  let low = 0;
  let high = arr.length - 1;

  while (low <= high) {
    const mid = low + Math.floor((high - low) / 2);

    if (arr[mid] === target)  return mid;      // found
    if (arr[mid] < target)   low = mid + 1;   // search right
    else                       high = mid - 1;  // search left
  }

  return -1; // not found
}

// Example
const nums = [2, 7, 13, 19, 25, 34, 37, 46, 58, 72];
console.log(binarySearch(nums, 37));  // 6
console.log(binarySearch(nums, 99));  // -1

Java

Java
public class BinarySearch {

    public static int binarySearch(int[] arr, int target) {
        int low = 0;
        int high = arr.length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2; // avoids overflow

            if (arr[mid] == target)  return mid;
            if (arr[mid] <  target)  low  = mid + 1;
            else                       high = mid - 1;
        }

        return -1; // not found
    }

    public static void main(String[] args) {
        int[] nums = {2, 7, 13, 19, 25, 34, 37, 46, 58, 72};
        System.out.println(binarySearch(nums, 37)); // 6
        System.out.println(binarySearch(nums, 99)); // -1
    }
}

Time & Space Complexity

Binary search is one of the most efficient search algorithms. Its performance is measured using Big O notation.

Case Time Complexity What it means
Best Case O(1) Target is at the midpoint on the first check
Average Case O(log n) Halves the search space each iteration
Worst Case O(log n) Target not found after exhausting all halves
Space (iterative) O(1) Only stores low, mid, high — no extra memory
Space (recursive) O(log n) Call stack grows with each recursive call

📐 The log n explained

log₂(n) is the number of times you can halve n before reaching 1. For n = 1,024 → log₂(1024) = 10 steps. For n = 1,048,576 (1 million) → just 20 steps. Doubling the input only adds one more step.

Binary vs Linear Search — The Speed Race

Drag the slider to set the array size and watch how many steps each algorithm needs. The difference becomes dramatic very quickly.

  Speed Comparison Demo
1,000
Linear
500 steps
Binary
10 steps

* Linear shows average-case (n/2). Binary shows worst-case (log₂n). Both searching the same array.

The One Rule You Must Not Break

Binary search requires a sorted array. This is non-negotiable. Here’s why: when the algorithm looks at the midpoint and finds the target is smaller, it assumes everything to the right is also larger. In an unsorted array, that assumption is false — and the algorithm silently gives wrong answers.

Python — What goes wrong with unsorted input
# ❌ WRONG — unsorted array
unsorted = [37, 2, 72, 13, 25]
binary_search(unsorted, 2)   # returns -1 even though 2 is there!

# ✅ CORRECT — sort first
sorted_arr = sorted(unsorted)  # [2, 13, 25, 37, 72]
binary_search(sorted_arr, 2)  # returns 0 ✓

# Tip: Python has bisect module built-in for production use
import bisect
idx = bisect.bisect_left(sorted_arr, 2)
print(sorted_arr[idx] == 2)    # True

⛔ Common Mistake

Sorting takes O(n log n) time. If you’re only searching once, sorting + binary search is slower than linear search. Binary search pays off when you search the same sorted data many times — the sort cost is paid once, searches are O(log n) forever.

Where Binary Search Is Used in Real Life

Binary search isn’t just a textbook exercise. It runs inside software you use every day.

🗄️

Database Indexes

B-trees (the data structure behind MySQL, PostgreSQL indexes) use a generalized form of binary search to find rows in milliseconds across millions of records.

📦

Package Managers

npm, pip, and apt use binary search on sorted version lists to find compatible package versions quickly.

🎮

Game Development

Finding which tile a player is on, collision detection bounds, and sorted leaderboard lookups all use binary search variants.

🔤

Spell Checkers

Dictionaries are sorted. When you type a word, spell checkers run binary search on the dictionary to verify it in O(log n) time.

📡

Git Bisect

Git’s git bisect command uses binary search through your commit history to find which commit introduced a bug.

🌐

IP Routing

Routers use binary search on sorted prefix tables to find the right network path for packets — billions of times per second.

Knowledge Quiz

Five questions to lock in what you’ve learned.

  Binary Search Quiz
Question 1 of 5

The post How Binary Search Works — with Visual Examples appeared first on Learn With Examples.

]]>
https://learnwithexamples.org/how-binary-search-works-visual-examples/feed/ 0 736