DataLane

27. Top-k indices

hardProNot started~12 minPythonImplement topk()

Asked in screens shaped like: Google, NVIDIA

Return the indices of the k largest values in x, largest first. Ties keep the earlier index.

Examples

  • Example 1

    Input: { "x": [0.1, 0.9, 0.3], "k": 2 }
    Output: [1, 2]

Constraints

  • 1 <= k <= len(x)

Python editor

Loading Python engine…

Ctrl/Cmd + Enter submits

Run your code to see stdout.

Pro problem

Hard problems unlock with Pro

Easy pads stay free. Medium and hard SQL and Python problems — editor, tests, hints, and solutions — open after you upgrade to Pro or coaching.

See plansPractice free easy problems
↑↓ navigate openesc close