AP Courses AP Biology AP Biology Units AP Human Geography AP HUG Units AP Computer Science Principles AP CSP Units
Practice Daily Practice Practice by Course Practice by Topic Practice Tests
AP Exam Resources AP Exam Dates Registration Fees Scores & Credit What to Bring
Start Practicing → Login Register →

AP Computer Science Principles · Unit 2 · Data

Data Compression in AP CSP

Unit 2 · Compression · ~8 min read

Data compression is the process of reducing the number of bits needed to store or transmit data. In AP Computer Science Principles Unit 2, compression matters because smaller files can save storage space, transfer faster, and use less bandwidth.

On this page, you will learn why compression is useful, how compression ratios work, how run-length encoding uses repeated patterns, and how to avoid common AP CSP compression mistakes.

Updated May 21, 2026Reviewed by APScore5 Editorial TeamAP CSP Unit 2 · Data

File sizeCompression ratioPercent savedRun-length encodingTradeoffsAP-style practice
Step 1Why compressStorage and transfer. Step 2RatiosOriginal ÷ compressed. Step 3RLERepeated runs. Step 4PracticeTwelve topic MCQs.
Direct answer

In AP CSP, data compression means reducing the number of bits needed to represent data so files can use less storage and transfer faster—always check whether a question asks for ratio or percent saved.

Quick answer

What is data compression in AP CSP?

In AP CSP, data compression means reducing the number of bits needed to represent data. Compression can make files smaller, save storage space, reduce transfer time, and help data move across networks more efficiently.

In one sentence: Data compression makes files smaller by using fewer bits to represent the same or similar information.

Tiny example: If a 100 MB file is compressed to 25 MB, the compressed file uses less storage and can usually transfer faster.

Start on the AP CSP Unit 2 Data hub, or review bits and bytes if file-size units still feel fuzzy.

Why compress

Why is data compressed?

Compression solves a simple problem: raw data can take up too much storage and take too long to transfer.

Reason to CompressWhy It Matters
Save storageSmaller files take up less space
Transfer fasterSmaller files move across networks more quickly
Reduce bandwidthStreaming and downloads use fewer bits
Improve performanceApps and websites can load faster
Handle large mediaPhotos, audio, and video are easier to store and share
AP exam tip: If a question asks why compression is useful, answer with storage, transfer speed, bandwidth, or performance.
Ratio

How do you calculate compression ratio?

A compression ratio compares the original file size to the compressed file size. In AP CSP, write the ratio as original : compressed unless the question defines it another way.

Compression ratios AP CSP
Figure - Compression Ratios Compare File Sizes

Compression ratios compare original file size to compressed size to show storage savings.

Compression ratio = original size ÷ compressed size

For AP CSP: 100 MB compressed to 25 MB gives a compression ratio of 4:1.

Worked example

Original size = 100 MB
Compressed size = 25 MB
100 ÷ 25 = 4
Compression ratio = 4:1

OriginalCompressedRatio
100 MB25 MB4:1
80 MB20 MB4:1
50 MB10 MB5:1
30 MB10 MB3:1
12 MB3 MB4:1
AP exam tip: Do not reverse the ratio. If the original is 100 MB and the compressed file is 25 MB, the ratio is 4:1, not 1:4.
Percent

Compression ratio vs percent saved

Compression ratio and percent saved are related, but they are not the same answer. AP CSP questions may ask for either one.

Percent saved = amount reduced ÷ original size × 100

Worked example

Original size = 100 MB
Compressed size = 25 MB
Amount reduced = 75 MB
75 ÷ 100 × 100 = 75%
Percent saved = 75%

OriginalCompressedRatioPercent Saved
100 MB25 MB4:175%
80 MB20 MB4:175%
50 MB10 MB5:180%
30 MB10 MB3:166.7%
12 MB3 MB4:175%
AP CSP trap: 4:1 is a ratio. 75% is a percent saved. Do not swap them.
RLE

Run-length encoding in AP CSP

Run-length encoding, or RLE, is a simple compression idea. It replaces repeated values with a count and the value.

Run-length encoding RLE
Figure - Run Length Encoding Repeated Patterns

Run-length encoding compresses repeated patterns by storing counts instead of repeating identical values.

Example: AAAAAA can be represented as 6A.

When RLE works well

RLE works well when data has long repeated runs. Simple icons, flat-color images, repeated characters, or rows of identical pixels can shrink with RLE.

When RLE works poorly

RLE works poorly when data has few repeated values. A pattern like ABCDABCD may not shrink because there are no long runs to replace.

DataRLE ResultHelpful?
AAAAAA6AYes
BBBBCCCC4B4CYes
ABABABAB1A1B1A1B1A1B1A1BNo
WWWWBB4W2BYes
AP exam tip: Before choosing RLE, ask whether the data has long repeated runs.
Tradeoffs

Compression tradeoffs

Compression is useful, but it can involve tradeoffs. A compressed file may save space, but compression can require processing time or may reduce quality depending on the method.

BenefitPossible Tradeoff
Smaller file sizeTime needed to compress/decompress
Faster transferExtra processing
Less storagePossible quality loss if lossy
Lower bandwidth useSome methods may not preserve every detail

Lossless compression preserves the original exactly. Lossy compression removes some detail to create smaller files. This page gives the overview; the dedicated Lossless vs Lossy page explains format choices and scenario decisions.

Lossless vs lossy AP CSP
Figure - Lossless Vs Lossy Compression Tradeoffs

Lossless compression preserves all data, while lossy compression reduces file size by removing some detail.

Mistakes

Common mistakes about data compression

MistakeCorrection
Thinking compression always improves qualityCompression reduces file size; it does not automatically improve quality
Confusing ratio and percent saved4:1 is a ratio; 75% is percent saved
Reversing the ratioUse original : compressed unless told otherwise
Thinking all compression is losslessSome compression is lossless; some is lossy
Assuming RLE always helpsRLE helps when there are repeated runs
Ignoring transfer speedSmaller files often transfer faster
Forgetting file-size unitsUse the same units before comparing sizes
Exam

How AP CSP tests data compression

AP CSP data compression questions usually test why compression is useful, how to compare original and compressed file sizes, and whether a simple compression method works for a given pattern.

Question TypeWhat to Do
DefinitionCompression reduces the bits needed to represent data
BenefitMention storage, transfer time, or bandwidth
Compression ratioOriginal size ÷ compressed size
Percent savedAmount reduced ÷ original size × 100
RLE scenarioCheck for repeated runs
Tradeoff questionMention size vs quality/time/exactness
Lossless/lossy previewDecide whether exact reconstruction matters
AP exam tip: Read the question word carefully. Ratio, compressed size, percent saved, and tradeoff are different answer types.

After this page, try the Unit 2 quiz or the 50-question practice set.

Practice

AP CSP practice questions: Data compression

These are short topic checks. For full mixed Unit 2 practice, use the 50-question practice page. Tap an answer to reveal the explanation. Choices shuffle on load.

What is the main purpose of data compression?

Q1

A 100 MB file is compressed to 25 MB. What is the compression ratio?

Q2

A 100 MB file is compressed to 25 MB. What percent of the original file size was saved?

Q3

Which data pattern would run-length encoding compress best?

Q4

Which pattern is least likely to compress well with RLE?

Q5

Why might compression help a video stream?

Q6

Which statement is true?

Q7

A file is compressed from 80 MB to 20 MB. What is the compression ratio?

Q8

A file is compressed from 50 MB to 10 MB. What percent was saved?

Q9

Which is the best AP CSP explanation for why compression is useful?

Q10

Which answer best describes a compression tradeoff?

Q11

Which question should you ask before using RLE?

Q12
Before you leave

What you should be able to do now

Check each skill when you can explain it without looking at notes.

0 of 8 ready

Quick answers

Frequently asked questions

What is data compression in AP CSP?

Data compression in AP CSP means reducing the number of bits needed to represent data. Compression can make files smaller, save storage space, and reduce transfer time.

Why is data compression useful?

Data compression is useful because smaller files take less storage, use less bandwidth, and can transfer faster across networks.

What is a compression ratio?

A compression ratio compares original file size to compressed file size. For example, if a 100 MB file becomes 25 MB, the compression ratio is 4:1.

How do you calculate percent saved in compression?

Percent saved equals the amount reduced divided by the original size, multiplied by 100. If a 100 MB file becomes 25 MB, 75 MB was saved, so the percent saved is 75%.

What is run-length encoding?

Run-length encoding, or RLE, is a compression method that stores repeated values as a count and the value. For example, AAAAAA can be represented as 6A.

When does run-length encoding work best?

Run-length encoding works best when data has long repeated runs, such as repeated characters or repeated pixels in simple images.

When does run-length encoding work poorly?

Run-length encoding works poorly when data has few repeated values. If the pattern changes often, RLE may add overhead instead of reducing size.

Is all compression lossless?

No. Some compression is lossless and preserves the original exactly. Other compression is lossy and removes some detail to make the file smaller.

What is the biggest compression mistake in AP CSP?

The biggest mistake is confusing compression ratio with percent saved, or reversing the ratio. Always check whether the question asks for ratio, remaining size, or percent saved.

What should I study after data compression?

After data compression, study lossless vs lossy compression, then take the Unit 2 quiz or full Unit 2 practice questions.

Practice Questions Unit 2 Hub