โš™๏ธ Engineering

The Slow Lookup

Stardate 55158.6 ยท UES Magnanimous

Briefing

Lt. Skree wants every entry id in our log that also appears in the other ship's log, in our order. Both logs have about two hundred thousand entries. The current routine checks each of ours against every one of theirs, which is forty billion comparisons, and Skree has calculated it will finish "on Thursday, of a different week".

The fix is not a faster loop. It is a different data structure: put theirs into a set, where membership is a hash lookup rather than a scan, and the same job takes a fraction of a second. ARCHIE will check the small cases first, then a medium one with a stopwatch, and only if that is fast will it attempt the full two hundred thousand. Do not, Commander Raghunathan says, make it attempt the full two hundred thousand the slow way.

Mission objectives

ARCHIE checks all five every time you run diagnostics.

  • โ€ขCommon ids on a small case, in our order
  • โ€ขNo overlap gives nothing
  • โ€ขIds in theirs but not ours are not included
  • โ€ขA medium case is fast (a set, not a scan)
  • โ€ขThe full two hundred thousand, if the medium case was fast
Ask Commander Raghunathan for a hint

Build a set from theirs once, then keep each of ours that is in the set. That is O(n) instead of O(nยฒ). Rust: HashSet; Python: set().