FastPrepLongest Decreasing Matrix Path with Limited Relaxations
Problems/Longest Decreasing Matrix Path with Limited Relaxations

Longest Decreasing Matrix Path with Limited Relaxations

Duolingo logoDuolingo● HardNEW GRADONSITE INTERVIEW

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This practice problem keeps the reported decreasing-path core, the path-output follow-up, and the k-relaxation follow-up in one callable contract; deterministic tie-breaking and finite bounds are added for judging.

Given an integer matrix and a nonnegative integer k, return a longest orthogonal path of cell coordinates.

A move to an up, down, left, or right neighbor is:

  • free when the next value is strictly smaller than the current value; or
  • a relaxation when the next value is greater than or equal to the current value.

The path may use at most k relaxed moves. Because every cycle contains at least one non-decreasing move, the relaxation budget keeps every valid walk finite; a coordinate may therefore be revisited only after spending additional budget.

Return the coordinates as [row, column] rows. If several paths have maximum length, return the lexicographically smallest coordinate sequence. Setting k = 0 is the original longest-decreasing-path task, while returning coordinates and accepting positive k preserve both reported follow-ups.

Function

longestRelaxedDecreasingPath(matrix: int[][], k: int) → int[][]

Examples

Example 1

matrix = [[9,8,7],[2,3,6],[1,4,5]]k = 0return = [[0,0],[0,1],[0,2],[1,2],[2,2],[2,1],[1,1],[1,0],[2,0]]
matrix
012098712362145
→
return
01000101202312422521611710820

The spiral visits values 9, 8, 7, 6, 5, 4, 3, 2, 1. Every move is strictly decreasing, so no relaxation is used.

Example 2

matrix = [[1,2]]k = 1return = [[0,1],[0,0],[0,1],[0,0]]
matrix
01012
→
return
01001100201300

Start at value 2, move down to 1 for free, spend the one relaxation to return to 2, then move down to 1 again. The repeated coordinates have different remaining-budget states.

Example 3

matrix = [[4,3],[2,1]]k = 0return = [[0,0],[0,1],[1,1]]
matrix
01043121
→
return
01000101211

There are multiple decreasing paths of length three. The returned one is lexicographically smallest.

Constraints

  • 1 <= matrix.length, matrix[0].length <= 20.
  • All rows have the same length.
  • -10^9 <= matrix[r][c] <= 10^9.
  • 0 <= k <= 5.

More Duolingo interview questions

  • Rank Word-Translation Hints from Sentence TranslationsONSITE INTERVIEW · Seen Apr 2026
  • Group Timestamps into Consecutive-Day IntervalsPHONE SCREEN · Seen Nov 2026
  • Return Every Matching DecryptionPHONE SCREEN · Seen Nov 2026
  • Visit Desired Attractions Without Reusing a TrailPHONE SCREEN · Seen Oct 2026
  • Correct an In-Place Array ReversalPHONE SCREEN · Seen Oct 2026
  • Count Starts Reaching a Grid MaximumPHONE SCREEN · Seen Oct 2026
  • Select the Top User MatchesPHONE SCREEN · Seen Oct 2026
  • Sum All Values in a Binary TreePHONE SCREEN · Seen Oct 2026
  • User Resource Transition ProbabilitiesPHONE SCREEN · Seen Oct 2026
  • Encrypt and Decrypt StringsPHONE SCREEN · Seen Apr 2026

Similar questions at other companies

  • Longest Matrix Walk with Two-Step ComparisonsGoogle · ONSITE INTERVIEW
  • Longest Increasing Path in a MatrixNew Relic · PHONE SCREEN · ONSITE INTERVIEW
  • Longest Increasing Path in a MatrixVisa · ONSITE INTERVIEW
  • Making a Large IslandMeta · ONSITE INTERVIEW
  • Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Island Count and Maximum AreaOracle · ONSITE INTERVIEW
  • Number of IslandsTripjack · ONSITE INTERVIEW
  • Number of IslandsTekion · ONSITE INTERVIEW
  • Grid Pathfinding with Obstacles (DFS)Roblox · PHONE SCREEN · ONSITE INTERVIEW
  • Number of IslandsGoldman Sachs · OA · PHONE SCREEN · ONSITE INTERVIEW
See Duolingo hiring insights
Saved
class Solution {
    public int[][] longestRelaxedDecreasingPath(int[][] matrix, int k) {
        // write your code here
    }
}
Checking account…