Skip to main content

Unit 3

30–35% of exam

Algorithms and Programming

This is the biggest part of the exam: reading, tracing and writing code. You'll work with variables, expressions, strings, conditionals, loops, lists and procedures in the exam's own pseudocode (in text and block form), then look at searching, random values, simulations, efficiency and the limits of what algorithms can do.

Study this unit

Flashcards (40)Practice questions (76)Computer Science Principles must-know sheet

Free-response questions on this unit

Write your own answer, then score it with the rubric or with AI.

Big ideas

  • Every algorithm is built from sequencing, selection and iteration
  • In the exam's pseudocode, ← means assignment and list indexes start at 1
  • Lists and procedures are abstractions that keep programs manageable
  • Binary search is usually much faster than linear search, but needs sorted data
  • Some problems take unreasonable time, and some can't be solved by any algorithm

Full unit reviews

Longer videos that cover the whole unit. Good for a first pass or a final review.

  • AP Computer Science Principles Big Idea 3: Algorithms and Programming

    Dr. D's TutorTimeWatch on YouTube (opens in a new tab)

  • AP CS Principles: Algorithms and Programming Review

    Mr Comp SciWatch on YouTube (opens in a new tab)

  • AP CS Principles Exam Review - Code

    Flavio KupermanWatch on YouTube (opens in a new tab)

  • [CompSci] Algorithms and Programming | Big Ideas in Computer Science Principles

    Eamon MarchantWatch on YouTube (opens in a new tab)

A variable is a named place that holds a value, like a number, a Boolean, a string or a list. Assignment (a ← expression) works out the expression and stores the result in the variable, replacing whatever was there, so a variable always holds the last value assigned to it.

Key terms

  • variable
  • assignment operator (←)
  • value
  • data type
  • meaningful variable name
  • AP CSP Topic 3.1 - Variables and Assignments - Explanations and 5 MCQs!

    Dr_WuWatch on YouTube (opens in a new tab)

  • AP CS Principles Exam Review - Variables

    Flavio KupermanWatch on YouTube (opens in a new tab)

  • AP CSP Reference Sheet - Assignment, Display, and Input

    Chris OzarkaWatch on YouTube (opens in a new tab)

  • Variables and assignment | Intro to CS - Python | Khan Academy

    Khan AcademyWatch on YouTube (opens in a new tab)

  • Quick Bit: Variables

    UTeach Computer ScienceWatch on YouTube (opens in a new tab)

Read the review notes: 3.1 Variables and Assignments

A few quick questions on this topic, with the answers explained.

A list holds many values in order, and each value (an element) has a position number called its index; on the exam, indexes start at 1, so aList[1] is the first element. Using one list instead of many separate variables is data abstraction: you can work with the whole collection by name, which makes a program simpler to write and change.

Key terms

  • list
  • element
  • index
  • data abstraction
  • string
  • managing complexity
Read the review notes: 3.2 Data Abstraction

A few quick questions on this topic, with the answers explained.

An algorithm is a finite list of steps that does a job, and sequencing means running those steps in the order they're written. Expressions use +, -, *, / and MOD with the usual order of operations (MOD ranks with * and /). On the exam / is ordinary division, so 7 / 2 is 3.5, and a MOD b is what's left over after dividing a by b, so 20 MOD 6 is 2.

Key terms

  • algorithm
  • sequencing
  • expression
  • arithmetic operators
  • MOD (remainder)
  • order of operations
  • AP CSP Topic 3.3 - Mathematical Expressions! - Explanations and 5 MCQs!

    Dr_WuWatch on YouTube (opens in a new tab)

  • AP CSP Reference Sheet - Arithmetic Operators and Numeric Procedures

    Chris OzarkaWatch on YouTube (opens in a new tab)

  • Tracing arithmetic expressions | Intro to CS - Python | Khan Academy

    Khan AcademyWatch on YouTube (opens in a new tab)

  • Modulus Operator - CS101 - Udacity

    UdacityWatch on YouTube (opens in a new tab)

  • AP CSP Topic 3.3 - Mathematical Expressions - Speedrun! 20 MCQs

    Dr_WuWatch on YouTube (opens in a new tab)

Read the review notes: 3.3 Mathematical Expressions

A few quick questions on this topic, with the answers explained.

A string is text: a sequence of characters in a set order, like "hello" or "A7!". Concatenation sticks strings together into a new, longer string ("sun" and "flower" give "sunflower"), and a substring is a piece of a string, like "flow" inside "sunflower". On the exam, any string procedure you need is explained in the question.

Key terms

  • string
  • character
  • concatenation
  • substring
Read the review notes: 3.4 Strings

A few quick questions on this topic, with the answers explained.

A Boolean value can only be true or false. Relational operators (=, ≠, >, <, ≥, ≤) compare two values, and the logical operators combine conditions: NOT flips a value, a AND b is true only when both are true, and a OR b is true when at least one is true.

Key terms

  • Boolean value
  • relational operator
  • NOT
  • AND
  • OR
  • AP CSP Topic 3.5 - Boolean Expressions - Explanations and 5 MCQs!

    Dr_WuWatch on YouTube (opens in a new tab)

  • AP CS Principles Exam Review - Booleans and Conditionals

    Flavio KupermanWatch on YouTube (opens in a new tab)

  • AP CSP Reference Sheet - Relational and Boolean Operators

    Chris OzarkaWatch on YouTube (opens in a new tab)

  • CS Principles: Conditionals - Part 3 "And & Or" Operators

    CodeAIWatch on YouTube (opens in a new tab)

  • Evaluating compound boolean expressions | Intro to CS - Python | Khan Academy

    Khan AcademyWatch on YouTube (opens in a new tab)

Read the review notes: 3.5 Boolean Expressions

A few quick questions on this topic, with the answers explained.

Selection lets an algorithm choose which steps to run based on whether a condition is true. An IF (condition) block runs only when the condition is true, and IF ... ELSE runs exactly one of its two blocks.

Key terms

  • selection
  • conditional statement
  • condition
  • IF
  • ELSE
  • AP CSP Topic 3.6 - Conditionals - Explanations and 5 MCQs!

    Dr_WuWatch on YouTube (opens in a new tab)

  • AP CSP Reference Sheet - Selection

    Chris OzarkaWatch on YouTube (opens in a new tab)

  • AP CS Principles Exam Review - Booleans and Conditionals

    Flavio KupermanWatch on YouTube (opens in a new tab)

  • CS Principles: Conditionals - Part 2 If/Else Statements

    CodeAIWatch on YouTube (opens in a new tab)

  • if statements | Intro to CS - Python | Khan Academy

    Khan AcademyWatch on YouTube (opens in a new tab)

  • Quick Bit: Selection Statements

    UTeach Computer ScienceWatch on YouTube (opens in a new tab)

Read the review notes: 3.6 Conditionals

A few quick questions on this topic, with the answers explained.

A nested conditional is a conditional inside another conditional, so the inner check only happens when the outer condition sends the program that way. Tracing one branch at a time, with a specific input, is how you work out which output you'll get.

Key terms

  • nested conditional
  • branch
  • condition
  • tracing
  • AP CSP Topic 3.7 - Nested Conditionals - Explanations and 5 MCQs!

    Dr_WuWatch on YouTube (opens in a new tab)

  • Nested conditionals | Intro to CS - Python | Khan Academy

    Khan AcademyWatch on YouTube (opens in a new tab)

  • Python Nested Conditionals - AP Computer Science Principles

    Chris OzarkaWatch on YouTube (opens in a new tab)

  • AP CSP Units 4 and 5 REVIEW Lesson 6 Nested Conditionals and Loops

    Janelle WhalenWatch on YouTube (opens in a new tab)

  • AP CSP Topic 3.7 - Nested Conditionals - Speedrun! 29 MCQs

    Dr_WuWatch on YouTube (opens in a new tab)

Read the review notes: 3.7 Nested Conditionals

A few quick questions on this topic, with the answers explained.

Iteration repeats part of an algorithm. REPEAT n TIMES runs its block exactly n times, while REPEAT UNTIL (condition) checks the condition before each pass and stops once it's true, so the body never runs if the condition starts out true, and the loop never ends if the condition can never become true.

Key terms

  • iteration
  • loop
  • REPEAT n TIMES
  • REPEAT UNTIL
  • infinite loop
Read the review notes: 3.8 Iteration

A few quick questions on this topic, with the answers explained.

Different algorithms can solve the same problem, and algorithms that look different can still give the same result. You can build new algorithms from ones you already know work, like finding a maximum or minimum, adding up a sum or average, or checking whether a number divides evenly using MOD.

Key terms

  • algorithm
  • equivalent algorithms
  • existing algorithms as building blocks
  • maximum and minimum
  • sum and average
Read the review notes: 3.9 Developing Algorithms

A few quick questions on this topic, with the answers explained.

3.10

Lists

The exam's list procedures are INSERT, APPEND, REMOVE and LENGTH, and an index below 1 or above LENGTH(aList) stops the program with an error. Traversing a list means visiting its elements one at a time (often with FOR EACH item IN aList), and a linear search checks each element in order until it finds the value or runs out of elements.

Key terms

  • traversal
  • FOR EACH
  • APPEND, INSERT, REMOVE
  • LENGTH
  • linear (sequential) search
Read the review notes: 3.10 Lists

A few quick questions on this topic, with the answers explained.

Binary search looks for a value in a sorted list by checking the middle element and throwing away the half that can't contain it, repeating until it finds the value or nothing is left. It only works on sorted data, but on large lists it's usually far faster than a linear search.

Key terms

  • binary search
  • sorted data
  • linear search
  • efficiency
  • AP CS Principles Exam Review - Binary Search

    Flavio KupermanWatch on YouTube (opens in a new tab)

  • AP CSP Topic 3.11 - Binary and Linear search - Code.org Unit 10.2 walkthrough - 7 practice MCQs!

    Dr_WuWatch on YouTube (opens in a new tab)

  • AP CSP Exam Review Binary Search

    Computer Science CoachWatch on YouTube (opens in a new tab)

  • How Binary Search Makes Computers Much, Much Faster

    Tom ScottWatch on YouTube (opens in a new tab)

  • CS50 2019 - Lecture 0 - Binary Search

    CS50Watch on YouTube (opens in a new tab)

  • AP CSP Topic 3.11 - Binary search - 40 practice MCQs, SPEEDRUN !

    Dr_WuWatch on YouTube (opens in a new tab)

Read the review notes: 3.11 Binary Search

A few quick questions on this topic, with the answers explained.

A procedure is a chunk of code with a name. It can take inputs called parameters and can send back a value with RETURN, and you call it by name with arguments (the actual values for the parameters), like total ← addUp(3, 5). A call pauses the normal order of the program, runs the procedure's steps, and then picks up right after the call, using any value that was returned.

Key terms

  • procedure
  • parameter
  • argument
  • procedure call
  • return value
Read the review notes: 3.12 Calling Procedures

A few quick questions on this topic, with the answers explained.

Writing your own procedures is procedural abstraction: you can call a procedure as long as you know what it does, even if you never look at the code inside it, and one procedure with parameters can replace many repeated chunks of code. Breaking a big problem into smaller procedures (modularity) makes a program easier to read, test, fix and reuse.

Key terms

  • procedural abstraction
  • modularity
  • parameter
  • RETURN
  • reusing code
Read the review notes: 3.13 Developing Procedures

A few quick questions on this topic, with the answers explained.

A software library is a collection of procedures someone else has already written that you can use in your own programs. Its API (application program interface) spells out what each procedure does and how to call it, and reading the documentation is how you learn to use it. Libraries make complex programs much quicker to build.

Key terms

  • software library
  • API (application program interface)
  • documentation
  • reusing code
Read the review notes: 3.14 Libraries

A few quick questions on this topic, with the answers explained.

RANDOM(a, b) returns a random whole number from a to b, including both a and b, and each value is equally likely. Because each run of the program can give a different result, you reason about every possible outcome rather than a single one.

Key terms

  • RANDOM(a, b)
  • inclusive range
  • random number generation
  • possible outcomes
Read the review notes: 3.15 Random Values

A few quick questions on this topic, with the answers explained.

A simulation is a program that models a real-world system or event, often using random values to copy real-world variety, so you can test ideas that would be too slow, costly or dangerous to try for real. Simulations leave out details on purpose, which makes them simpler but means their choices about what to include can add bias.

Key terms

  • simulation
  • model
  • abstraction
  • random values
  • bias in simulations
Read the review notes: 3.16 Simulations

A few quick questions on this topic, with the answers explained.

Efficiency is how much time or memory an algorithm needs as its input grows. Algorithms whose number of steps grows no faster than a polynomial (like n, n² or n³) run in a reasonable amount of time, while ones that grow exponentially or faster (like 2ⁿ or n!) take an unreasonable amount of time. When finding the best answer would take too long, a heuristic finds an answer that's good enough.

Key terms

  • efficiency
  • reasonable time
  • unreasonable time
  • heuristic
  • optimization problem
  • AP CSP Exam Review | Algorithmic Efficiency

    Computer Science CoachWatch on YouTube (opens in a new tab)

  • AP CSP Topic 3.17 - Reasonable/unreasonable - Code.org Unit 10.3 walkthrough - PLUS 6 practice MCQs!

    Dr_WuWatch on YouTube (opens in a new tab)

  • Quick Bit: Heuristics

    UTeach Computer ScienceWatch on YouTube (opens in a new tab)

  • U6 L3 Unreasonable Time

    Janelle WhalenWatch on YouTube (opens in a new tab)

  • AP CSP Topic 3.17 - Algorithmic Efficiency - Speedrun! 25 MCQs!

    Dr_WuWatch on YouTube (opens in a new tab)

  • AP CS Principles Exam Review - Algorithmic Efficiency and Undecidable Problems

    Flavio KupermanWatch on YouTube (opens in a new tab)

Read the review notes: 3.17 Algorithmic Efficiency

A few quick questions on this topic, with the answers explained.

A decision problem has a yes-or-no answer, and it's decidable if some algorithm gives the right answer for every possible input (like "Is this list sorted?"). An undecidable problem is one where no algorithm can always give the right answer, though some individual cases may still be solvable.

Key terms

  • decision problem
  • decidable problem
  • undecidable problem
  • instance of a problem
  • AP CSP Topic 3.17 and 3.18 - Undecidable and heuristics. Code.org Unit 10.4 walkthrough. 6 MCQs!

    Dr_WuWatch on YouTube (opens in a new tab)

  • Are There Problems That Computers Can't Solve?

    Tom ScottWatch on YouTube (opens in a new tab)

  • Alan Turing: Crash Course Computer Science #15

    CrashCourseWatch on YouTube (opens in a new tab)

  • Proof That Computers Can't Do Everything (The Halting Problem)

    udiprodWatch on YouTube (opens in a new tab)

  • AP CSP Topic 3.18 - Decidable/Undecidable problems - 15 practice MCQs, SPEEDRUN !

    Dr_WuWatch on YouTube (opens in a new tab)

Read the review notes: 3.18 Undecidable Problems

A few quick questions on this topic, with the answers explained.