Detect subsequence in matrix
Last updated: November 26, 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 top-to-bottom and left-to-right manner. The function should return true if the string can be constructed as a subsequence from the matrix, and false otherwise. The input consists of the matrix dimensions and the target string, while the output is a boolean value indicating the result.
Goldman Sachs
November 26, 2025124
12
182 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 top-to-bottom and left-to-right manner. The function should return true if the string can be constructed as a subsequence from the matrix, and false otherwise. The input consists of the matrix dimensions and the target string, while the output is a boolean value indicating the result.
Goldman Sachs 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
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 optimize the space complexity of your solution?
- How would you test this solution thoroughly?
- How would your solution change if the input was sorted?
- How would you parallelize this solution?
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 requires us to determine if a given string can be formed as a subsequence by traversing a 2D matrix of characters from top to bottom and left to right. This means that we can only move in ...
Approach
- Initialize: Start from the top-left corner of the matrix (0,0) and the first character of the target string.
- DFS Function: Create a recursive DFS function that takes the current positio...