Lesson guide
What this Python exercise practices
Find First Negative is a beginner practice lesson that focuses on loops, iteration, counters. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Lists or strings
- Basic for loop syntax
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Related public exercises
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.
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.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.