The Lawfare Academy

Lawfare Group → Knowledge Institute → Lawfare Academy

← Back to program

← CS 201 — Data Structures, Algorithms and Systems

Weeks 15–16

Systems, resource budgets and an integrated comparison

Learning objectives

  • Explain and trace the mechanisms in systems, resource budgets and an integrated comparison.
  • Apply the method to the worked example and changed-condition exercise.
  • Identify assumptions, failure cases and the evidence needed to justify a result.

Original on-site study sequence

Study this preserved lesson with the expanded original reader linked in Further reading. It supplies mathematical preparation, advanced representations, complete original and changed models, the Cedar cumulative investigation and a 24-point rubric. Use the resource-safe laboratory edition for optional local execution. All older publisher and textbook links are optional references. Current whole-course internal review remains pending.

Programs execute within systems that impose resource limits and failure modes. A process has memory and execution state; files provide persistence; network requests can fail independently of local calculation. Moving data between components has a cost. An asymptotically efficient algorithm can still be impractical if it repeatedly serializes huge records or holds unnecessary copies.

Separate latency, throughput and memory. Latency is time for one operation; throughput is operations per unit time under specified load. Increasing concurrency can improve throughput until another bottleneck dominates, while worsening tail latency. Report input sizes, repetitions, measurement method and variability. A single fastest timing is poor evidence. Verify outputs before comparing speed, because removing validation can make an incorrect program look faster.

Resource budgets are design constraints. If an in-memory index exceeds available memory, an on-disk database or streaming strategy may be preferable. Streaming can compute totals without storing every row, but cannot answer arbitrary later queries unless it maintains sufficient state or rereads the data. Explain what information a summary discards. Resilience requires understanding interruption: a crash between changing a source record and updating its index can violate consistency. Rebuildable indexes and transactional storage address different parts of this problem.

Worked reasoning The loop holds aggregate state rather than all rows, so its auxiliary state is small. It consumes an iterator; callers cannot assume that the same iterator still yields those rows afterward. The total deliberately discards per-customer detail. If later queries require customer totals, maintain a customer mapping whose size grows with distinct customers or use persistent storage.

Extended teaching and laboratory

A resource budget includes input storage, auxiliary state, output and transient copies. An iterator-based aggregate may hold little auxiliary state, yet its caller may already have materialized the entire input. A benchmark must identify which costs are included. Reusing an exhausted iterator can make a second algorithm appear artificially fast because it processes no records.

Measure correctness before performance. Use the same logical records, reset the input for every run, count operations where possible and report distributions rather than one favorable time. For memory, distinguish a constant running total from per-customer state that grows with the number of distinct customers.

Worked problem A generator produces one million transactions. Method A sums it, then Method B indexes the same generator and reports a tiny runtime and zero customers. Evaluate the comparison.

Step-by-step reasoning 1. A consumes the iterator. B receives no remaining records, so its output fails the shared logical-input requirement.

  1. Recreate the generator from the same deterministic source for each method. Verify count and total before comparing runtime.
  1. An overall total uses constant aggregate state; an index needs storage proportional to represented keys and records, depending on its output contract.
  1. Include construction costs, query volume and update workload. One timing cannot establish an asymptotic law or a universal winner.

Apply the idea

A generator produces one million transactions. Method A sums it, then Method B indexes the same generator and reports a tiny runtime and zero customers. Evaluate the comparison.

The application only needs the five largest customer totals. Can it always use constant memory while reading unsorted transactions? Explain what must be retained or moved elsewhere.

Read the worked reasoning

Exact arbitrary customer totals generally require state for customers that may reappear later. Keeping only the current five can discard a customer whose later transactions make it the largest. Aggregate by customer in a mapping, external sort, or database, then select five totals. A fixed-size top-five heap helps after complete customer totals exist; it does not eliminate the grouping requirement.

Check understanding

What should a defensible benchmark compare first?

  1. Equivalent specified behavior and correct outputs
    Feedback

    Timing is meaningful only for comparable correct work.

  2. Only the fastest observed run
    Feedback

    A selected best run can hide variability and setup costs.

  3. Implementations with validation removed from one side
    Feedback

    Unequal responsibilities invalidate the comparison.

Further reading

Study the original native lesson and expanded on-site reader. Work through the relevant mathematical and representation mechanisms, then the complete Cedar original/changed investigation and rubric. The retained workbook and weighted-path model provide further original practice. All practice and source references are optional; no external chapter or exercise is required.

My learning →