Detect subsequence in matrix

Last updated: August 7, 2025

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 left-to-right and top-to-bottom manner. The function should return true if the subsequence exists, and false otherwise. The input consists of the matrix dimensions and the string, while the output is a boolean value indicating the presence of the subsequence.

Square/Block
Coding & Algorithms
Software Engineer
Square/Block
August 7, 2025
Software Engineer
Technical Screen
Coding & Algorithms
Hard

96

10

666 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 left-to-right and top-to-bottom manner. The function should return true if the subsequence exists, and false otherwise. The input consists of the matrix dimensions and the string, while the output is a boolean value indicating the presence of the subsequence.

Square/Block uses this problem in the Technical 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
  • Quickly identify the optimal approach and its theoretical basis
  • Handle complex algorithm design with multiple interacting components
  • Write concise, elegant code under time pressure
  • Prove correctness of your approach and discuss alternative solutions
  • Optimize beyond the obvious: discuss constant factor improvements
  • Address follow-up variations and explain how the solution generalizes
Key Topics to Cover
Data structure selection and trade-offs
Graph algorithms and traversal
Dynamic programming and memoization
Tree structures and recursion
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 optimize the space complexity of your solution?
  • How would you test this solution thoroughly?
  • What happens if the input contains duplicates?
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

In this problem, we need to determine if a given string can be formed as a subsequence by navigating through a 2D matrix of characters. The key constraint is that we can only traverse the matrix in a ...

Approach
  1. Initialization: Start by checking if the string is empty. If it is, return true since an empty string is trivially a subsequence. If the matrix is empty or the string is longer than the total n...

Submit Your Answer
Markdown supported

Related Questions