Checkpoint: Choosing Structures
Challenge on lesson: Checkpoint: Choosing Structures
Final module integration. Two problems, one file:
1. int structure_penalty(int n_ops, int lookups, int inserts) โ a candidate stores n_ops records in an unordered array and answers lookups by linear scan. Return the total elementary operations: lookups * n_ops + inserts (each append is O(1)). Then implement int sorted_penalty(int n_ops, int lookups, int inserts) where a sorted array answers lookups in log2 steps (use the ceiling of log2(n_ops), and 0 steps when n_ops == 0) but inserts cost n_ops shifts: lookups * ceil_log2(n_ops) + inserts * n_ops.
2. const char *choose(int lookups, int inserts) โ return "hash" when lookups >= inserts, otherwise "sorted". The penalty functions are the evidence; the chooser is the decision.
Back to lesson: Checkpoint: Choosing Structures