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
Coding & Algorithms
Software Engineer
Twitter/X
May 28, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

112

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
Tree structures and recursion
Sorting and searching
Data structure selection and trade-offs
Hash maps and frequency counting
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
  • 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 Problems
Sample 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
  1. Initialization: Start by checking the dimensions of the matrix and the length of the target string. If the string is empty, return true immediately.
  2. DFS Function: Create a recursive func...

Submit Your Answer
Markdown supported

Related Questions