A step-by-step set of instructions for solving a problem or accomplishing a specific task. An algorithm must be precise (every step is unambiguous), finite (it terminates after a bounded number of steps), and effective (each step can actually be carried out).
A recipe for solving problemsβfollow the steps, get the answer.
Showing a random 20 of 50 problems.
Example 1
medium
How many comparisons does linear search make to find 8 in [3,8,1,5] (searching left to right)?Linear search for 8 in [3, 8, 1, 5]
Example 2
medium
An algorithm counts how many items in [5,2,8,1,7,4] are greater than 4. What does it output?
Example 3
challenge
Design check: an algorithm to find the median of a list. Which step ordering is correct: (A) sort then pick middle, (B) pick middle then sort?
Example 4
medium
An algorithm has steps that depend on a user choosing 'whichever feels right.' What property of an algorithm does this violate?
Example 5
medium
Trace: r=1; for i in 1..4: r=rΓi. Final r?
Example 6
easy
An algorithm receives an empty list and must output its average. What edge case must it handle?
Example 7
medium
Trace this max-finder on [2,8,3,8,1]: keep a running best. What is the final best?
Example 8
easy
Trace: s=0; for each number in [6,4,5], do s=s+number. Final s?
Example 9
easy
Does an algorithm need to give the correct answer for ALL valid inputs, or just one example input?
Example 10
medium
Trace: x=3; while x<20: x=xΓ2. Final x?
Example 11
easy
Which property is REQUIRED of every algorithm: (a) it uses a loop, (b) it terminates after a bounded number of steps, (c) it is written in Python?
Example 12
medium
Trace: n=6, sum=0. While n>0: sum=sum+n; n=nβ1. Final sum?
Example 13
hard
An algorithm tests whether n=21 is prime by trying divisors from 2 to nβ1. At which divisor does it first find a factor?
Example 14
hard
Two algorithms compute 1+2+β―+n: A uses a loop with n additions; B uses 2n(n+1)β. For n=100, how many additions does each do (count only the additions inside the formula)?
Example 15
challenge
An algorithm doubles a value each step starting at 1 and stops when it exceeds 100. How many steps run, and what is the final value?
Example 16
easy
Fill in the blank: An algorithm must be finite, meaning it ____ after a bounded number of steps.
Example 17
medium
Bubble sort on [3,1,2]: after the FIRST full pass (compare/swap adjacent left to right), what is the list?
Example 18
medium
Trace: a=7, b=3. Do a=a+b; b=aβb; a=aβb. Final a, b?
Example 19
easy
Is this a valid algorithm step? 'Pick whichever number looks nicest.' Answer yes or no.
Example 20
hard
Binary search needs at most how many comparisons on a sorted list of length 16?