Detect subsequence in matrix

Last updated: May 6, 2026

Quick Overview

Given a 2D matrix of characters and a sequence of characters, determine if the sequence can be found as a subsequence in the matrix by traversing adjacent cells (horizontally or vertically). Return true if the subsequence exists, and false otherwise. The input consists of the matrix dimensions and the character sequence, while the output is a boolean value indicating the presence of the subsequence.

HRT
Coding & Algorithms
Machine Learning Engineer
HRT
May 6, 2026
Machine Learning Engineer
Technical Screen
Coding & Algorithms
Medium

52

5

3,210 solved


Given a 2D matrix of characters and a sequence of characters, determine if the sequence can be found as a subsequence in the matrix by traversing adjacent cells (horizontally or vertically). Return true if the subsequence exists, and false otherwise. The input consists of the matrix dimensions and the character sequence, while the output is a boolean value indicating the presence of the subsequence.

This coding problem is frequently asked during Technical Screen at HRT. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. HRT 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
Edge cases and input validation
Data structure selection and trade-offs
Hash maps and frequency counting
Tree structures and recursion
Sorting and searching
Binary search and divide and conquer
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 in a single pass?
  • How would you modify your solution to handle streaming input?
  • Can you optimize the space complexity of your solution?
  • What is the worst-case input for your solution?
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

To solve this problem, we can identify that it fits a Depth-First Search (DFS) pattern because we need to explore adjacent cells to check if we can form the given character sequence. We need to traver...

Approach
  1. Initialize: Set up a function that takes the matrix and the target sequence. Define a helper function for DFS that will track the current position in the matrix and the index of the current cha...

Submit Your Answer
Markdown supported

Related Questions