Skip to content
IGCSE·Tuition
Computer Science · Lessons

Check an algorithm against empty and duplicate data

An algorithm can pass the example in the question and still fail on the first list a tester tries.

On this page
  1. Which awkward cases should I try?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

Choosing test data is a skill of its own. Normal data shows the algorithm can work. Empty, single-item and repeated data show whether it still works at the edges, where most faults hide.

This lesson closes searching, sorting and files. It uses the testing language from validation, verification and testing and the trace tables from tracing a linear search.

Which awkward cases should I try?

  • Empty: no items at all.
  • One item: the smallest non-empty list.
  • All equal: every value the same.
  • Duplicates of the answer: the maximum or target appears more than once.
  • Answer at an end: first or last position.

Worked example

The task: find the highest score in an array Scores of Count items, and say how many times it occurs. A first version, with an empty-data guard:

IF Count = 0 THEN
   OUTPUT "No data"
ELSE
   Max ← Scores[1]
   Times ← 1
   I ← 2
   WHILE I <= Count
      IF Scores[I] > Max THEN
         Max ← Scores[I]
         Times ← 1
      ELSE
         IF Scores[I] = Max THEN
            Times ← Times + 1
         ENDIF
      ENDIF
      I ← I + 1
   ENDWHILE
   OUTPUT Max, Times
ENDIF

Test: Scores = [8, 5, 8, 3, 8], Count = 5.

IScores[I]MaxTimes
Start81
2581
3882
4382
5883

Output: 8, 3. The highest is 8 and it occurs three times.

Test: empty, Count = 0. The guard catches it and outputs No data. Without the guard, Scores[1] would refer to an item that does not exist.

Test: one item, Scores = [9], Count = 1. Max = 9 and Times = 1. The loop starts with I = 2, which is greater than Count, so the body never runs. Output: 9, 1.

The mistake to watch for

A student changes the comparison to >= to “include equal values”:

IF Scores[I] >= Max THEN
   Max ← Scores[I]
   Times ← 1

On [8, 5, 8, 3, 8], at I = 3 the value 8 satisfies >=, so Times is reset to 1 instead of increasing to 2. At I = 5 it resets again. The final output is 8, 1, which is wrong.

The correction is to keep > for a new maximum and use a separate = branch to count repeats. The faulty version only shows up with duplicate data, which is why the duplicate test matters.

Check yourself

Use the algorithm from the worked example.

1. Scores = [4, 4], Count = 2. What is output?

Show answer

Start Max = 4, Times = 1. At I = 2, 4 equals Max, so Times becomes 2. Output 4, 2.

2. Scores = [2, 7, 7, 7], Count = 4. What is output?

Show answer

Start Max = 2. At I = 2, 7 > 2, so Max = 7 and Times = 1. At I = 3, Times = 2. At I = 4, Times = 3. Output 7, 3.

3. Name three test cases that would check an algorithm that finds the lowest value in a list.

Show answer

For example: an empty list, a one-item list, and a list where the lowest value appears twice. A list with the lowest value in the last position is also worthwhile.

Where this leads next

Put the whole module to work in the mixed practice set, and log each slip in the mistake log and retest queue.

If you tend to test only the example that came with the question, our teachers can help you build the edge-case habit in online one-to-one Computer Science tuition.

Questions people ask

Why test an empty list?

Many algorithms read the first item before checking whether one exists. On an empty list that reads something that is not there, or divides by a count of zero. Testing empty data shows whether the algorithm has a guard for that case.

What is a duplicate in test data?

It is a value that appears more than once, such as 8 twice in a list of scores. Duplicates test whether an algorithm counts, replaces or compares correctly when two items are equal, which tidy data never does.

How many test cases are enough?

Enough to cover each kind of situation once: normal data, the smallest case, an empty case, repeated values and the boundary where behaviour changes. One well-chosen case per situation is more useful than many similar ones.

Updated:

Your next step

If your algorithms work on the example but you are unsure which test cases to try next, a one-to-one teacher can help you build a small set of tests and trace each one.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service