Longest Strictly Rising Trail on a Terrain Elevation Map

Last updated: November 29, 2026

Quick Overview

Given an m x n grid of elevations from a Google Maps terrain layer, return the length of the longest path that moves up, down, left or right and strictly increases in elevation at every step.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given an m x n grid of elevations from a Google Maps terrain layer, return the length of the longest path that moves up, down, left or right and strictly increases in elevation at every step.

A Google onsite problem that separates candidates who memoize from those who rerun DFS from every cell. The interviewer checks that you argue why no cycle is possible (strict increase makes the move graph a DAG), and that you can switch to a topological BFS if recursion depth is a concern.

What the Interviewer Expects
  • Observes strict increase means the move graph is acyclic
  • Uses DFS with memoization so each cell is solved once
  • Mentions recursion depth limits and offers an iterative alternative
  • Achieves O(mn) time and space
  • Handles plateaus, where equal neighbors are not allowed
Key Topics to Cover
DFS with Memoization
DAG Longest Path
Topological Sort
Grid Graphs
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 actual path, not only its length.
  • Allow at most one step onto an equal elevation; how does the state change?
  • The grid is too large for one machine; how would you partition the work?
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: heights, m x n ints.
  • A move goes to a 4-neighbor with strictly greater height.
  • Output: the number of cells on the longest such path (at least 1 for a non-empty grid)....
High-Level Design

Treat each cell as a node with edges to strictly higher neighbors. Strictness means you can never return to a cell, so the graph is a DAG and the longest path is well defined. `best(r, c) = 1 + max(be...


Submit Your Answer
Markdown supported