Practice Algorithm in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick Recap

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][3,8,1,5] (searching left to right)?

Example 2

medium
An algorithm counts how many items in [5,2,8,1,7,4][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=1r=1; for ii in 1..41..4: r=rΓ—ir = r \times i. Final rr?

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][2,8,3,8,1]: keep a running best. What is the final best?

Example 8

easy
Trace: s=0s=0; for each number in [6,4,5][6,4,5], do s=s+numbers = s + \text{number}. Final ss?

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=3x=3; while x<20x < 20: x=xΓ—2x = x \times 2. Final xx?

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=6n=6, sum=0sum=0. While n>0n>0: sum=sum+nsum = sum + n; n=nβˆ’1n = n - 1. Final sumsum?

Example 13

hard
An algorithm tests whether n=21n=21 is prime by trying divisors from 2 to nβˆ’1n-1. At which divisor does it first find a factor?

Example 14

hard
Two algorithms compute 1+2+β‹―+n1+2+\dots+n: A uses a loop with nn additions; B uses n(n+1)2\tfrac{n(n+1)}{2}. For n=100n=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][3,1,2]: after the FIRST full pass (compare/swap adjacent left to right), what is the list?

Example 18

medium
Trace: a=7a=7, b=3b=3. Do a=a+ba = a + b; b=aβˆ’bb = a - b; a=aβˆ’ba = a - b. Final aa, bb?

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?