Learning objectives
- Specify a software rule in terms that can be tested at its boundaries.
- Explain why efficiency depends on workload and the chosen representation.
Learn
Before programming, specify which inputs enter, which output should leave, and how to handle an empty or invalid input. An invariant is a property that remains true at a named step, such as “the count equals the number of accepted records processed so far.” A correct computation, a correct handling of unusual inputs, and adequate performance are separate duties. Choose tests with expected outputs that follow from the contract.
From a question to an operational contract
Computing begins by making a task precise enough that a result can be checked. An input domain states what information is accepted. A postcondition describes the relationship the output must bear to that input. Preconditions identify assumptions a component relies on. A failure policy describes what happens when the input is outside that domain or a resource is unavailable. For example, 'return the earliest request' requires decisions about time zones, missing timestamps and ties. Faster code cannot repair an undefined meaning of earliest.
Abstraction deliberately preserves some details and hides others. A queue interface can promise insertion at one end and removal at the other without exposing whether storage uses linked nodes or a circular array. That hidden implementation still affects capacity and performance, so the interface must state any relevant limits. Decomposition assigns responsibilities to components whose contracts fit together. If one component returns seconds and another interprets milliseconds, each can appear locally reasonable while their composition fails. A useful boundary includes representation and units as well as a function name.
An algorithm is a finite description of a procedure; a program expresses behavior through the rules of a programming language and its execution environment. Termination is a property to establish for a particular procedure and input class, not a guarantee provided by calling something an algorithm. A correct implementation must satisfy the contract, including termination where promised. The retained native lower-bound investigation supplies a full invariant argument and changed-boundary counterexample; this expansion explains the programming and representation mechanisms around that investigation.
Data structures and resource tradeoffs
An array-like sequence supports positional access and ordered traversal. A linked structure follows references between nodes and offers different insertion and traversal costs. A stack removes the most recently added item; a queue removes the oldest queued item under a first-in-first-out contract. A map associates keys with values; a set represents membership without retaining a meaningful count of repeated equal elements. A tree organizes parent/child relationships; a graph permits more general connections. These are concepts and interfaces: a particular library supplies concrete semantics, capacity and cost guarantees.
Choosing a set for fictional readings [4, −1, 4, 0] produces the distinct values{4, −1, 0}. That changes the task if repeated observations must contribute separately. A map keyed by request ID similarly collapses repeated identifiers unless the design records or rejects duplicates explicitly. A hash-based implementation uses a hash to locate candidates and equality to distinguish keys that collide. Equal hash values do not prove equal keys, and expected lookup performance does not establish an unconditional worst-case bound.
Asymptotic analysis studies resource growth under a stated model. A scan can require a number of element inspections proportional to n; comparing every unordered pair requires n(n−1)/2 comparisons. For n=6 that is 15; for n=12 it is 66, not 30. A logarithm describes repeated multiplicative reduction, as developed in the preserved search model. Big-O is an upper-bound relation up to constants beyond a threshold. It is neither an exact time nor necessarily a tight bound; use a matching lower bound or Theta notation when claiming a tight growth rate. Count construction, copying, updates and memory where the real workload pays those costs.
Apply
A fictional community center wants to count distinct check-ins without counting each scanned badge repeatedly. The input is a sequence of badge identifiers. The proposed contract counts each exact nonempty identifier once after validation. For inputs A17, A17, B09, and blank, it should return two distinct valid identifiers and report one invalid record separately. A list of identifiers already seen can work. Searching it repeatedly may become expensive on long inputs. A set can represent membership directly, while the program may need another structure if it also must preserve a first-seen order or report repeat counts. State whether identifier case or spacing is significant before normalizing. Check empty input, one unique record, duplicates, and both accepted and rejected values.
Optional check
Why does a successful test with all distinct badges say little about repeated badges?
Show the explained answer
The duplicate branch is never exercised. Use a trace with intentional repetitions and compare both the expected distinct count and the separate invalid or repeated record counts.