Longest Climb-Then-Descent Segment on a Google Maps Hiking Route
Last updated: November 29, 2026
Quick Overview
Given elevation samples along a trail, return the length of the longest contiguous segment that strictly rises and then strictly falls, with at least one step up and one step down. Return 0 if none exists.
0
0
0 solved
Given elevation samples along a trail, return the length of the longest contiguous segment that strictly rises and then strictly falls, with at least one step up and one step down. Return 0 if none exists.
A Google technical-screen array problem framed around elevation profiles in Google Maps. The interviewer checks a single pass that tracks the up and down run lengths and resets them correctly on flats and new climbs.
What the Interviewer Expects
- Tracks the current ascending and descending run lengths
- Requires both an up and a down part
- Resets on equal neighbors
- Starts a new mountain when a rise follows a descent
- States O(n) time and O(1) space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the start and end indices of the longest segment.
- What if small plateaus of up to t equal samples are allowed at the peak?
- Count how many valid segments exist rather than the longest.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list
elevof integers. - A valid segment has indices
i < p < jwith strictly increasing values fromitopand strictly decreasing fromptoj. - Output: the maximum
j - i + 1, ...
High-Level Design
Walk once with two counters: up is the number of rising steps in the current segment and down is the number of falling steps after the peak. For each step from i - 1 to i:
- If we were descend...