Find longest subsequence in matrix
Last updated: October 8, 2025
Quick Overview
Given a matrix of integers, write a function to find the longest increasing subsequence that can be formed by moving only right or down from the top-left corner to the bottom-right corner. The function should return the length of this subsequence. The input will be a 2D array of integers, and the output should be a single integer representing the length of the longest subsequence.
Booking.com
October 8, 20254
13
4,017 solved
Given a matrix of integers, write a function to find the longest increasing subsequence that can be formed by moving only right or down from the top-left corner to the bottom-right corner. The function should return the length of this subsequence. The input will be a 2D array of integers, and the output should be a single integer representing the length of the longest subsequence.
Booking.com 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
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
- What if the input doesn't fit in memory?
- What is the worst-case input for your solution?
- Can you solve this iteratively instead of recursively (or vice versa)?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
This problem can be viewed as a dynamic programming challenge, where we are tasked with finding the longest increasing subsequence (LIS) in a matrix from the top-left corner to the bottom-right corner...
Approach
-
Initialization: Create a 2D dp array of the same dimensions as the input matrix, initialized to 1. This is because each cell can be a subsequence of at least length 1 (itself).
-
**Iterate th...