Longest Strictly Rising Trail on an Engagement Heatmap
Last updated: November 29, 2026
Quick Overview
Given a grid of engagement scores, return the length of the longest path that moves up, down, left or right and visits strictly increasing scores.
Meta
November 29, 20260
0
0 solved
Given a grid of engagement scores, return the length of the longest path that moves up, down, left or right and visits strictly increasing scores.
Meta onsites use this to test whether you see that the strictly increasing constraint makes the grid a DAG, so memoized DFS works without a visited set. The interviewer also probes recursion depth limits and an iterative topological alternative.
What the Interviewer Expects
- Notice strict increase rules out cycles
- Memoize the longest path starting at each cell
- Avoid a visited set by relying on the DAG property
- Discuss recursion depth and the topological sort alternative
- State O(r * c) time
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
- How would you implement it iteratively with Kahn's algorithm by peeling sinks?
- What if equal neighbors were allowed (non-decreasing)?
- How would you return the path itself?
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 is a non-empty r x c integer matrix up to 200 x 200. A trail moves between edge-adjacent cells and each step goes to a strictly larger value. Return the number of cells in the longest trail....
High-Level Design
Draw an edge from each cell to every neighbor with a larger value. Because values strictly increase along edges, there are no cycles, so the longest path from a cell depends only on longer paths from ...