Detect subsequence in matrix
Last updated: May 28, 2026
Quick Overview
Given a 2D matrix of characters and a string, determine if the string can be formed as a subsequence by traversing the matrix in a row-wise manner, where you can move only right or down. Return true if the string can be formed, and false otherwise. The input consists of the matrix dimensions and the string, while the output is a boolean value.
Twitter/X
May 28, 2026112
5
4,181 solved
Given a 2D matrix of characters and a string, determine if the string can be formed as a subsequence by traversing the matrix in a row-wise manner, where you can move only right or down. Return true if the string can be formed, and false otherwise. The input consists of the matrix dimensions and the string, while the output is a boolean value.
This coding problem is frequently asked during Phone Screen at Twitter/X. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Twitter/X expects candidates to write production-quality code, not just solve the puzzle.
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
- Can you solve this iteratively instead of recursively (or vice versa)?
- Can you optimize the space complexity of your solution?
- How would you test this solution thoroughly?
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
The problem at hand involves detecting if a given string can be formed as a subsequence in a 2D matrix of characters by traversing the matrix in a specific manner—only moving right or down. This indic...
Approach
- Initialization: Start by checking the dimensions of the matrix and the length of the target string. If the string is empty, return true immediately.
- DFS Function: Create a recursive func...