AP CSP Topic 3.17 Coding Practice - Algorithmic Efficiency
Big Idea 3: Algorithms and Programming · Topic 3.17 · Coding Practice
Algorithmic Efficiency - Count It Yourself
Count the operations an algorithm actually performs, then watch what happens to that count as the input grows.
You compared growth by eye in Big-O Race. Now count the operations for real. Every problem here is the same question asked louder: how many steps, and what happens to that number when the input gets bigger? Predict the count before you run, then let the computer settle it. The last one is the difference between an algorithm you can ship and one nobody will ever wait for.
Problem 1 of 4
A linear search checks list items one at a time from the front until it finds the target. The list is 4, 8, 15, 16, 23, 42, 7, 9 and the target is 9, which sits at the very end. Count how many items the search has to look at, and print that count.
Expected output: 8
Show AP pseudocode reference
data ← [4, 8, 15, 16, 23, 42, 7, 9]
target ← 9
count ← 0
FOR EACH item IN data
{
count ← count + 1
IF(item = target)
{
DISPLAY(count)
}
}
The target is the last item, which is the worst case for a linear search: it has to look at every single element before it finds one. That is why linear search is described as taking n steps on a list of n items.
Problem 2 of 4
Now a nested loop. For a list of 6 items, an algorithm compares every item against every item, including itself. Count how many comparisons that is in total, and print the count.
Expected output: 36
Show AP pseudocode reference
n ← 6
count ← 0
FOR EACH i IN [1..n]
{
FOR EACH j IN [1..n]
{
count ← count + 1
}
}
DISPLAY(count)
The inner loop runs a full 6 times for each single pass of the outer loop, so the counts multiply rather than add. Six outer passes times six inner passes is the answer, and that multiplying is what makes nested loops quadratic.
Problem 3 of 4
Compare the two directly. For n = 10 and then n = 100, print the number of steps a single loop takes and the number a nested loop takes. Print one line per size, in exactly this format: n=10 linear=10 quadratic=100
FOR EACH n IN [10, 100]
{
linear ← n
quadratic ← n * n
DISPLAY("n=" + n + " linear=" + linear + " quadratic=" + quadratic)
}
You do not need to actually run the loops to count their steps: a single loop over n items takes n steps and a nested pair takes n times n. Look at what happens between the two lines. The input got ten times bigger and the linear count did too, but the quadratic count got a hundred times bigger.
Problem 4 of 4
Write from scratch. Some algorithms have to try every possible subset of their input, which takes 2 to the power of n steps. For n = 20, print the step count for a quadratic algorithm and for a subset algorithm, then print which of the two is unreasonable. Use exactly this format: quadratic 400 subsets 1048576 subsets is unreasonable
Expected output: quadratic 400 subsets 1048576 subsets is unreasonable
Show AP pseudocode reference
n ← 20
quadratic ← n * n
subsets ← 2 ^ n
DISPLAY("quadratic " + quadratic)
DISPLAY("subsets " + subsets)
DISPLAY("subsets is unreasonable")
In Python 2 to the power of n is 2 ** n; in JavaScript it is 2 ** n as well. The gap is the whole point: at n = 20 the quadratic algorithm does 400 steps and the subset one does over a million. Push n to 60 and the subset algorithm outlasts a human lifetime, which is what "unreasonable time" means.
Your code runs on a secure external service. Answers are checked automatically and nothing is stored.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed.
Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Typically responds within 24 hours
✓
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
34.8% of Tanner’s CSP students score 5s. The national average is 9.6%.
I’m a Student
I’m a Teacher
✓Free AP CSP Big Ideas cheat sheet (PDF)
✓Daily practice questions covering all 5 Big Ideas
✓Create Task tips that actually work — from a real AP teacher
✓Free class codes with student progress tracking
✓3 full practice exams + Top 100 questions for your class
✓Create Task guidance and pseudocode reference sheets
Which AP CS exams are you prepping for?
✓
You’re in!
Your Big Ideas cheat sheet is on its way.
No thanks, I’ll figure it out myself
Avg student improvement: 2+ score levels | Real AP teacher, not just a tutor
AP Cybersecurity — National Launch 2026–27
Get Early Access to AP Cyber
AP Cyber launches nationally fall 2026. Get in early to help shape the course — start free with Unit 1 and the free teacher gradebook.
✓
You’re in — you’re on the AP Cyber early-access list!
Tanner will follow up personally within 48 hours. Your feedback will directly shape what gets built.
Step 1 of 4
Early Access — Limited Spots
Who are you?
Are you a teacher or a student?
I’m a Teacher
I’m a Student
Free to start — Unit 1 and the teacher gradebook are always free, no credit card.
Founding teachers unlock all 5 units and get direct input on what we build.
Not interested right now
Step 2 of 4
Your School
Tell us about your class
Other AP CS courses you teach
Your Situation
Tell us about yourself
Step 3 of 4
Classroom Needs
What does your classroom need? (select all that apply)
How You Study
What would help you most? (select all that apply)
Step 4 of 4
Almost Done
Where should we send your early-access details?
Free gradebook + Unit 1 | Your feedback shapes the course | Built by a real AP teacher