enumerate: Index and Value Together

~9 mincode completion

Write index_of_largest(values) that returns the index of the largest value in the list. If the largest value appears more than once, return the index of the first one. Assume the list is not empty.

[4, 9, 2, 9] returns 1.

Examples

Returns the index, not the value itself

Input
index_of_largest([4, 9, 2, 7])
Output
1

When the largest value repeats, the earliest index wins

Input
index_of_largest([4, 9, 2, 9])
Output
1

The largest can be the first item

Input
index_of_largest([10, 1, 2])
Output
0

Hints

Hint 1

gives you the index and the value together.

Hint 2

Return index rather than value.

Requirements

  • : a non-empty list of numbers

  • Return the index of the largest value. Ties go to the earliest one.

Constraints

  • Standard library only, no imports required

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~9 min

3 employers weight this skill

2 frontier labs, 1 AI product company. Top match scores 80.

Python
def index_of_largest(values):
    """
    Find the position of the largest value.

    Args:
        values: a non-empty list of numbers

    Returns:
        The index of the largest value. Ties go to the earliest one.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.