Count longest subsequence in matrix

Last updated: November 19, 2025

Quick Overview

Given a matrix of integers, write a function to count the length of the longest increasing subsequence that can be formed by selecting elements from the matrix, where you can move only right or down. The function should take the matrix as input and return an integer representing the length of the longest subsequence.

Palo Alto Networks
Coding & Algorithms
Software Engineer
Palo Alto Networks
November 19, 2025
Software Engineer
Onsite
Coding & Algorithms
Medium

244

12

4,107 solved


Given a matrix of integers, write a function to count the length of the longest increasing subsequence that can be formed by selecting elements from the matrix, where you can move only right or down. The function should take the matrix as input and return an integer representing the length of the longest subsequence.

Palo Alto Networks uses this problem in the Onsite to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Edge cases and input validation
Data structure selection and trade-offs
Time and space complexity analysis
Tree structures and recursion
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
  • How would you parallelize this solution?
  • How would you modify your solution to handle streaming input?
  • What if the input doesn't fit in memory?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem requires us to find the longest increasing subsequence in a matrix while only moving down or right. This suggests a dynamic programming approach, where we can calculate the longest subsequ...

Approach
  1. Initialize a 2D DP array with the same dimensions as the input matrix, filled with 1s (since the minimum subsequence length for each cell is 1).
  2. Iterate through each cell in the matrix. For ea...

Submit Your Answer
Markdown supported

Related Questions