Detect subsequence in matrix

Last updated: September 4, 2025

Quick Overview

Given a 2D matrix of characters and a string, determine if the string 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 target string, while the output is a boolean value indicating the presence of the subsequence.

JPMorgan
Coding & Algorithms
Software Engineer
JPMorgan
September 4, 2025
Software Engineer
Phone Screen
Coding & Algorithms
Medium

8

13

1,883 solved


Given a 2D matrix of characters and a string, determine if the string 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 target string, while the output is a boolean value indicating the presence of the subsequence.

JPMorgan uses this problem in the Phone Screen 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
Time and space complexity analysis
Binary search and divide and conquer
Graph algorithms and traversal
Dynamic programming and memoization
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
  • What if the input doesn't fit in memory?
  • How would you test this solution thoroughly?
  • How would your solution change if the input was sorted?
  • Can you solve this in a single pass?
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 if a given string can be formed as a subsequence by traversing adjacent cells in a 2D matrix of characters. The adjacency rule allows us to move horizontally or vertica...

Approach
  1. Initialization: Start by iterating through each cell in the matrix as a potential starting point.
  2. DFS Function: Implement a recursive DFS function that takes parameters for the current ...

Submit Your Answer
Markdown supported

Related Questions