Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 6

Find element index or return -1

Easy

10 minute session

Summary

Find the first index of a target in a list, or return -1 if it isn't present.

Problem statement

Write a function find_index_or_minus_one(lst, target) that searches lst for target and returns the index of the first occurrence. If target is not present, return -1. Do not raise exceptions for missing elements.

Task

Implement a function that returns the index of the first occurrence of target in a list, or -1 when the target is not found.

Examples

Find existing element

Input

find_index_or_minus_one([1, 2, 3], 2)

Output

1

Explanation

2 is at index 1 in the list.

Input format

A function call: find_index_or_minus_one(lst, target)

Output format

Return an integer index (or -1 if not found).

Constraints

Use linear search (O(n)). The list may contain mixed types; equality comparison should be used.

Samples

Sample input 0

find_index_or_minus_one([], 'x')

Sample output 0

-1

Explanation 0

Empty list can't contain 'x', so return -1.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

07:42 PM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.