Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 11

Find First Negative

Easy

8 minute session

Summary

Search a list to find the index of the first negative number.

Problem statement

Given a list of integers, return the index (0-based) of the first negative number. If there are no negative numbers, return -1.

Task

Use iteration to locate the first element satisfying a condition and return its index.

Examples

Negative in the middle

Input

find_first_negative([1, 2, -3, 4])

Output

2

Explanation

The first negative number is -3 at index 2.

Input format

A list of integers.

Output format

An integer representing the index of the first negative, or -1 if none.

Constraints

Do not use built-in functions that directly find index by predicate. Aim for O(n) time and O(1) extra space.

Samples

Sample input 0

find_first_negative([-1, 2, -3])

Sample output 0

0

Explanation 0

The very first element is negative, so return index 0.

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.

03:41 AM

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