These questions follow Efficiency and Big-O. Counting is exact and portable; timing is neither. Every question below asks you to count first and measure second — and to say what your measurement is actually evidence of.

Counting and classifying

  1. Give the Big-O of each, in terms of the length of values, and say what you counted:
    def first_item(values):
        return values[0]
     
    def total(values):
        running = 0
        for value in values:
            running = running + value
        return running
     
    def every_pair(values):
        pairs = []
        for first in values:
            for second in values:
                pairs.append((first, second))
        return pairs
  2. count_steps(n) runs a loop inside a loop, each range(n). It returns 100 for n = 10 and 400 for n = 20. Predict n = 40 without running it, then say what rule you used.
  3. Simplify each to Big-O and say which term you kept and why: , , , .
  4. Write halvings(n), which counts how many times n can be halved with // before it reaches 1. Report it for 8, 1 000, and 1 000 000, and name the Big-O it is measuring.

Measuring

  1. Write comparisons_to_find(values, target), counting the comparisons a linear search makes. Report the count for the first item, the last item, and an absent item in a list of 1 000, and label each as best, worst, or average case.
  2. Predict, then measure. Which of these finds duplicates faster, and by how much at 4 000 items? Time both at 1 000, 2 000, and 4 000 and describe the two growth patterns.
    def has_duplicate_slow(values):
        """True when any value appears twice. Compares every pair."""
        for first in range(len(values)):
            for second in range(first + 1, len(values)):
                if values[first] == values[second]:
                    return True
        return False
     
     
    def has_duplicate_fast(values):
        """True when any value appears twice. Remembers what it has seen."""
        seen = {}
        for value in values:
            if value in seen:
                return True
            seen[value] = True
        return False
  3. A classmate reports that their sort “takes 0.4 seconds, so it is “. Name two things wrong with that sentence, and describe the smallest experiment that would settle the question.

Judgement

  1. Your team’s program takes eleven seconds to produce the community centre’s monthly report. One member wants to replace the sort with a faster one. What would you measure first, and what are the two most likely outcomes of that measurement?
  2. A pull request replaces a linear search with a binary search and makes the report four times faster. All existing tests pass. Write the review comment you would leave, and say what would have to be true before you approve it.
  3. Theoretical bounds. Why is it impossible for any comparison-based sorting algorithm to achieve a worst-case time better than ? Model comparison sorting as a binary decision tree and prove that the tree’s height must be .

Answers

Curriculum connection

C2.1

demonstrate the ability to analyse a precondition (i.e., starting state) and a postcondition (i.e., ending state) in an algorithm;

Link to original

C2.2

compare the efficiency of linear and binary searches, using run times and computational complexity analysis (e.g., to analyse the number of statements executed, the number of iterations of a loop, or the number of comparisons performed);

Link to original

C2.3

compare the efficiency of sorting algorithms, using run times and computational complexity analysis (e.g., to analyse the number of statements executed, the number of iterations of a loop, or the number of comparisons performed);

Link to original

C2.4

identify common pitfalls in recursive functions (e.g., infinite recursion, exponential growth in recursive algorithms such as Fibonacci numbers).

Link to original

D4.2

investigate a topic in theoretical computer science (e.g., cryptography, graph theory, logic, computability theory, attribute grammar, automata theory, data mining, artificial intelligence, robotics, computer vision, image processing), and produce a report, using an appropriate format (e.g., website, presentation software, video);

Link to original