Skip to main content

Longest Increasing Path in a Matrix

Mountain Trail Finder You're building a hiking app that finds the longest uphill trail in a terrain map. Given a grid where each cell represents elevation, find the longest path where each step moves to a strictly higher elevation. You can move up, down, left, or right. Example: [[9,9,4],[6,6,8],[2,1,1]] → 4 (path: 1→2→6→9)

This one is about turning repeated work into a reusable state instead of re-solving subproblems. It's a hard one for a reason: it stresses state-and-transition thinking when the rules pile up. Don't rush the code—spend your time nailing the state definition first.

Examples

You write

longest_increasing_path(matrix)

  1. Example 1

    Input
    matrix = [[9,9,4],[6,6,8],[2,1,1]]
    returns
    4
  2. Example 2

    Input
    matrix = [[3,4,5],[3,2,6],[2,2,1]]
    returns
    4

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Graphs

I reach for it when

  • The input is edges, prerequisites or connections.
  • You need cycles, an ordering or connected groups.

Say this before you type. Say whether the edges are directed, and which of the three jobs you are doing.

All 32 Graphs problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Longest Increasing Pathhard

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.