Topic 2.2: Data Compression | AP CSP Big Idea 2 | APCSExamPrep.com
Data Compression
After this lesson, you will be able to:
- Explain why data is compressed and what redundancy has to do with it
- Compare lossless and lossy compression and state exactly what each guarantees
- Work a run-length encoding example by hand and show why it is lossless
- Reason about the size-versus-quality trade-off and pick the right method for a use case
- Explain why fewer bits does not automatically mean less information
- Describe how data-format and compression choices appear in your Create Task written response
A single minute of raw, uncompressed audio is over 10 megabytes, yet a three-minute song streams to your phone in seconds. A photo straight off a camera sensor is enormous, yet it fits in a text message. Nothing about the original recording changed; the file just got dramatically smaller on the way to you. Compression is the reason almost everything on the internet is fast enough to use, and the key question this lesson answers is: when is it safe to make a file smaller, and when does making it smaller quietly throw away something you needed?
Why We Compress Data
Data compression reduces the number of bits used to store or transmit a piece of data. Smaller files take less storage space and travel faster across a network, which is why compression sits underneath streaming, messaging, backups, and nearly every download you make. A compression method looks for redundancy in the data, which is repeated or predictable patterns, and finds a shorter way to represent the same content.
How much a file shrinks is not fixed. The amount of size reduction depends on both how much redundancy the original contains and which compression method is used. A page of text with long repeated stretches compresses a great deal; data that is already varied or close to random barely compresses at all, no matter how clever the algorithm.
Fewer bits does not automatically mean less information. Removing redundancy shortens how the data is written down without changing what it says, so a smaller file can still carry all of the original information. Only certain methods actually discard information. Keep "fewer bits" and "less information" as two separate ideas.
That last point splits every compression method into two families. Some methods promise the original can be rebuilt exactly, and some trade that promise away for a much smaller file. Those two families are lossless and lossy, and knowing which one a scenario needs is the whole skill.
Lossless Compression
Lossless compression reduces the number of bits while guaranteeing that the original data can be reconstructed exactly. Nothing is lost. You compress, and later you decompress, and what comes back is bit-for-bit identical to what you started with. Common lossless formats include ZIP for general files, PNG for images, and run-length encoding as a simple teaching example. Lossless is the only acceptable choice whenever the exact original must survive, which is why it is used for text, source code, spreadsheets, and archives.
A worked example: run-length encoding
Run-length encoding (RLE) is the clearest way to see lossless compression happen. It scans the data left to right and replaces each run of identical symbols with a count followed by the symbol. Watch it compress an 8-character string:
The five A characters become 5A and the three B characters become 3B, so AAAAABBB shrinks to 5A3B: from 8 characters down to 4, a saving of 4. Crucially, the counts and symbols carry everything needed to rebuild the string, so decoding returns AAAAABBB perfectly. That exact-recovery guarantee is what makes RLE lossless.
RLE only helps when there are long runs. Encode ABABAB and you get 1A1B1A1B1A1B, which is longer than the original because there is no redundancy to remove. This is the general rule in miniature: compression pays off in proportion to how much repetition the data actually contains.
Lossy Compression
Lossy compression can reduce the number of bits far more than lossless, but it does so by permanently discarding some of the data. Once compressed, the exact original cannot be reconstructed; decompressing gives back an approximation, not a perfect copy. The classic lossy formats are JPEG for photos and MP3 for audio. They work by throwing away detail a human is unlikely to notice, such as subtle color shifts or sounds masked by louder ones, so the file gets much smaller while still looking or sounding acceptable.
Lossy is the right tool when transmission and storage efficiency matter more than a perfect reproduction, and when a person perceiving the media will tolerate the loss. That describes most photos, music, and video on the web. It is the wrong tool the instant an exact copy is required, because the discarded data is gone for good and no amount of decompressing brings it back.
Lossy loss is permanent. Saving a JPEG at high compression and later "saving it again at full quality" does not restore the detail; that detail no longer exists in the file. Re-compressing a lossy file repeatedly only degrades it further. Treat lossy as a door that opens one way only.
The Size-versus-Quality Trade-off
Choosing a compression method is a trade-off between size and fidelity (how faithful the copy is to the original). Lossless keeps perfect fidelity but reduces size less. Lossy sacrifices some fidelity to reduce size much more. Neither is "better" in the abstract; the right choice depends entirely on what the data is for.
A reliable way to decide is to ask one question: must the reconstructed data be exactly the original?
- If yes, use lossless. Text, source code, spreadsheets, executable programs, legal contracts, and medical scans all break or lose meaning if a single value changes. When the data must be reconstructed exactly, lossless is preferred.
- If no, lossy is often the smart choice. For photos, music, and video that a human will simply view or hear, lossy delivers a much smaller file at a quality the person will accept, which is why it dominates streaming and the web.
Scenario questions almost always hinge on one clue: does the situation demand a perfect copy or just a good-enough one? "Archive," "legal record," "source code," and "must be recovered exactly" point to lossless. "Stream," "download quickly," "small thumbnail," and "a listener or viewer" point to lossy. Read the stem for that clue before you look at the options.
AAAAABBB? Type it with no spaces.Key Vocabulary
| Term | Definition | Example |
|---|---|---|
| Data compression | Reducing the number of bits used to store or transmit data | Zipping a folder before emailing it |
| Redundancy | Repeated or predictable patterns in data that compression can remove | A long run of the same pixel color |
| Lossless | Compression that guarantees the original can be reconstructed exactly | ZIP, PNG, RLE |
| Lossy | Compression that permanently discards data for a much smaller file | JPEG, MP3 |
| Run-length encoding | A lossless method that replaces each run of repeats with count + symbol |
AAAAABBB becomes 5A3B
|
| Reconstruction | Rebuilding the data from its compressed form when decompressing | Unzipping restores every byte |
| Trade-off | Balancing smaller file size against loss of quality or detail | Lower JPEG quality for a smaller photo |
The Create Task is a program you build, and your written response has to explain how the program manages data. You will not compress files by hand, but the same reasoning you just practiced, choosing a representation that fits the purpose, is exactly what graders want to see described clearly. When your program stores or moves data, the format you pick is a decision you can justify.
What a clear data explanation sounds like
Suppose your Create Task saves a list of user scores and reads them back later:
A response earns credit when it names the data, states how it is stored, and ties that choice to the program's purpose. The habit is the same one compression teaches: match the representation to what the data is for.
- "The list
scoresstores each user's results so the program can display and total them later." - "Because every score must be kept exactly, the data is stored without any loss, the way lossless compression preserves the original values."
- "Choosing a list lets the program handle any number of scores, which is how it manages the complexity of varied input."
The idea that carries over
Compression is a concrete case of a bigger CSP theme: data must be represented in a way that fits its use. Exact-copy data (text, records, code) is kept losslessly; human-perceived media (images, sound) can accept lossy trade-offs. Being able to state why a representation fits the purpose is what turns a vague answer into a point-earning one. See the full Create Task module →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
5A3B has fewer characters than AAAAABBB, and fewer characters means information was lost." Which choice best identifies the error in this argument?5A3B is 4 characters and AAAAABBB is 8, so the encoded form really is shorter. The flaw is elsewhere in the reasoning.5A3B decodes back to AAAAABBB exactly, so nothing was lost and the method is lossless.- I. Saving a diagram as a PNG image
- II. Compressing a folder of documents into a ZIP file
- III. Saving a photograph as a JPEG image
Practice: Compression Challenge
Put the ideas to work. Each round either asks you to pick the right compression method for a real use case or to crack a run-length encoding by hand. Fast, accurate answers build a streak. Aim to reason from the size-versus-quality trade-off, not from a guess.
Compression Challenge
Lossless vs lossy, and the size versus quality trade-off (AP CSP 2.2)
How to play: pick the right compression for each use case, then crack the run-length encoding rounds. Fast answers keep your streak alive.
Loading...
Nice work!
Frequently Asked Questions
ZIP, PNG, and run-length encoding are lossless; JPEG and MP3 are lossy.AAAAABBB has a run of five A characters and a run of three B characters, so it encodes to 5A3B. Because the counts and symbols are kept, the original can be rebuilt exactly, which makes it lossless.🔗 Continue studying
The Superpack includes an editable Topic 2.2 slide deck with animated lossless-versus-lossy demos, a run-length encoding worksheet with an answer key, a use-case sorting activity, and a unit quiz. View what's included →
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]