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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

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
Two Pointers
Run Length Tracking
Arrays
State Machine
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: list elev of integers.
  • A valid segment has indices i < p < j with strictly increasing values from i to p and strictly decreasing from p to j.
  • 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...

Submit Your Answer
Markdown supported