Detect subsequence in matrix

Last updated: December 4, 2025

Quick Overview

Given a 2D matrix of characters and a target string, write a function to determine if the target string can be found as a subsequence in the matrix. A subsequence is defined as a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. The function should return true if the target string can be formed as a subsequence from the characters in the matrix, and false otherwise.

Databricks
Coding & Algorithms
Software Engineer
Databricks
December 4, 2025
Software Engineer
Onsite
Coding & Algorithms
Easy

10

12

4,970 solved


Given a 2D matrix of characters and a target string, write a function to determine if the target string can be found as a subsequence in the matrix. A subsequence is defined as a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. The function should return true if the target string can be formed as a subsequence from the characters in the matrix, and false otherwise.

Coding interviews at Databricks focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

What the Interviewer Expects
  • Identify the correct data structure and algorithm for the problem
  • Write clean, bug-free code with proper variable naming
  • Analyze time and space complexity correctly
  • Handle basic edge cases (empty input, single element)
  • Communicate your thought process while coding
Key Topics to Cover
Time and space complexity analysis
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Graph algorithms and traversal
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 optimize the space complexity of your solution?
  • How would you parallelize this solution?
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • How would you modify your solution to handle streaming input?
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 determine if a target string can be formed as a subsequence from a 2D matrix of characters, we can think of the matrix as a flat sequence of characters. The challenge is to ensure that we can find ...

Approach
  1. Initialization: Create a variable to track the length of the target string and a pointer to traverse it.
  2. Traverse the matrix: For each character in the matrix, check if it matches the cu...

Submit Your Answer
Markdown supported

Related Questions