Detect subsequence in matrix

Last updated: February 16, 2026

Quick Overview

Given a 2D matrix of characters and a string, determine if the string can be found as a subsequence in the matrix, where the subsequence can be formed by moving horizontally or vertically between adjacent cells. Return a boolean indicating whether the subsequence exists in the matrix.

Neon
Coding & Algorithms
Software Engineer
Neon
February 16, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Hard

47

5

810 solved


Given a 2D matrix of characters and a string, determine if the string can be found as a subsequence in the matrix, where the subsequence can be formed by moving horizontally or vertically between adjacent cells. Return a boolean indicating whether the subsequence exists in the matrix.

Coding interviews at Neon 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
  • 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
Binary search and divide and conquer
Edge cases and input validation
Sorting and searching
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
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
  • How would you parallelize this solution?
  • Can you solve this in a single pass?
  • How would you test this solution thoroughly?
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 string can be formed as a subsequence in a 2D matrix of characters, we can use a Depth-First Search (DFS) approach. The reason DFS is suitable here is that we need to explore paths i...

Approach
  1. Loop through each cell in the matrix as a potential starting point.
  2. For each cell, initiate a DFS if the character matches the first character of the string.
  3. In the DFS function, mark the cur...

Submit Your Answer
Markdown supported

Related Questions