{
  "name": "perf-slow-sort",
  "description": "Optimize a bubble sort to handle large inputs efficiently",
  "dataset": "terminal-bench-local",
  "difficulty": "medium",
  "instruction": "The file sort_records.py contains a function that sorts a list of records by a composite key. It works correctly but is too slow for large inputs — it uses a naive O(n²) bubble sort. Optimize it so it handles 100,000 records in under 2 seconds while keeping the same interface and output. Do NOT change test_sort.py.",
  "setup_files": {
    "sort_records.py": "def sort_records(records: list[dict]) -> list[dict]:\n    \"\"\"Sort records by ('department', 'salary' descending, 'name').\n    Returns a new sorted list.\n    \"\"\"\n    result = list(records)\n    n = len(result)\n    for i in range(n):\n        for j in range(0, n - i - 1):\n            a = result[j]\n            b = result[j + 1]\n            # Compare: department asc, salary desc, name asc\n            if (a['department'], -a['salary'], a['name']) > (b['department'], -b['salary'], b['name']):\n                result[j], result[j + 1] = result[j + 1], result[j]\n    return result\n",
    "test_sort.py": "import sys\nimport time\nimport random\nimport string\nfrom sort_records import sort_records\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!r}, expected {expected!r}\")\n        failed += 1\n\n# Correctness tests\nrecords = [\n    {'name': 'Alice', 'department': 'Engineering', 'salary': 90000},\n    {'name': 'Bob', 'department': 'Engineering', 'salary': 95000},\n    {'name': 'Charlie', 'department': 'Design', 'salary': 80000},\n    {'name': 'Diana', 'department': 'Design', 'salary': 80000},\n    {'name': 'Eve', 'department': 'Engineering', 'salary': 90000},\n]\n\nsorted_recs = sort_records(records)\ncheck('first record dept', sorted_recs[0]['department'], 'Design')\ncheck('tie-break name', sorted_recs[0]['name'], 'Charlie')\ncheck('second record', sorted_recs[1]['name'], 'Diana')\ncheck('highest salary eng', sorted_recs[2]['name'], 'Bob')\ncheck('salary desc then name asc', sorted_recs[3]['name'], 'Alice')\ncheck('last record', sorted_recs[4]['name'], 'Eve')\n\n# Empty and single\ncheck('empty', sort_records([]), [])\ncheck('single', sort_records([{'name': 'X', 'department': 'A', 'salary': 1}]), [{'name': 'X', 'department': 'A', 'salary': 1}])\n\n# Performance test: 100,000 records under 2 seconds\ndepts = ['Engineering', 'Design', 'Marketing', 'Sales', 'Support']\ndef rand_name():\n    return ''.join(random.choices(string.ascii_lowercase, k=8))\n\nrandom.seed(42)\nbig = [{'name': rand_name(), 'department': random.choice(depts), 'salary': random.randint(40000, 150000)} for _ in range(100000)]\n\nstart = time.time()\nresult = sort_records(big)\nelapsed = time.time() - start\n\ncheck('perf length', len(result), 100000)\ncheck('perf sorted', all(\n    (result[i]['department'], -result[i]['salary'], result[i]['name']) <=\n    (result[i+1]['department'], -result[i+1]['salary'], result[i+1]['name'])\n    for i in range(len(result) - 1)\n), True)\n\nif elapsed > 2.0:\n    print(f\"FAIL performance: took {elapsed:.2f}s (must be under 2s)\")\n    failed += 1\nelse:\n    passed += 1\n\nprint(f\"{passed}/{passed+failed} tests passed\")\nif failed > 0:\n    sys.exit(1)\n"
  },
  "verify": "cd $BENCH_WORK_DIR && python3 test_sort.py",
  "timeout": 240000,
  "tags": ["performance", "python", "optimization", "sorting"]
}
