{
  "name": "hard-implement-trie",
  "description": "Implement a Trie with autocomplete functionality",
  "dataset": "terminal-bench-local",
  "difficulty": "hard",
  "instruction": "Implement a Trie (prefix tree) data structure in Python. Create a file called trie.py with a class Trie that supports:\n\n1. insert(word: str) -> None - Insert a word into the trie\n2. search(word: str) -> bool - Return True if the word exists in the trie\n3. starts_with(prefix: str) -> bool - Return True if any word starts with the given prefix\n4. autocomplete(prefix: str) -> list[str] - Return all words that start with the given prefix, sorted alphabetically\n5. delete(word: str) -> bool - Delete a word from the trie. Return True if the word existed and was deleted.\n\nHandle edge cases: empty strings, single characters, overlapping prefixes.",
  "setup_files": {
    "test_trie.py": "import sys\nfrom trie import Trie\n\npassed = 0\nfailed = 0\n\ndef check(desc, got, expected):\n    global passed, failed\n    if got == expected:\n        passed += 1\n    else:\n        print(f\"FAIL {desc}: got {got}, expected {expected}\")\n        failed += 1\n\nt = Trie()\n\n# Basic insert and search\nt.insert('apple')\nt.insert('app')\nt.insert('apricot')\nt.insert('banana')\nt.insert('band')\nt.insert('bandana')\n\ncheck('search apple', t.search('apple'), True)\ncheck('search app', t.search('app'), True)\ncheck('search ap', t.search('ap'), False)\ncheck('search banana', t.search('banana'), True)\ncheck('search ban', t.search('ban'), False)\ncheck('search missing', t.search('cherry'), False)\n\n# starts_with\ncheck('prefix ap', t.starts_with('ap'), True)\ncheck('prefix ban', t.starts_with('ban'), True)\ncheck('prefix xyz', t.starts_with('xyz'), False)\ncheck('prefix empty', t.starts_with(''), True)\n\n# autocomplete\ncheck('autocomplete ap', t.autocomplete('ap'), ['app', 'apple', 'apricot'])\ncheck('autocomplete ban', t.autocomplete('ban'), ['banana', 'band', 'bandana'])\ncheck('autocomplete band', t.autocomplete('band'), ['band', 'bandana'])\ncheck('autocomplete xyz', t.autocomplete('xyz'), [])\ncheck('autocomplete apple', t.autocomplete('apple'), ['apple'])\n\n# delete\ncheck('delete app', t.delete('app'), True)\ncheck('search app after delete', t.search('app'), False)\ncheck('search apple still exists', t.search('apple'), True)\ncheck('autocomplete ap after delete', t.autocomplete('ap'), ['apple', 'apricot'])\ncheck('delete nonexistent', t.delete('cherry'), False)\ncheck('delete banana', t.delete('banana'), True)\ncheck('search band still exists', t.search('band'), True)\ncheck('autocomplete ban after delete', t.autocomplete('ban'), ['band', 'bandana'])\n\n# Edge cases\nt2 = Trie()\nt2.insert('')\ncheck('empty string search', t2.search(''), True)\nt2.insert('a')\ncheck('single char search', t2.search('a'), True)\ncheck('single char prefix', t2.starts_with('a'), True)\n\nprint(f\"{passed}/{passed+failed} tests passed\")\nif failed > 0:\n    sys.exit(1)\n"
  },
  "verify": "cd $BENCH_WORK_DIR && python3 test_trie.py",
  "timeout": 240000,
  "tags": ["hard", "data-structure", "implementation", "python"]
}
