First peak
A free medium coding practice problem. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.
- Difficulty
- Medium
- Type
- Function
- Languages
- JavaScript, TypeScript or Python
Problem
A peak is an element strictly greater than its neighbours (the ends only need to beat their one neighbour; a single element is a peak). Return the index of the FIRST peak, or -1 if there is none.
Function signature
// JavaScript
function firstPeak(nums)
// TypeScript
function firstPeak(nums: any): any
// Python
def first_peak(nums):Examples
firstPeak([1,3,2,4,1])→1firstPeak([5,5,5])→-1firstPeak([1,2,3])→2
How checking works
Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.