Question bank › Monotonic Stack
Dsa Monotonic Stack Easy

The Altitude Drop

A hiker records their altitude at fixed intervals. For every reading, they want to know how much they have climbed since the last time they were at a strictly lower altitude. For each reading at index $i$, find the nearest index $j < i$ where $altitude[j] < altitude[i]$. Output the difference $altitude[i] - altitude[j]$. If no such $j$ exists, output 0. Input Format: A single line of space-separated integers representing altitudes. Output Format: A single line of space-separated integers representing the drop/diff values. Example: Input: 10 15 12 18 Output: 0 5 2 6

Key concepts

monotonic_stackprevious_smaller_element

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.