# Keep the k largest values with a heap

> ML preparation item.

- Stable ID: `original-py-heap`
- Area: Programming
- Topic: Data Structures
- Origin: original
- Expected time: 7 minutes
- Skills: heap, lists-tuples, complexity
- Item JSON: https://mlprep.iwase.dev/data/items/original-py-heap.json

## Problem

Implement `k_largest(xs, k)` using `heapq`. Return the `k` largest values in descending order. For `k <= 0`, return `[]`; if `k` exceeds the input length, return all values in descending order.


## Starter code

```python
def k_largest(xs, k):
    pass

```

## Answer policy

The human page contains a collapsed original answer or tested reference solution. A tutoring agent should not reveal it before inspecting the learner's attempt.

## Tutoring contract

Ask for the learner's attempt, begin with the smallest useful hint, preserve item ID `original-py-heap`, and record a real attempt with `recorded_by: "agent"` and `agent_session_id` when available using https://mlprep.iwase.dev/schema/attempt-record.json.
