MediumRetrievalPython 3

Codebase Symbol Index

Build an incremental code symbol index with file replacement, deletion, case-insensitive prefix search, and stable ranking.

40m2 sample tests4 hidden tests

Implement SymbolIndex, a small in-memory index for code symbols.

Requirements

  • update_file(path, content) extracts symbols from one file and replaces any old symbols for that path.
  • remove_file(path) removes that file's symbols.
  • search(prefix, limit=10) returns matching symbols sorted by exact match first, then symbol name, then path.
  • A symbol is a dictionary with name, path, and line.
  • Extract symbols from lines that start with def, class, function, const, let, or var.
  • Prefix matching is case-insensitive.

Example

python
1index = SymbolIndex() 2index.update_file("src/cache.py", "class Cache:\\n pass\\ndef clear_cache():\\n pass") 3 4assert index.search("Ca")[0] == {"name": "Cache", "path": "src/cache.py", "line": 1}

Constraints

  • Keep it in memory.
  • Don't use a parser library.
  • File updates must remove stale symbols from previous content.

Editor