The Lawfare Academy

Lawfare Group → Knowledge Institute → Lawfare Academy

← CS 201 — Data Structures, Algorithms and Systems

Weeks 3–5

Lists, stacks, queues and hash indexes

Learning objectives

  • Explain and trace the mechanisms in lists, stacks, queues and hash indexes.
  • Apply the method to the worked example and changed-condition exercise.
  • Identify assumptions, failure cases and the evidence needed to justify a result.

A data structure embodies access patterns and tradeoffs. A stack supports last-in-first-out behavior; a queue supports first-in-first-out behavior. They are policies, not merely variable names. Using a list's end for a stack is natural; repeatedly removing its first element shifts remaining references, while a deque supports efficient end operations.

A hash table maps a key to a storage location using a hash function, resolving collisions when distinct keys share a bucket. Average constant-time lookup depends on implementation and distribution assumptions; it is not a worst-case guarantee for arbitrary hostile inputs. Keys must remain suitable for equality and hashing. A dictionary mapping customer ID to one work order loses other work orders unless the intended value is a collection.

Indexing trades construction and maintenance effort for repeated lookup speed. For one small search, building a separate index may cost more than scanning. For many searches, it can be worthwhile. If a record changes, update all indexes consistently or return stale results. Memory matters too: storing keys and references uses additional space even when records are not copied.

Worked reasoning The value is a list of order identifiers, so repeated customers retain every order. setdefault supplies an empty list only when the key is missing. A mapping directly to a single order ID would overwrite order 1 with order 2. Queries are faster after building the index, but deleting order 1 now requires updating the list as well as the source records.

Extended teaching and laboratory

An index trades construction and maintenance costs for faster lookup. Comparing only one lookup ignores the work needed to build the index. For n records and q queries, repeated scans cost on the order of qn comparisons. A hash-based index has expected construction O(n) and expected lookup O(1) per query under ordinary hashing assumptions, but it consumes memory and must remain synchronized with updates. These are expected bounds, not guarantees that collisions never occur.

Choose an index value that preserves the information required by the query. Mapping customer to a single order overwrites history. Mapping to a list preserves multiplicity but makes deletion and ordering policies explicit responsibilities.

Worked problem A report has 10,000 orders and 1,000 customer queries. Compare repeated complete scans with one customer-to-order-list index, ignoring constants.

Step-by-step reasoning 1. Repeated scans examine up to 10,000,000 order positions. An index build visits 10,000 records before the 1,000 expected constant-time key lookups.

  1. Returning matching records still costs at least the number of records returned. O(1) key access does not make a large answer free.
  1. After deleting an order, update both authoritative records and the customer list, or rebuild the index. A stale index can be fast and incorrect.
  1. For a single query on a stream, building the complete index may have no worthwhile advantage. Include memory and update rates in the choice.

Apply the idea

A report has 10,000 orders and 1,000 customer queries. Compare repeated complete scans with one customer-to-order-list index, ignoring constants.

Most queries return all 10,000 orders for one customer. Can the indexed report have constant total query time? Explain the lower bound.

Read the worked reasoning

No. Producing 10,000 results requires at least proportional output work under the ordinary explicit-output model. The index can locate the list quickly, but cannot eliminate the cost of visiting or serializing its contents. Report key-access time separately from result-production time and avoid claiming O(1) for the whole report.

Check understanding

When is building an index most defensible?

  1. When repeated lookups justify its construction and maintenance costs
    Feedback

    Assess the complete workload rather than lookup time alone.

  2. For every one-time query regardless of input size
    Feedback

    A scan can be simpler and cheaper for one query.

  3. When duplicate customers should disappear
    Feedback

    An index should preserve the specified multiplicity.

Further reading

Preparation: study 1.1–1.4: counting principles, combinations and combinatorial proof in the complete on-site Levin text. Attempt the investigation, work at least four exercises of increasing difficulty in each assigned section, and compare the selected hints/solutions only after a written attempt. Then complete the algorithm reading and implement the unit laboratory. For each algorithm give a specification, trace, correctness argument and time/space analysis.

My learning →